本文将使用python自带的图形开发包tkinter编写一个带界面的计算器程序,可以进行简单的加减乘除,非常适合入门。
是不是很棒,颜值个人感觉还是很高的。好,接下来,让我们一步步来完成这个计算器的开发。
我们默认你已经安装pycharm,且配置好基于python3.x的开发环境(如果还未配置,请参考我之前的教程)。
好,万事具备,只欠东风,让我们打开编辑器,愉快的敲起来吧
首先要导入我们用到的包,tkinter以及相应的字体包
import tkinter
import tkinter.font
接着我们来定义一个对象,并初始化一些会用到的变量
class Calculator(object):
def __init__(self):
self.all_press_lists = [] # 所有按下的按键列表
self.is_press = False
self.is_press_num = False
self.root = tkinter.Tk() # tkiter的主窗口
self.result = tkinter.StringVar() # 显示输入的数字及结果
self.record = tkinter.StringVar() # 显示计算过程
好了,接下来是我们用到的判断系统,判断是数字键还是运算键被按下,并进行记录
# 按下数字键
def press_num(self, num):
if self.is_press is False:
pass
else:
self.result.set(0)
self.is_press = False
# 判断界面的数字是否为0
old_num = self.result.get()
if old_num == '0':
self.result.set(num)
else:
new_num = old_num + num
self.result.set(new_num)
# 按下运算键
def press_compute(self, sign):
num = self.result.get()
self.all_press_lists.append(num)
self.all_press_lists.append(sign)
self.is_press = True
if sign == 'AC': # 按下'AC',清空列表内容,显示0
self.all_press_lists.clear()
self.result.set(0)
if sign == 'b': # 按下退格,当前数字逐步减一
a = num[0:-1]
self.all_press_lists.clear()
self.result.set(a)
# 获取运算结果函数
def press_equal(self):
cur_num = self.result.get()
self.all_press_lists.append(cur_num)
compute_str = ''.join(self.all_press_lists)
try:
calculate_result = eval(compute_str)
except:
calculate_result = 'bad parameter'
self.result.set(calculate_result) # 显示结果
self.record.set(compute_str + "=") # 显示运算过程
self.all_press_lists.clear() # 清空列表内容
最后,是我们的界面搭建部分
def main(self):
self.root.minsize(300, 550)
self.root.title('计算器')
input_bg = "#393943"
num_fg = "#DCDCDC"
btn_fg = "#909194"
btn_bg = "#22222C"
btn_w = 75
btn_h = 70
my_font = tkinter.font.Font(family='微软雅黑', size=20) # 设置字体
self.result.set(0)
self.record.set('')
# 显示版
label = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se',
textvariable=self.record)
label.place(width=300, height=120)
label2 = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se',
textvariable=self.result)
label2.place(y=120, width=300, height=80)
# 第一行
btn_ac = tkinter.Button(self.root, text='c', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('AC'))
btn_ac.place(x=btn_w * 0, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_back = tkinter.Button(self.root, text='←', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('b'))
btn_back.place(x=btn_w * 1, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_per = tkinter.Button(self.root, text='%', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('%'))
btn_per.place(x=btn_w * 2, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_divi = tkinter.Button(self.root, text='÷', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('/'))
btn_divi.place(x=btn_w * 3, y=200 + btn_h * 0, width=btn_w, height=btn_h)
# 第二行
btn7 = tkinter.Button(self.root, text='7', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('7'))
btn7.place(x=btn_w * 0, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn8 = tkinter.Button(self.root, text='8', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('8'))
btn8.place(x=btn_w * 1, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn9 = tkinter.Button(self.root, text='9', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('9'))
btn9.place(x=btn_w * 2, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn_mul = tkinter.Button(self.root, text='×', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('*'))
btn_mul.place(x=btn_w * 3, y=200 + btn_h * 1, width=btn_w, height=btn_h)
# 第三行
btn4 = tkinter.Button(self.root, text='4', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('4'))
btn4.place(x=btn_w * 0, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn5 = tkinter.Button(self.root, text='5', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('5'))
btn5.place(x=btn_w * 1, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn6 = tkinter.Button(self.root, text='6', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('6'))
btn6.place(x=btn_w * 2, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn_sub = tkinter.Button(self.root, text='-', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('-'))
btn_sub.place(x=btn_w * 3, y=200 + btn_h * 2, width=btn_w, height=btn_h)
# 第四行
btn1 = tkinter.Button(self.root, text='1', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('1'))
btn1.place(x=btn_w * 0, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn2 = tkinter.Button(self.root, text='2', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('2'))
btn2.place(x=btn_w * 1, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn3 = tkinter.Button(self.root, text='3', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('3'))
btn3.place(x=btn_w * 2, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn_add = tkinter.Button(self.root, text='+', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('+'))
btn_add.place(x=btn_w * 3, y=200 + btn_h * 3, width=btn_w, height=btn_h)
# 第五行
btn0 = tkinter.Button(self.root, text='0', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('0'))
btn0.place(x=btn_w * 0, y=200 + btn_h * 4, width=btn_w * 2, height=btn_h)
btn_point = tkinter.Button(self.root, text='.', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('.'))
btn_point.place(x=btn_w * 2, y=200 + btn_h * 4, width=btn_w, height=btn_h)
btn_equ = tkinter.Button(self.root, text='=', bg='#982425', font=my_font, fg=num_fg, bd=0,
command=lambda: self.press_equal())
btn_equ.place(x=btn_w * 3, y=200 + btn_h * 4, width=btn_w, height=btn_h)
self.root.mainloop()
到此我们的计算器就已经开发完成了,很多地方其实都还不完善,小伙伴们可以根据自己的需求再添加一些相应的功能,最后附上完整代码。
import tkinter
import tkinter.font
class Calculator(object):
def __init__(self):
self.all_press_lists = []
self.is_press = False
self.is_press_num = False
self.root = tkinter.Tk()
self.result = tkinter.StringVar() # 显示输入的数字及结果
self.record = tkinter.StringVar() # 显示计算过程
def main(self):
self.root.minsize(300, 550)
self.root.title('计算器')
input_bg = "#393943"
num_fg = "#DCDCDC"
btn_fg = "#909194"
btn_bg = "#22222C"
btn_w = 75
btn_h = 70
my_font = tkinter.font.Font(family='微软雅黑', size=20) # 设置字体
self.result.set(0)
self.record.set('')
# 显示版
label = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se',
textvariable=self.record)
label.place(width=300, height=120)
label2 = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se',
textvariable=self.result)
label2.place(y=120, width=300, height=80)
# 第一行
btn_ac = tkinter.Button(self.root, text='c', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('AC'))
btn_ac.place(x=btn_w * 0, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_back = tkinter.Button(self.root, text='←', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('b'))
btn_back.place(x=btn_w * 1, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_per = tkinter.Button(self.root, text='%', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('%'))
btn_per.place(x=btn_w * 2, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn_divi = tkinter.Button(self.root, text='÷', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('/'))
btn_divi.place(x=btn_w * 3, y=200 + btn_h * 0, width=btn_w, height=btn_h)
# 第二行
btn7 = tkinter.Button(self.root, text='7', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('7'))
btn7.place(x=btn_w * 0, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn8 = tkinter.Button(self.root, text='8', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('8'))
btn8.place(x=btn_w * 1, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn9 = tkinter.Button(self.root, text='9', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('9'))
btn9.place(x=btn_w * 2, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn_mul = tkinter.Button(self.root, text='×', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('*'))
btn_mul.place(x=btn_w * 3, y=200 + btn_h * 1, width=btn_w, height=btn_h)
# 第三行
btn4 = tkinter.Button(self.root, text='4', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('4'))
btn4.place(x=btn_w * 0, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn5 = tkinter.Button(self.root, text='5', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('5'))
btn5.place(x=btn_w * 1, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn6 = tkinter.Button(self.root, text='6', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('6'))
btn6.place(x=btn_w * 2, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn_sub = tkinter.Button(self.root, text='-', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('-'))
btn_sub.place(x=btn_w * 3, y=200 + btn_h * 2, width=btn_w, height=btn_h)
# 第四行
btn1 = tkinter.Button(self.root, text='1', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('1'))
btn1.place(x=btn_w * 0, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn2 = tkinter.Button(self.root, text='2', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('2'))
btn2.place(x=btn_w * 1, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn3 = tkinter.Button(self.root, text='3', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('3'))
btn3.place(x=btn_w * 2, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn_add = tkinter.Button(self.root, text='+', font=my_font, bg=btn_bg, fg=btn_fg, bd=0,
command=lambda: self.press_compute('+'))
btn_add.place(x=btn_w * 3, y=200 + btn_h * 3, width=btn_w, height=btn_h)
# 第五行
btn0 = tkinter.Button(self.root, text='0', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('0'))
btn0.place(x=btn_w * 0, y=200 + btn_h * 4, width=btn_w * 2, height=btn_h)
btn_point = tkinter.Button(self.root, text='.', font=my_font, bg=btn_bg, fg=num_fg, bd=0,
command=lambda: self.press_num('.'))
btn_point.place(x=btn_w * 2, y=200 + btn_h * 4, width=btn_w, height=btn_h)
btn_equ = tkinter.Button(self.root, text='=', bg='#982425', font=my_font, fg=num_fg, bd=0,
command=lambda: self.press_equal())
btn_equ.place(x=btn_w * 3, y=200 + btn_h * 4, width=btn_w, height=btn_h)
self.root.mainloop()
# 按下数字键
def press_num(self, num):
if self.is_press is False:
pass
else:
self.result.set(0)
self.is_press = False
# 判断界面的数字是否为0
old_num = self.result.get()
if old_num == '0':
self.result.set(num)
else:
new_num = old_num + num
self.result.set(new_num)
# 按下运算键
def press_compute(self, sign):
num = self.result.get()
self.all_press_lists.append(num)
self.all_press_lists.append(sign)
self.is_press = True
if sign == 'AC': # 按下'AC',清空列表内容,显示0
self.all_press_lists.clear()
self.result.set(0)
if sign == 'b': # 按下退格,当前数字逐步减一
a = num[0:-1]
self.all_press_lists.clear()
self.result.set(a)
# 获取运算结果函数
def press_equal(self):
cur_num = self.result.get()
self.all_press_lists.append(cur_num)
compute_str = ''.join(self.all_press_lists)
try:
calculate_result = eval(compute_str)
except:
calculate_result = 'bad parameter'
self.result.set(calculate_result) # 显示结果
self.record.set(compute_str + "=") # 显示运算过程
self.all_press_lists.clear() # 清空列表内容
if __name__ == '__main__':
my_calculator = Calculator()
my_calculator.main()