python gui界面实例
时间: 2023-09-08 10:10:20 浏览: 182
这里提供一个使用 Python Tkinter 模块实现的 GUI 界面示例,包含一个简单的计算器:
```python
from tkinter import *
class Calculator:
def __init__(self):
self.root = Tk()
self.root.title("Calculator")
# 添加输入框
self.entry = Entry(self.root, width=35, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 添加按钮
self.button_1 = Button(self.root, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = Button(self.root, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = Button(self.root, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = Button(self.root, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = Button(self.root, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = Button(self.root, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = Button(self.root, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = Button(self.root, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = Button(self.root, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_0 = Button(self.root, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
self.button_add = Button(self.root, text="+", padx=39, pady=20, command=self.button_add)
self.button_subtract = Button(self.root, text="-", padx=41, pady=20, command=self.button_subtract)
self.button_multiply = Button(self.root, text="*", padx=40, pady=20, command=self.button_multiply)
self.button_divide = Button(self.root, text="/", padx=41, pady=20, command=self.button_divide)
self.button_clear = Button(self.root, text="Clear", padx=79, pady=20, command=self.button_clear)
self.button_equal = Button(self.root, text="=", padx=91, pady=20, command=self.button_equal)
# 按钮布局
self.button_1.grid(row=3, column=0)
self.button_2.grid(row=3, column=1)
self.button_3.grid(row=3, column=2)
self.button_4.grid(row=2, column=0)
self.button_5.grid(row=2, column=1)
self.button_6.grid(row=2, column=2)
self.button_7.grid(row=1, column=0)
self.button_8.grid(row=1, column=1)
self.button_9.grid(row=1, column=2)
self.button_0.grid(row=4, column=0)
self.button_clear.grid(row=4, column=1, columnspan=2)
self.button_add.grid(row=5, column=0)
self.button_subtract.grid(row=6, column=0)
self.button_multiply.grid(row=6, column=1)
self.button_divide.grid(row=6, column=2)
self.button_equal.grid(row=5, column=1, columnspan=2)
self.root.mainloop()
def button_click(self, number):
current = self.entry.get()
self.entry.delete(0, END)
self.entry.insert(0, str(current) + str(number))
def button_clear(self):
self.entry.delete(0, END)
def button_add(self):
first_number = self.entry.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
self.entry.delete(0, END)
def button_subtract(self):
first_number = self.entry.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
self.entry.delete(0, END)
def button_multiply(self):
first_number = self.entry.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
self.entry.delete(0, END)
def button_divide(self):
first_number = self.entry.get()
global f_num
global math
math = "division"
f_num = int(first_number)
self.entry.delete(0, END)
def button_equal(self):
second_number = self.entry.get()
self.entry.delete(0, END)
if math == "addition":
self.entry.insert(0, f_num + int(second_number))
elif math == "subtraction":
self.entry.insert(0, f_num - int(second_number))
elif math == "multiplication":
self.entry.insert(0, f_num * int(second_number))
elif math == "division":
self.entry.insert(0, f_num / int(second_number))
calculator = Calculator()
```
这个计算器界面包含了数字按钮、四则运算符、清除键和等于键,并且可以进行简单的数学计算。
阅读全文