limd_ 2022-05-26 09:13 采纳率: 100%
浏览 46
已结题

Python简单计算器

from tkinter import*
root = Tk()
root.geometry('500x300')      #中间的乘号可用’x’字符
# 创建一个显示结果的标签
show = Label(root,font=('宋体', 24),width=23, bg='red')
show.pack()
# 创建Frame框架,摆放按钮
p = Frame(root)
p.pack()
b1 = Button(p, text='+', width=5)
b1.grid(row=0,column=0)
b2 = Button(p,text='1', width=5)
b2.grid(row=0,column=1)
b3 = Button(p,text='2', width=5)
b3.grid(row=0,column=2)
b4 = Button(p,text='3', width=5)
b4.grid(row=0,column=3)
b5 = Button(p,text='C', width=5)
b5.grid(row=0,column=4)
c1 = Button(p,text='-', width=5)
c1.grid(row=1,column=0)
c2 = Button(p,text='4', width=5)
c2.grid(row=1,column=1)
c3 = Button(p,text='5', width=5)
c3.grid(row=1,column=2)
c4 = Button(p,text='3', width=5)
c4.grid(row=1,column=3)
c5 = Button(p,text='=', width=5)
c5.grid(row=1,column=4)
def click1(event):
   show['text']=show['text']+b1['text']
def click2(event):
   show['text']=show['text']+b2['text']
def click3(event):
    show['text']=show['text']+ b3['text']
def click5(event):
    show['text'] = ''
def click10(event):
    show['text'] = eval(show['text'])
b1.bind('<Button-1>', click1)
b2.bind('<Button-1>', click2)
b3.bind('<Button-1>', click3)
c5.bind('<Button-1>', click10)

names = ("+", "1", "2", "3", "C", "-", "4", "5", "6", "**", "*", "7", "8", "9", "//", "/", ".", "0", "%", "=")
# 遍历字符串元组
for i in range(len(names)):
#创建Button,将Button放入frame组件p中
 b = Button(p,text=names[i],width=5)
 b.grid(row=i// 5, column=i % 5)
# 为鼠标左键的单击事件绑定事件处理方法
 b.bind('<Button-1>','click')
# 为鼠标左键的双击事件绑定事件处理方法
 if b['text'] == 'C':
   b.bind('<Button-1>','clean')
b1 = Button(p,text='+', width=5)
b1.grid(row=0,column=0)
b2 = Button(p,text='1', width=5)
b2.grid(row=0,column=1)
b3 = Button(p,text='2', width=5)
b3.grid(row=0,column=2)
b4 = Button(p,text='3', width=5)
b4.grid(row=0,column=3)
b5 = Button(p,text='C', width=5)
b5.grid(row=0,column=4)
c1 = Button(p,text='-', width=5)
c1.grid(row=1,column=0)
c2 = Button(p,text='4', width=5)
c2.grid(row=1,column=1)
c3 = Button(p,text='5', width=5)
c3.grid(row=1,column=2)
c4 = Button(p,text='3', width=5)
c4.grid(row=1,column=3)
c5 = Button(p,text='=', width=5)
c5.grid(row=1,column=4)
def click(event):
        # 如果用户单击的是数字键或点号
        if (event.widget['text'] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.')):
            show['text'] = show['text'] + event.widget['text']

            # 如果用户单击了运算符
        elif(event.widget['text'] in ('+', '-', '*', '/', '%', '**', '//')):
            if show['text'][-1] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.']:
               show['text'] = show['text'] + event.widget['text']
            else:
               show['text']=show['text']+''
        elif(event.widget['text'] == '=' and show['text'] is not None):
            show['text'] = eval(show['text'])
        elif(event.widget['text'] =='C'):
            show['text'] =''
root.mainloop()

为啥按钮按了显示不出来

  • 写回答

1条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-05-26 10:07
    关注

    52行绑定 click函数,click不要用引号包起来。另外,函数要在程序前面定义,不然无法引用。
    还有你有些按钮在前面和后面定义了三遍,导致有些按钮绑定的函数被覆盖了,把重复定义的按钮删掉即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月4日
  • 已采纳回答 5月27日
  • 创建了问题 5月26日

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线