|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
废话不多说,直接上图
源码如下:
- from tkinter import * #直接引用tkinter全部变量,这样代码多比较方便
- root=Tk()
- root.minsize(280,495) #这里是定义窗口的大小,喜欢多大定多大
- root.title('小小计算器') #名字随意哈
- v1=StringVar() #这里定义了两个StringVar()
- v1.set(0) #v1用于显示过程
- v2=StringVar() #v2显示结果
- v2.set('')
- Label(root,textvariable=v2,font=('Aharoni',20),bg='#EEE9E9',bd='9',fg='#828282',\
- anchor='se').place(width=280,height=150)
- Label(root,textvariable=v1,font=('Aharoni',30),bg='#EEE9E9',bd='9',fg='black',\
- anchor='se').place(y=150,width=280,height=70)
- #标签的字体是WIN7自带的,如果报错可以换成黑体
- #y的布局设计好,因为和窗口大小有关
- #以下是按钮的设计,字体颜色都可以自定哈
- Button(root,text='7',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('7'))\
- .place(x=0,y=275,width=70,height=55)
- Button(root,text='8',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('8'))\
- .place(x=70,y=275,width=70,height=55)
- Button(root,text='9',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('9'))\
- .place(x=140,y=275,width=70,height=55)
- Button(root,text='4',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('4'))\
- .place(x=0,y=330,width=70,height=55)
- Button(root,text='5',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('5'))\
- .place(x=70,y=330,width=70,height=55)
- Button(root,text='6',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('6'))\
- .place(x=140,y=330,width=70,height=55)
- Button(root,text='1',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('1'))\
- .place(x=0,y=385,width=70,height=55)
- Button(root,text='2',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('2'))\
- .place(x=70,y=385,width=70,height=55)
- Button(root,text='3',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('3'))\
- .place(x=140,y=385,width=70,height=55)
- Button(root,text='AC',font=('Aharoni',20),fg='orange',bd=0.5,command=lambda:Sign1('AC'))\
- .place(x=210,y=220,width=70,height=55)
- Button(root,text='BACK',font=('Aharoni',13),fg='#4F4F4F',bd=0.5,command=lambda:Sign1('BACK'))\
- .place(x=140,y=220,width=70,height=55)
- Button(root,text='+',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:Sign1('+'))\
- .place(x=0,y=220,width=70,height=55)
- Button(root,text='-',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:Sign1('-'))\
- .place(x=70,y=220,width=70,height=55)
- Button(root,text='*',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:Sign1('*'))\
- .place(x=210,y=275,width=70,height=55)
- Button(root,text='/',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:Sign1('/'))\
- .place(x=210,y=330,width=70,height=55)
- Button(root,text='=',font=('Aharoni',20),bg='orange',fg='#4F4F4F',bd=0.5,command=lambda:Math())\
- .place(x=210,y=385,width=70,height=110)
- Button(root,text='00',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('00'))\
- .place(x=70,y=440,width=70,height=55)
- Button(root,text='.',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('.'))\
- .place(x=140,y=440,width=70,height=55)
- Button(root,text='0',font=('Aharoni',20),fg='#4F4F4F',bd=0.5,command=lambda:PressNum('0'))\
- .place(x=0,y=440,width=70,height=55)
- lists=[] #创建一个列表,用于存放数据
- PressOK=False #这里是判断按钮是否按下,默认为没按
- PressNo=False
- def PressNum(num): #设置一个数字函数,判断是否按下
- global lists #全局化列表和按钮状态
- global PressOK #按下则显示在v1 Label上
- if PressOK==False:
- pass
- else:
- v1.set(0)
- PressOK=False
- oldnum=v1.get()
- if oldnum=='0': #如果界面的数字为0,则获取新按的数字
- v1.set(num)
- else:
- newnum=oldnum+num #如果界面的数字不为0则加上新按的数字
- v1.set(newnum) #显示新数字
- def Sign1(sign):
- global lists
- global PressOK
- num=v1.get()
- lists.append(num) #将界面数字保存到列表中
- lists.append(sign) #将按下的运算符号保存列表中
- PressOK = True
- if sign=='AC': #按下AC,清空列表,并且屏幕显示数字0
- lists.clear()
- v1.set(0)
- v2.set('')
- if sign=='BACK': #清空列表,显示当前数字第一位和倒数第二位
- a=num[0:-1]
- lists.clear()
- v1.set(a)
- def Math():
- global lists
- global PressOK
- newnum1=v1.get()
- lists.append(newnum1) #设置当前数字变量,并添加到列表
- Str=''.join(lists) #列表内容用join命令将字符串连接起来
- endnum=eval(Str) #eval命令运算字符串内容
- v1.set(endnum) #v1显示运算结果
- v2.set(Str) #v2显示运算过程
- lists.clear() #清空列表
- root.mainloop()
复制代码 |
|