Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。我们可以用它来实现一个简易计算器。
直接上代码:
# encoding=utf-8
from tkinter import *
class Calculator:
def __init__(self, pwindow):
self.pwindow = pwindow
self.num1 = 0
self.num2 = 0
self.sign = None # 运算符号
self.if_equal = None # 判断输入显示之前是否已进行过运算
self.init_pwindow()
def init_pwindow(self):
"""父窗口相关配置"""
self.pwindow.title('计算器') # 窗口名
self.pwindow.geometry('200x300+10+10') # 设置窗口的大小以及在屏幕上的位置
# 设置按钮
self.button_1 = Button(self.pwindow, text=1, width=5, command=lambda: self.show_num(1))
self.button_1.grid(row=1, column=0)
self.button_2 = Button(self.pwindow, text=2, width=5, command=lambda: self.show_num(2))
self.button_2.grid(row=1, column=1)
self.button_3 = Button(self.pwindow, text=3, width=5, command=lambda: self.show_num(3))
self.button_3.grid(row=1, column=2)
self.button_4 = Button(self.pwindow, text=4, width=5, command=lambda: self.show_num(4))
self.button_4.grid(row=2, column=0)
self.button_5 = Button(self.pwindow, text=5, width=5, command=lambda: self.show_num(5))
self.button_5.grid(row=2, column=1)
self.button_6 = Button(self.pwindow, text=6, width=5, command=lambda: self.show_num(6))
self.button_6.grid(row=2, column=2)
self.button_7 = Button(self.pwindow, text=7, width=5, command=lambda: self.show_num(7))
self.button_7.grid(row=3, column=0)
self.button_8 = Button(self.pwindow, text=8, width=5, command=lambda: self.show_num(8))
self.button_8.grid(row=3, column=1)
self.button_9 = Button(self.pwindow, text=9, width=5, command=lambda: self.show_num(9))
self.button_9.grid(row=3, column=2)
self.button_0 = Button(self.pwindow, text=0, width=5, command=lambda: self.show_num(0))
self.button_0.grid(row=4, column=2)
self.button_add = Button(self.pwindow, text='+', width=5, command=lambda: self.sign_num('+'))
self.button_add.grid(row=4, column=0)
self.button_add = Button(self.pwindow, text='-', width=5, command=lambda: self.sign_num('-'))
self.button_add.grid(row=4, column=1)
self.button_add = Button(self.pwindow, text='*', width=5, command=lambda: self.sign_num('*'))
self.button_add.grid(row=5, column=0)
self.button_add = Button(self.pwindow, text='/', width=5, command=lambda: self.sign_num('/'))
self.button_add.grid(row=5, column=1)
self.button_equal = Button(self.pwindow, text='=', width=5, command=self.equal_num)
self.button_equal.grid(row=5, column=2)
# 设置文本显示框
self.text_1 = Text(self.pwindow, width=18, height=3)
self.text_1.grid(row=0, column=0, columnspan=3)
def show_num(self, num):
"""显示数字到显示框"""
if self.if_equal:
self.text_1.delete(1.0, END)
self.if_equal = False
self.text_1.insert(2.0, num)
def sign_num(self, sign):
"""运算符号"""
self.sign = sign
self.num1 = self.text_1.get(1.0, END)
self.text_1.delete(1.0, END) # 清空显示框
def equal_num(self):
"""等于号"""
self.if_equal = True
self.num2 = self.text_1.get(1.0, END)
self.text_1.delete(1.0, END)
count = 0
if self.sign == '+':
count = float(self.num1) + float(self.num2)
elif self.sign == '-':
count = float(self.num1) - float(self.num2)
elif self.sign == '*':
count = float(self.num1) * float(self.num2)
else:
count = float(self.num1) / float(self.num2)
self.text_1.insert(1.0, count)
if __name__ == '__main__':
parent_window = Tk()
c = Calculator(parent_window)
parent_window.mainloop()
该计算器真的很简易,还有很多可以优化的地方。可以用pyinstaller将它打包成一个exe应用程序。