python实现gui界面计算器
时间: 2023-06-29 20:03:00 浏览: 111
以下是一个简单的Python GUI界面计算器示例:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title('计算器')
self.display = tk.Entry(master, width=25, font=('Arial', 16))
self.display.grid(row=0, column=0, columnspan=4, pady=5)
# 数字按键
self.button1 = tk.Button(master, text='1', width=5, height=2, command=lambda:self.add_digit('1'))
self.button2 = tk.Button(master, text='2', width=5, height=2, command=lambda:self.add_digit('2'))
self.button3 = tk.Button(master, text='3', width=5, height=2, command=lambda:self.add_digit('3'))
self.button4 = tk.Button(master, text='4', width=5, height=2, command=lambda:self.add_digit('4'))
self.button5 = tk.Button(master, text='5', width=5, height=2, command=lambda:self.add_digit('5'))
self.button6 = tk.Button(master, text='6', width=5, height=2, command=lambda:self.add_digit('6'))
self.button7 = tk.Button(master, text='7', width=5, height=2, command=lambda:self.add_digit('7'))
self.button8 = tk.Button(master, text='8', width=5, height=2, command=lambda:self.add_digit('8'))
self.button9 = tk.Button(master, text='9', width=5, height=2, command=lambda:self.add_digit('9'))
self.button0 = tk.Button(master, text='0', width=5, height=2, command=lambda:self.add_digit('0'))
# 运算符按键
self.button_add = tk.Button(master, text='+', width=5, height=2, command=lambda:self.add_operator('+'))
self.button_sub = tk.Button(master, text='-', width=5, height=2, command=lambda:self.add_operator('-'))
self.button_mul = tk.Button(master, text='×', width=5, height=2, command=lambda:self.add_operator('*'))
self.button_div = tk.Button(master, text='÷', width=5, height=2, command=lambda:self.add_operator('/'))
self.button_equal = tk.Button(master, text='=', width=5, height=2, command=self.calculate)
self.button_clear = tk.Button(master, text='C', width=5, height=2, command=self.clear)
# 摆放按键
self.button1.grid(row=4, column=0, padx=5, pady=5)
self.button2.grid(row=4, column=1, padx=5, pady=5)
self.button3.grid(row=4, column=2, padx=5, pady=5)
self.button_add.grid(row=4, column=3, padx=5, pady=5)
self.button4.grid(row=3, column=0, padx=5, pady=5)
self.button5.grid(row=3, column=1, padx=5, pady=5)
self.button6.grid(row=3, column=2, padx=5, pady=5)
self.button_sub.grid(row=3, column=3, padx=5, pady=5)
self.button7.grid(row=2, column=0, padx=5, pady=5)
self.button8.grid(row=2, column=1, padx=5, pady=5)
self.button9.grid(row=2, column=2, padx=5, pady=5)
self.button_mul.grid(row=2, column=3, padx=5, pady=5)
self.button0.grid(row=5, column=0, padx=5, pady=5)
self.button_clear.grid(row=5, column=1, padx=5, pady=5)
self.button_equal.grid(row=5, column=2, padx=5, pady=5)
self.button_div.grid(row=5, column=3, padx=5, pady=5)
def add_digit(self, digit):
self.display.insert(tk.END, digit)
def add_operator(self, operator):
self.display.insert(tk.END, operator)
def calculate(self):
try:
result = eval(self.display.get())
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, 'Error')
def clear(self):
self.display.delete(0, tk.END)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
此代码使用了Python内置的GUI库 - tkinter。使用tkinter,您可以创建各种窗口部件(如按钮、文本框等),并将它们放置在屏幕上以创建GUI应用程序。
阅读全文