用Python写一个传统计算器,支持GUI,需要有加减乘除按键和输入框
时间: 2023-02-09 16:46:23 浏览: 111
pythontkinter写一个简易计算器
可以使用 Python 的 Tkinter 模块来创建 GUI。下面是一个简单的计算器示例代码:
```python
from tkinter import *
def btn_click(numbers):
global operator
operator=operator + str(numbers)
text_Input.set(operator)
def btn_clear():
global operator
operator=""
text_Input.set("")
def btn_equal():
global operator
sumup=str(eval(operator))
text_Input.set(sumup)
operator=""
cal = Tk()
cal.title("Calculator")
operator=""
text_Input = StringVar()
txtDisplay = Entry(cal,font=('arial',20,'bold'), textvariable=text_Input, bd=30, insertwidth=4,
bg="powder blue", justify='right').grid(columnspan=4)
btn7=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="7",bg="powder blue",command=lambda:btn_click(7)).grid(row=1,column=0)
btn8=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="8",bg="powder blue",command=lambda:btn_click(8)).grid(row=1,column=1)
btn9=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="9",bg="powder blue",command=lambda:btn_click(9)).grid(row=1,column=2)
Addition=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="+",bg="powder blue",command=lambda:btn_click("+")).grid(row=1,column=3)
# similarly for subtraction, multiplication and division
btnClear=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="C",bg="powder blue",command=btn_clear).grid(row=4,column=0)
btnEquals=Button(cal,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),
text="=",bg="powder blue",command=btn_equal).grid(row=4,column=1)
cal.mainloop()
```
这份代码只是一个简单的示例,您可以根据自己的需求进行修改。
阅读全文