用python内置的GUI库tkinter编写计算器

阅读: 评论:0

用python内置的GUI库tkinter编写计算器

用python内置的GUI库tkinter编写计算器

前言:使用Python内置编写GUI界面程序的库来写一个简单的计算器。
源代码:
ps:code这东西不好解释,请看注释自己调试运行。

from tkinter import *
import tkinter as tk
window = tk.Tk()
window.title('Bin计算器')
sizable(width=0, height=0)  # 禁止使用鼠标缩放常开
### 获取屏幕分辨率然后使其窗口的时候居中
x_screen=window.winfo_screenwidth()-300
y_sreeen=window.winfo_screenheight()-350
x=int(x_screen/2)
y=int(y_sreeen/2)
ry(f'300x350+{x}+{y}')
# window icon  ps:如果复制代码记得修改成自己的图片路径!
window.iconphoto(False, tk.PhotoImage(file='bin.png'))# text box
text = Text(window, width=40, height=6, undo=True, autoseparators=False)
text.pack()# ******* button_function *******
def click_button1(event):# 按钮执行函数# 开启编辑fig(state=NORMAL)get_but_text=event.widget['text']print(type(get_but_text))text.insert(INSERT, get_but_text)# 禁止编辑fig(state=DISABLED)def button_num():# =按钮执行函数# 开启编辑fig(state=NORMAL)ex&#("1.0","end")del_text=text.delete("1.0","end")result=eval(ex)print(type(result))text.insert(INSERT, result)# 禁止编辑fig(state=DISABLED)def clear_button():  # clear text执行函数# 开启编辑fig(state=NORMAL)del_text=text.delete("1.0","end")# 禁止编辑fig(state=DISABLED)def pop_button():  #X按钮执行函数# 开启编辑fig(state=NORMAL)er&#("1.0","end")text_len=len(er)-1er=er[:-2]del_text=text.delete("1.0","end")text.insert(INSERT, er)# 禁止编辑fig(state=DISABLED)# ****** row1 buttton ********
button1 = tk.Button(window,text='1', width =5,height = 2)
button1.pack()
button1.place(x=24,y=300)
button1.bind("<Button-1>",click_button1) button2 = tk.Button(window,text='2', width =5,height = 2)
button2.pack()
button2.place(x=93,y=300)
button2.bind("<Button-1>",click_button1) button3 = tk.Button(window,text='3', width =5,height = 2,)
button3.pack()
button3.place(x=162,y=300)
button3.bind("<Button-1>",click_button1) button4 = tk.Button(window,text='+', width =5,height = 2,)
button4.pack()
button4.place(x=231,y=300)
button4.bind("<Button-1>",click_button1)# ******** row2 buttton **********
button11 = tk.Button(window,text='4', width =5,height = 2)
button11.pack()
button11.place(x=24,y=251)
button11.bind("<Button-1>",click_button1)button12 = tk.Button(window,text='5', width =5,height = 2)
button12.pack()
button12.place(x=93,y=251)
button12.bind("<Button-1>",click_button1)button13 = tk.Button(window,text='6', width =5,height = 2)
button13.pack()
button13.place(x=162,y=251)
button13.bind("<Button-1>",click_button1)button14 = tk.Button(window,text='-', width =5,height = 2)
button14.pack()
button14.place(x=231,y=251)
button14.bind("<Button-1>",click_button1)# ******* row3 buttton **********
button21 = tk.Button(window,text='7', width =5,height = 2)
button21.pack()
button21.place(x=24,y=202)
button21.bind("<Button-1>",click_button1)button22 = tk.Button(window,text='8', width =5,height = 2)
button22.pack()
button22.place(x=93,y=202)
button22.bind("<Button-1>",click_button1)button23 = tk.Button(window,text='9', width =5,height = 2)
button23.pack()
button23.place(x=162,y=202)
button23.bind("<Button-1>",click_button1)button24 = tk.Button(window,text='*', width =5,height = 2)
button24.pack()
button24.place(x=231,y=202)
button24.bind("<Button-1>",click_button1)# ********* row4 buttton ********
button31 = tk.Button(window,text='0', width =5,height = 2)
button31.pack()
button31.place(x=24,y=153)
button31.bind("<Button-1>",click_button1)button32 = tk.Button(window,text='.', width =5,height = 2)
button32.pack()
button32.place(x=93,y=153)
button32.bind("<Button-1>",click_button1)#  = num按钮要单独处理
button33 = tk.Button(window,text='=', width =5,height = 2,command=button_num)
button33.pack()
button33.place(x=162,y=153)button34 = tk.Button(window,text='/', width =5,height = 2)
button34.pack()
button34.place(x=231,y=153)
button34.bind("<Button-1>",click_button1)# ********row4 buttton **********
#  clear清空按钮要单独处理
button41 = tk.Button(window,text='clear', width =5,height = 2,command=clear_button)
button41.pack()
button41.place(x=24,y=104)button42 = tk.Button(window,text='(', width =5,height = 2)
button42.pack()
button42.place(x=93,y=104)
button42.bind("<Button-1>",click_button1)button43 = tk.Button(window,text=')', width =5,height = 2)
button43.pack()
button43.place(x=162,y=104)
button43.bind("<Button-1>",click_button1)
#  x删除按钮要单独处理
button44 = tk.Button(window,text='X', width =5,height = 2,command=pop_button)
button44.pack()
button44.place(x=231,y=104)window.mainloop()

结果:
ps:有些表达式错误导致异常的问题这里没有继续优化不过本身不影响使用;只能进行简单的加减乘除运算。

本文发布于:2024-02-04 13:51:18,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170708695756132.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:计算器   python   GUI   tkinter
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23