基于tkinter的简易计算器

发布时间:2022-06-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了基于tkinter的简易计算器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

tkinter 介绍

  tkinter是python自带的GUI库,是对图形库TK的封装

  tkinter是一个跨平台的GUI库,开发的程序可以在win,linux或者mac下运行

 按钮组件:

  Button   按钮组件

  RadioButton   单选框组件

  CheckButton   选择按钮组件

  Listbox   列表框组件

 文本输入框组件:

  Entry   单行文本框组件

  Text   多行文本框组件

 标签组件:

  Label   标签组件,可以显示图片和文字

  Message   标签组件,可以根据内容将文字换行

 菜单组件:

  Menu   菜单组件

  MenuButton   菜单按钮组件,可以使用Menu替代

 其他组件:

  Canvas   画布组件

  Frame   框架组件

  Toplevel   创建子窗口容器组件

........

更多详见 https://blog.csdn.net/qq_40223983/article/details/96093396?utm_source=app&app_version=4.20.0

 

简易计算器的实现:

 1 from tkinter import * 
 2 def calculate():
 3     result=eval(equ.get())
 4     equ.set(equ.get()+'=n'+str(result))
 5 def show(buttonString):    
 6     content=equ.get()
 7     if content=='0':
 8         content=""
 9     equ.set(content+buttonString)
10 root = Tk()
11 root.title('计算器')
12 equ = StringVar()
13 equ.set('0')
14 label = Label(root, width=25, height=2, relief=RAISED, anchor=SE,
15               textvariable=equ)
16 label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
17 clearButton=Button(root, text='C', fg='blue', width=5, command=lambda:equ.set('0'))
18 clearButton.grid(row=1, column=0)
19 Button(root, text='DEL', width=5, command=lambda:equ.set(str(equ.get()[:-1]))).grid(row=1,column=1)
20 Button(root, text="%", width=5,command=lambda:show('%')).grid(row=1, column=2)
21 Button(root, text="/", width=5,command=lambda:show('/')).grid(row=1, column=3)
22 Button(root, text="7", width=5,command=lambda:show('7')).grid(row=2, column=0)
23 Button(root, text="8", width=5,command=lambda:show('8')).grid(row=2, column=1)
24 Button(root, text="9", width=5,command=lambda:show('9')).grid(row=2, column=2)
25 Button(root, text="*", width=5,command=lambda:show('*')).grid(row=2, column=3)
26 Button(root, text="4", width=5,command=lambda:show('4')).grid(row=3, column=0)
27 Button(root, text="5", width=5,command=lambda:show('5')).grid(row=3, column=1)
28 Button(root, text="6", width=5,command=lambda:show('6')).grid(row=3, column=2)
29 Button(root, text="-", width=5,command=lambda:show('-')).grid(row=3, column=3)
30 Button(root, text="1", width=5,command=lambda:show('1')).grid(row=4, column=0)
31 Button(root, text="2", width=5,command=lambda:show('2')).grid(row=4, column=1)
32 Button(root, text="3", width=5,command=lambda:show('3')).grid(row=4, column=2)
33 Button(root, text="+", width=5,command=lambda:show('+')).grid(row=4, column=3)
34 Button(root, text="0", width=5,command=lambda:show('0')).grid(row=5, column=0,columnspan=2)
35 Button(root, text=".", width=5,command=lambda:show('.')).grid(row=5, column=2)
36 Button(root, text="=", width=5,command=lambda:calculate()).grid(row=5, column=3)
37 root.mainloop()

 

 

脚本宝典总结

以上是脚本宝典为你收集整理的基于tkinter的简易计算器全部内容,希望文章能够帮你解决基于tkinter的简易计算器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: