Python GUI编程(Tkinter) Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数的 Unix 平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk8.0 的后续版本可 以实现本地窗口风格,并良好地运行在绝大多数平台中。
tkinter绘制简易计算器
python tkinter 全部组件(widget)及事件类型(event)一览
tkinter模块常用参数(python3)
Python tkinter(五) 文本框(Entry)组件的属性说明及示例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import tkinter #导入tkinter模块 2 from tkinter import Tk,Label #导入类Tk,Label 3 4 root = Tk() # 创建主窗口 5 root.minsize(185, 250) # 窗口可调整的最小值 6 root.maxsize(185, 250) # 窗口可调整的最大值 7 root.title('计算器') #设置标题名 8 9 rs = True 10 11 def button(evt): 12 global rs 13 14 if label['text'] == '0': #当未输入时 15 16 if evt.widget['text'] == 'C': 17 label['text'] = '0' 18 elif evt.widget['text'] == '←': 19 label['text'] = '0' 20 elif evt.widget['text'] in ['+', '-', '*', '/', '=']: 21 label['text'] = '0' 22 elif evt.widget['text'] == '.': 23 label['text'] = label['text'] + evt.widget['text'] 24 else: 25 label['text'] = evt.widget['text'] 26 else: #在输入后 27 28 if evt.widget['text'] == 'C': 29 label['text'] = '0' 30 31 elif evt.widget['text'] == '←': 32 label['text'] = label['text'][:-1] 33 print(label['text']) 34 if label['text'] == '': 35 label['text'] = '0' 36 37 elif label['text'].count('.') == 1 and evt.widget['text'] == '.': 38 return 39 elif label['text'].count('+') == 1 and evt.widget['text'] in ['+', '-', '*', '/']: 40 label['text'] = label['text'].replace('+', evt.widget['text']) 41 elif label['text'].count('-') == 1 and evt.widget['text'] in ['+', '-', '*', '/']: 42 label['text'] = label['text'].replace('-', evt.widget['text']) 43 elif label['text'].count('*') == 1 and evt.widget['text'] in ['+', '-', '*', '/']: 44 label['text'] = label['text'].replace('*', evt.widget['text']) 45 elif label['text'].count('/') == 1 and evt.widget['text'] in ['+', '-', '*', '/']: 46 label['text'] = label['text'].replace('/', evt.widget['text']) 47 48 elif evt.widget['text'] == '=': 49 label['text'] = str(eval(label['text'])) 50 rs = False 51 52 elif rs == True or evt.widget['text'] in ['+', '-', '*', '/']: 53 label['text'] = label['text']+evt.widget['text'] 54 rs = True 55 56 elif rs == False: 57 label['text'] = evt.widget['text'] 58 rs = True 59 60 #设置文本框 61 label = tkinter.Label(root, text='0', bg='#ffffff', width=22,height=2, font=("黑体", 10, "bold"), anchor='e') 62 label.grid(row=0, column=0, columnspan=4) 63 64 #设置按钮符号、位置、颜色 65 button_del=tkinter.Button(root, text='←', width=5, height=1, bg='#E0EEEE') 66 button_del.grid(row=1, column=0) 67 button_clear=tkinter.Button(root,text='C',width=5,height=1, bg='#E0EEEE').grid(row=1, column=1) 68 button_fan = tkinter.Button(root, text=' ',width=5,height=1, bg='#E0EEEE').grid(row=1, column=2) 69 button_ce = tkinter.Button(root, text=' ',width=5,height=1, bg='#E0EEEE').grid(row=1, column=3) 70 button_1 = tkinter.Button(root, text='1', width=5,height=2, bg='#E0EEEE').grid(row=2, column=0) 71 button_2 = tkinter.Button(root, text='2', width=5,height=2, bg='#E0EEEE').grid(row=2, column=1) 72 button_3 = tkinter.Button(root, text='3', width=5,height=2, bg='#E0EEEE').grid(row=2, column=2) 73 button_jia = tkinter.Button(root, text='+',width=5,height=2, bg='#E0EEEE').grid(row=2, column=3) 74 button_4 = tkinter.Button(root, text='4', width=5,height=2, bg='#E0EEEE').grid(row=3, column=0) 75 button_5 = tkinter.Button(root, text='5', width=5,height=2, bg='#E0EEEE').grid(row=3, column=1) 76 button_6 = tkinter.Button(root, text='6', width=5,height=2, bg='#E0EEEE').grid(row=3, column=2) 77 button_jian = tkinter.Button(root, text='-', width=5, height=2, bg='#E0EEEE').grid(row=3, column=3) 78 button_7 = tkinter.Button(root, text='7', width=5,height=2, bg='#E0EEEE').grid(row=4, column=0) 79 button_8 = tkinter.Button(root, text='8', width=5,height=2, bg='#E0EEEE').grid(row=4, column=1) 80 button_9 = tkinter.Button(root, text='9', width=5,height=2, bg='#E0EEEE').grid(row=4, column=2) 81 button_cheng = tkinter.Button(root, text='*', width=5, height=2, bg='#E0EEEE').grid(row=4, column=3) 82 button_0 = tkinter.Button(root, text='0', width=5,height=2, bg='#E0EEEE').grid(row=5, column=0) 83 button_dian = tkinter.Button(root, text='.', width=5, height=2, bg='#E0EEEE').grid(row=5, column=1) 84 button_deng = tkinter.Button(root, text='=', width=5, height=2, bg='#E0EEEE').grid(row=5, column=2) 85 button_chu = tkinter.Button(root, text='/', width=5,height=2, bg='#E0EEEE').grid(row=5, column=3) 86 87 88 button_del.bind_all('<Button-1>', button) # 鼠标左键按下按钮 89 90 root.mainloop() # 进入消息循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import math 2 import tkinter as tk 3 class Calc(tk.Tk):#计算器窗体类 4 def __init__(self):#初始化实例 5 tk.Tk.__init__(self) 6 self.title("计算器") 7 self.memory=0# 暂存数值 8 self.Demo() 9 #创建界面 10 def Demo(self): 11 btn_list=['C','(',')','/', 12 '7','8','9','*', 13 '4','5','6','-', 14 '1','2','3','+', 15 '+/-','0','.','='] 16 r=1 17 c=0 18 for b in btn_list: 19 self.button=tk.Button(self,text=b,width=5,command=(lambda x=b:self.operate(x))) 20 self.button.grid(row=r,column=c,padx=3,pady=6) 21 c+=1 22 if c>3: 23 c=0 24 r+=1 25 self.entry=tk.Entry(self,width=24,borderwidth=3,bg='white',font=("黑体",11)) 26 self.entry.grid(row=0,column=0,columnspan=4,padx=8,pady=6) 27 def operate(self,key): 28 if key=='=':# 输出结果 29 result=eval(self.entry.get())#获取文本框输入的值(值为=) 30 self.entry.insert(tk.END,'='+str(result))#在'='后输出计算结果 31 elif key=='C':# 清空输入框 32 self.entry.delete(0,tk.END)#将结果清零 33 elif key=='+/-':# 取相反数 34 if '=' in self.entry.get(): 35 self.entry.delete(0,tk.END) 36 elif self.entry.get()[0]=='-': 37 self.entry.delete(0) 38 else: 39 self.entry.insert(0,'-') 40 else:# 其他键 41 if '=' in self.entry.get(): 42 self.entry.delete(0,tk.END) 43 self.entry.insert(tk.END,key) 44 if __name__ == "__main__": 45 Calc().mainloop()