用python中的tkinter写一个计算器的代码
时间: 2023-03-13 18:29:07 浏览: 149
使用tkinter库来编写计算器代码,可以使用下面的代码:# 导入tkinter库
from tkinter import *# 创建窗口
window = Tk()# 设置窗口大小
window.geometry("400x400")# 设置窗口标题
window.title("计算器")# 定义函数
def calculate():
# 获取输入
number1 = entry1.get()
number2 = entry2.get()
# 计算结果
result = int(number1) + int(number2)
# 显示结果
label3 = Label(window, text="计算结果为:%s" % result)
label3.grid(row=3, column=0)# 创建标签
label1 = Label(window, text="输入第一个数字:")
label1.grid(row=0, column=0)label2 = Label(window, text="输入第二个数字:")
label2.grid(row=1, column=0)# 创建文本框
entry1 = Entry(window)
entry1.grid(row=0, column=1)entry2 = Entry(window)
entry2.grid(row=1, column=1)# 创建按钮
button1 = Button(window, text="计算", command=calculate)
button1.grid(row=2, column=0)# 运行
window.mainloop()
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)