使用python制作简易计算器

import tkinter as tk
top=tk.Tk()
top.title('制作一个计算器')
shows=tk.Label(top,width=25,text='0',bg='yellow')  #显示文本框,初值为0,背景为黄色
shows.grid(row=0,columnspan=4)                     #文本框在第0行,占用4格宽度
zero=tk.Button(top,text='0',width=5)
one=tk.Button(top,text='1',width=5)
two=tk.Button(top,text='2',width=5)
three=tk.Button(top,text='3',width=5)
four=tk.Button(top,text='4',width=5)
five=tk.Button(top,text='5',width=5)
six=tk.Button(top,text='6',width=5)
seven=tk.Button(top,text='7',width=5)
eight=tk.Button(top,text='8',width=5)
nine=tk.Button(top,text='9',width=5)
add=tk.Button(top,text='+',width=5)
sub=tk.Button(top,text='-',width=5)
mul=tk.Button(top,text='*',width=5)
div=tk.Button(top,text='/',width=5)
dot=tk.Button(top,text='.',width=5)
equal=tk.Button(top,text='=',width=5)
one.grid(row=1,column=0)
two.grid(row=1,column=1)
three.grid(row=1,column=2)
add.grid(row=1,column=3)
four.grid(row=2,column=0)
five.grid(row=2,column=1)
six.grid(row=2,column=2)
sub.grid(row=2,column=3)
seven.grid(row=3,column=0)
eight.grid(row=3,column=1)
nine.grid(row=3,column=2)
mul.grid(row=3,column=3)
zero.grid(row=4,column=0)
dot.grid(row=4,column=1)
equal.grid(row=4,column=2)
div.grid(row=4,column=3)

press_opt=False
def num_action(num):   #在数字按钮事件被中被调用的函数,传入被点击的数字
    global press_opt
    if press_opt:                       #如果上一次点击了运算按钮
        shows['text']=num                 #则显示框变为点击的数字
        press_opt=False                   #将press_opt变为 False
    else:
        shows['text']=shows['text']+num          #否则将前面点击的数字连在显示框数字后面
def zero_click():                         #数字0点击函数,调用  num_action()函数,并传入字符0
    num_action('0')
zero['command']=zero_click                #设置数字0的按钮的command值为 zero_click函数
def one_click():
    num_action('1')
one['command']=one_click
def two_click():
    num_action('2')
two['command']=two_click
def three_click():
    num_action('3')
three['command']=three_click
def four_click():
    num_action('4')
four['command']=four_click
def five_click():
    num_action('5')
five['command']=five_click
def six_click():
    num_action('6')
six['command']=six_click
def seven_click():
    num_action('7')
seven['command']=seven_click
def eight_click():
    num_action('8')
eight['command']=eight_click
def nine_click():
    num_action('9')
nine['command']=nine_click

has_dot=False                                     #值为false的时候,显示框中的数字有小数点
def dot_click():
    global has_dot
    if not has_dot:
        shows['text']=shows['text']+'.'
        has_dot=True
dot['command']=dot_click

pre_opt=''                 #记录上一次点击的运算符
pre_num=0                       #记录上一次点击的运算数
def compute():
    global pre_opt
    global pre_num
    if pre_opt=='':
        return
    pre_num=eval(pre_num)
    cur_num=eval(shows['text'])
    if pre_opt=='+':
        new_num=pre_num+cur_num
    elif pre_opt=='-':
        new_num = pre_num - cur_num
    elif pre_opt=='*':
        new_num = pre_num * cur_num
    elif pre_opt=='/':
        new_num = pre_num / cur_num
    elif pre_opt=='=':
        new_num =cur_num
    shows['text']= '%s'%new_num

def opt_click(cur_opt):             #传入当前点击的运算符
    global pre_opt
    global pre_num
    global press_opt
    global has_dot
    compute()
    pre_opt=cur_opt
    pre_num=shows['text']
    press_opt=True
    has_dot=False

def add_click():                 #加法按钮点击事件处理
    opt_click('+')
add['command']=add_click
def sub_click():
    opt_click('-')
sub['command']=sub_click
def mul_click():
    opt_click('*')
mul['command']=mul_click
def div_click():
    opt_click('/')
div['command']=div_click
def equal_click():
    opt_click('=')
equal['command'] =equal_click

top.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值