python-tkinter简易计算器
基础预备(其实你也不需要啦^^)
这是我第一次在csdn发文章,希望以后能够坚持下来,也希望能够收获大家的指正与帮助!
最近的课堂实验上用到了tkinter库来写一个简易的计算器,于是写了一个不到一百行(不含注释)完成了基本操作,这里写一篇blog来记录我的学习历程。
网上的具体tkinter库内部的方法讲解有很多,这里比较推荐的是菜鸟编程,还是比较全面的,可以ctrl+f搜索你想要的,当然百度或者google搜索你不理解的方法更加直接喽。
简要说几个本次实验用到的2个布局:
from tkinter import *
from tkinter import messagebox
# python3 以后就是小写的t开头的
def myheart():
# 弹出窗口显示 第一个参数为title 第二个参数为内容
messagebox.showinfo('我也爱你',"你点到我的心了!")
root = Tk()
root.title('我的第一个tkinter窗口!')
# 创建窗口 后面的每一个小控件的第一个参数root都是表明在本窗口内部
# label 我是标签控件 可以显示文本和图片
# bd 字体粗细 bold bg 背景颜色 font 字体的格式和大小
# text 显示文本内容
label1 = Label(root, bd=3, bg="white", font=('宋体', 30), text="我是一个小标签").grid(column=0,row=0)
but1 = Button(root, text='我爱你', command=lambda:myheart()).grid(column=0,row=1)
root.mainloop()
运行结果:
本次制作计算器的最主要的两个控件也是这两个,一起动动小手;
最后详细的控件内容(点击):label button
代码
from tkinter import messagebox
from tkinter import *
# import keyboard
# 还可以通过导入keyboard模块实现键盘输入 本次实验没有实现
class Mcalculate(object):
def __init__(self):
self.root = Tk()
self.root.title("小徐的简易计算器")
# 设置窗口大小
self.root.minsize(300,450)
self.root.resizable(0,0)
# 设置变量用来显示
self.result = StringVar()
self.result.set('')
# 设置一个判断是否计算的按钮
self.press = False
# 设置布局
self.dispose()
self.root.mainloop()
# 用于实现布局
def dispose(self):
# 第一个标签用来显示输入的公式
label1 = Label(self.root,bd=3,bg="white",font=
('宋体',30),anchor='e',textvariable=self.result)
label1.place(x=5,y=10,width=280,height=50)
# 设置第二个标签用来显示历史记录
# lable2 = Label(self.root,bd = 2, bg = "grey",font = ('arial',25),anchor = 'e',textvariable=self.history)
# 下面是设置的所有按钮共20个 需要注意的是提前计算每个按钮的左上角x,y 设置好对应的高度和宽度
# 过程中遇到的一个问题是网上有些教程里面 举例:command = lambda:self.empty 是没有括号的
# 后来发现可能是新的语言版本问题
but1 = Button(self.root,text='(',command=lambda:self.getstr('(')).place(x = 5,y=80,width=65,height=70)
but2 = Button(self.root,text=')',command=lambda:self.getstr(')')).place(x=80,y=80,width=65,height=70)
but3 = Button(self.root,text='del',command=lambda:self.mdel()).place(x = 155,y=80,width=65,height=70)
but4 = Button(self.root,text='C',command=lambda:self.empty()).place(x = 230,y=80,width=65,height=70)
but_1 = Button(self.root,text='1',command=lambda:self.getstr('1')).place(x = 5,y=154,width=65,height=70)
but_2 = Button(self.root,text='2',command=lambda:self.getstr('2')).place(x = 80,y=154,width=65,height=70)
but_3 = Button(self.root,text='3',command=lambda:self.getstr('3')).place(x = 155,y=154,width=65,height=70)
but5 = Button(self.root,text='=',command=lambda:self.check()).place(x = 230,y=154,width=65,height=70)
but_4 = Button(self.root,text='4',command=lambda:self.getstr('4')).place(x = 5,y=228,width=65,height=70)
but_5 = Button(self.root,text='5',command=lambda:self.getstr('5')).place(x = 80,y=228,width=65,height=70)
but_6 = Button(self.root,text='6',command=lambda:self.getstr('6')).place(x = 155,y=228,width=65,height=70)
but6 = Button(self.root,text='1/x',command=lambda:self.inverse()).place(x =230,y=228,width=65,height=70)
but_7 = Button(self.root,text='7',command=lambda:self.getstr('7')).place(x = 5,y=302,width=65,height=70)
but_8 = Button(self.root,text='8',command=lambda:self.getstr('8')).place(x = 80,y=302,width=65,height=70)
but_9 = Button(self.root,text='9',command=lambda:self.getstr('9')).place(x = 155,y=302,width=65,height=70)
but_0 = Button(self.root,text='0',command=lambda:self.getstr('0')).place(x = 230,y=302,width=65,height=70)
but9 = Button(self.root,text='+',command=lambda:self.getstr('+')).place(x = 5,y=376,width=65,height=70)
but10 = Button(self.root,text='-',command=lambda:self.getstr('-')).place(x = 80,y=376,width=65,height=70)
but11 = Button(self.root,text='*',command=lambda:self.getstr('*')).place(x = 155,y=376,width=65,height=70)
but12 = Button(self.root,text='/',command=lambda:self.getstr('/')).place(x = 230,y=376,width=65,height=70)
# 对应的是"C"清空置零
def empty(self):
self.result.set('0')
self.press = False
# 计算 这里用到了关键函数eval()用于转换为可执行的python语句
def check(self):
# 如果已经按下了 = 就跳过
if self.press == True:
pass
else:
# 没有按下就计算
try:
result = eval(self.result.get())
self.result.set(str(result))
self.press = True
except SyntaxError:
# 捕捉可能存在的表达式输入错误
messagebox.showinfo('你怕不是小学生吧!','小傻瓜,快回去问问小学数学老师!!')
# 删除最后一个输入字符或者清空
def mdel(self):
strs = self.result.get()
num = len(strs)
if self.result.get() in ['','0'] or num == 1:
self.result.set('')
else:
newstr = strs[0:num-1]
self.result.set(newstr)
# 捕获表达式输入
def getstr(self,strs):
if self.result.get() == '0':
self.result.set('')
# 判断是否按下了运算符号
if self.press == False:
oldstr = self.result.get()
newstr = oldstr + strs
self.result.set(newstr)
else:
self.result.set(strs)
# 用来装饰的button事件 本次还没用到
def wait(self):
messagebox.showinfo('啥也不是!','还在开发中。。')
# "1/x" 实现倒数的功能
def inverse(self):
strs = self.result.get()
try:
num1 = 1/eval(strs)
# 显示的长度要有限制 否则会无限长度导致标签无法显示开头
self.result.set(str(num1)[:10])
except SyntaxError:
messagebox.showinfo('我最胖','小傻瓜!!检查一下你的公式。')
if __name__ == '__main__':
# 调用
Mcalculate()
未实现的功能以后小弟会慢慢慢慢慢慢的实现的*……* 。
最后,感谢阅读。
小弟跪求一赞。
同时也跪求指正。
谢谢你呀!