码迷,mamicode.com
首页 > 其他好文 > 详细

简易计算器 tkinter模块

时间:2020-02-12 14:32:51      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:clear   gif   src   inter   ==   默认   zab   spl   int   

技术图片
  1 import tkinter
  2 
  3 
  4 # # 设置显示窗体及其属性
  5 windows = tkinter.Tk()              # 70*60
  6 windows.title(SkyGrass catulator)
  7 windows.geometry(280x440)
  8 windows.resizable(width=False, height=False)
  9 
 10 
 11 # # 定义显示界面,一个输入的数字界面,另一个是结果显示界面
 12 result_user = tkinter.StringVar()       # 用户按键显示界面
 13 result_user.set(0)
 14 result_end = tkinter.StringVar()        # 计算结果返回界面
 15 result_end.set(‘‘)
 16 
 17 label = tkinter.Label(windows, font=(黑体, 21),fg=black,anchor = se,textvariable=result_user)
 18 label.place(x = 0, y = 90, width = 280, height = 50)
 19 label = tkinter.Label(windows, font=(黑体, 18),fg=#4F4F4F,anchor = se,textvariable=result_end)
 20 label.place(y = 10, width = 280, height = 70)
 21 
 22 
 23 ## 数字按键
 24 btn0 = tkinter.Button(windows,text = 0,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1, command = lambda : pressNum(0))
 25 btn0.place(x = 0,y = 380,width = 70,height = 60)
 26 
 27 btn1 = tkinter.Button(windows,text = 1,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(1))
 28 btn1.place(x = 0,y = 320,width = 70,height = 60)
 29 btn2 = tkinter.Button(windows,text = 2,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(2))
 30 btn2.place(x = 70,y = 320,width = 70,height = 60)
 31 btn3 = tkinter.Button(windows,text = 3,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(3))
 32 btn3.place(x = 140,y = 320,width = 70,height = 60)
 33 
 34 btn4 = tkinter.Button(windows,text = 4,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(4))
 35 btn4.place(x = 0,y = 260,width = 70,height = 60)
 36 btn5 = tkinter.Button(windows,text = 5,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(5))
 37 btn5.place(x = 70,y = 260,width = 70,height = 60)
 38 btn6 = tkinter.Button(windows,text = 6,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(6))
 39 btn6.place(x = 140,y = 260,width = 70,height = 60)
 40 
 41 btn7 = tkinter.Button(windows,text = 7,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(7))
 42 btn7.place(x = 0,y = 200,width = 70,height = 60)
 43 btn8 = tkinter.Button(windows,text = 8,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(8))
 44 btn8.place(x = 70,y = 200,width = 70,height = 60)
 45 btn9 = tkinter.Button(windows,text = 9,font = (微软雅黑,20),fg = (red),bg =#87CEE8,bd = 1,command = lambda : pressNum(9))
 46 btn9.place(x = 140,y = 200,width = 70,height = 60)
 47 
 48 
 49 
 50 # # 字符按键
 51 btntop = tkinter.Button(windows,text = .,font = (微软雅黑,20),fg = (blue),bg =#87CEE8,bd = 1,command = lambda : pressCompu(.))
 52 btntop.place(x = 140,y = 380,width = 70,height = 60)
 53 btnsum = tkinter.Button(windows,text = =,font = (微软雅黑,20),bg=(orange),fg=(blue),bd = 1,command = lambda : pressEqual())
 54 btnsum.place(x = 210,y = 320,width = 70,height = 120)
 55 btnadd = tkinter.Button(windows,text = +,font = (微软雅黑,20),fg = (blue),bg=#A9A9A9,bd = 1,command = lambda : pressCompu(+))
 56 btnadd.place(x = 210,y = 200,width = 70,height = 120)
 57 btnsub = tkinter.Button(windows,text = -,font = (微软雅黑,20),fg = (blue),bg=#A9A9A9,bd = 1,command = lambda : pressCompu(-))
 58 btnsub.place(x = 210,y = 140,width = 70,height = 60)
 59 btnmul = tkinter.Button(windows,text = *,font = (微软雅黑,20),fg = (blue),bg=#A9A9A9,bd = 1,command = lambda : pressCompu(*))
 60 btnmul.place(x = 140,y = 140,width = 70,height = 60)
 61 btndiv = tkinter.Button(windows,text = /,font = (微软雅黑,20),fg = (blue),bg=#A9A9A9,bd = 1,command = lambda : pressCompu(/))
 62 btndiv.place(x = 70,y = 140,width = 70,height = 60)
 63 btnclr = tkinter.Button(windows,text = AC,font = (微软雅黑,20),bg=(orange),fg = (blue),bd = 1,command = lambda : pressCompu(AC))
 64 btnclr.place(x = 0,y = 140,width = 70,height = 60)
 65 btnb = tkinter.Button(windows,text = ,font = (微软雅黑,20),fg = (blue),bg=orange,bd = 1,command = lambda : pressCompu(b))
 66 btnb.place(x = 70,y = 380,width = 70,height = 60)
 67 
 68 
 69 #操作函数
 70 user_list = []                       #设置一个变量 保存运算数字和符号的列表
 71 isPressSign = False                  #添加一个判断是否按下运算符号的标志,假设默认没有按下按钮
 72 isPressNum = False
 73 #数字函数
 74 def pressNum(num):                   #设置一个数字函数 判断是否按下数字 并获取数字将数字写在显示版上
 75     global user_list                 #全局化lists和按钮状态isPressSign
 76     global isPressSign
 77     if isPressSign == False:
 78         pass
 79     else:                            #重新将运算符号状态设置为否
 80         result_user.set(0)
 81         isPressSign = False
 82 
 83     #判断界面的数字是否为0
 84     oldnum = result_user.get()
 85     if oldnum ==0:                 #如过界面上数字为0 则获取按下的数字
 86         result_user.set(num)
 87     else:                            #如果界面上的而数字不是0  则链接上新按下的数字
 88         newnum = oldnum + num
 89         result_user.set(newnum)            #将按下的数字写到面板中
 90 
 91 
 92 #运算函数
 93 def pressCompu(sign):
 94     global user_list
 95     global isPressSign
 96     num = result_user.get()
 97     user_list.append(num)
 98 
 99     user_list.append(sign)
100     isPressSign = True
101 
102     if sign ==AC:
103         user_list.clear()
104         result_user.set(0)
105     if sign ==b:
106         a = num[0:-1]
107         user_list.clear()
108         result_user.set(a)
109 
110 
111 
112 #获取运算结果函数
113 def pressEqual():
114     global user_list
115     global isPressSign
116 
117 
118     curnum = result_user.get()           #设置当前数字变量,并获取添加到列表
119     user_list.append(curnum)
120 
121     computrStr = ‘‘.join(user_list)
122     endNum = eval(computrStr)
123     result_user.set(endNum)
124     result_end.set(computrStr)
125     user_list.clear()
126 
127 windows.mainloop()
View Code

    

简易计算器 tkinter模块

标签:clear   gif   src   inter   ==   默认   zab   spl   int   

原文地址:https://www.cnblogs.com/skygrass0531/p/12298926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!