python tkinker模块 编写简易计算器 - Go语言中文社区

python tkinker模块 编写简易计算器


import tkinter as tk

window = tk.Tk()
window.title('简易计算器')
window.resizable(0,0)   #界面是否可以通过鼠标拉升,设置为不能
window.geometry('320x420')

result = tk.StringVar()     #用来显示结果的文本
equation = tk.StringVar()   #用来显示算式的文本
result.set(' ')
equation.set('0')

#结果显示框
show_uresult = tk.Label(window,bg='white',fg='black',font=('Arail','15'),bd='0',textvariable=equation,anchor='se')
show_uresult.place(x=10,y=10,width=300,height=50)
#算式显示框
show_dresult = tk.Label(window,bg='white',fg='black',font=('Arail','30'),bd='0',textvariable=result,anchor='se')
show_dresult.place(x=10,y=60,width=300,height=50)

def getnum(num):
    temp = equation.get()
    temp2 = result.get()
    if temp.startswith('.'):
        temp = '0'+ temp
    #计算之后,按其他键归0
    if temp2 != ' ':
        temp = '0'
        temp2 = ' '
        result.set(temp2)
    if temp == '0' :
        temp = ''
    temp = temp+num
    equation.set(temp)
    if num == 'C':
        return clear()
def back():
    temp = equation.get()
    equation.set(temp[:-1])

def clear():
    equation.set('0')
    result.set(' ')

def run():
    temp = equation.get()
    temp = temp.replace('x','*')
    temp = temp.replace('÷','/')
    #小彩蛋
    if temp == '5+2+0+1+3+1+4=':
        result.set('xxx我爱你')
        return 0
    answer = eval(temp)
    result.set(answer)
button = '789÷456x123+.0C-'

for row in range(4):
    for col in range(4):
        butt = tk.Button(window,text=button[row*4+col],bg='DarkGray',command= lambda s=button[row*4+col]: getnum(s))
        butt.place(x=(col*80)+10,y=(row*55)+150,height=40,width=60)

buttEq = tk.Button(window,text='=',bg='DarkGray',command=run)
buttEq.place(x = 10 ,y = (4*55)+150,width=120,height=40)
buttback = tk.Button(window,text='←',bg='DarkGray',command=back)
buttback.place(x=170,y=(4*55)+150,width=120,height=40)
window.mainloop()

结果演示

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xhn1871356/article/details/104269819
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢