问题遇到的现象和发生背景
在下拉列表中选择“按钮计算”,弹出新的窗口计算器,但是无法点击按钮进行运算。
问题相关代码,请勿粘贴截图
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
import matplotlib.pyplot as plt
import numpy as np
win =tk.Tk()
win.title("计算器")
win.geometry('250x150')
#创建一个计算器方式容器
monty = ttk.LabelFrame(win,text='计算器功能')
monty.grid(column=0, row=0, padx=10, pady=10)
aLabel = ttk.Label(monty, text = 'A label')
ttk.Label(monty,text='请选择一个功能:').grid(column=1,row=0)
# 按钮的方法
def clickMe():
action.configure(text=combobox.get())
if combobox.get()=='按钮计算':
win2 =tk.Tk()
win2.geometry('320x530')
win2.title('按钮计算')
win2.geometry('600x400')
#输入
e=''
def add(n):
global e
e += n
Label_result.config(text = e)
#清除
def clear():
global e
e = ''
Label_result.config(text = e)
#计算
def calculate():
global e
result = ''
if e != '':
try:
result = eval(e)
except:
result = 'error'
e = ''
Label_result.config(text = result)
#显示算式和答案
Label_result = tk.Label(win2,text = '',bg='white')
Label_result.place(x = 2,y=10,width = 398,height = 40)
#键盘
btn7 = tk.Button(win2,text = '7',font=('宋体',16),command = lambda:add('7'))
btn7.place(x = 100*0,y = 60*0+60,width = 100,height = 60)
btn8 = tk.Button(win2,text = '8',font=('宋体',16),command = lambda:add('8'))
btn8.place(x = 100*1,y = 60*0+60,width = 100,height = 60)
btn9 = tk.Button(win2,text = '9',font=('宋体',16),command = lambda:add('9'))
btn9.place(x = 100*2,y = 60*0+60,width = 100,height = 60)
btnadd = tk.Button(win2,text = '+',font=('宋体',16),command = lambda:add('+'))
btnadd.place(x = 100*3,y = 60*0+60,width = 100,height = 60)
btn4 = tk.Button(win2,text = '4',font=('宋体',16),command = lambda:add('4'))
btn4.place(x = 100*0,y = 6 btn4 = tk.Button(win2,text = '4',font=('宋体',16),command = lambda:add('4'))
btn4.place(x = 100*0,y = 60*1+60,width = 100,height = 60)
btn5 = tk.Button(win2,text = '5',font=('宋体',16),command = lambda:add('5'))
btn5.place(x = 100*1,y = 60*1+60,width = 100,height = 60)
btn6 = tk.Button(win2,text = '6',font=('宋体',16),command = lambda:add('6'))
btn6.place(x = 100*2,y = 60*1+60,width = 100,height = 60)
btnjian = tk.Button(win2,text = '-',font=('宋体',16),command = lambda:add('-'))
btnjian.place(x = 100*3,y = 60*1+60,width = 100,height = 60)
btn1 = tk.Button(win2,text = '1',font=('宋体',16),command = lambda:add('1'))
btn1.place(x = 100*0,y = 60*2+60,width = 100,height = 60)
btn2 = tk.Button(win2,text = '2',font=('宋体',16),command = lambda:add('2'))
btn2.place(x = 100*1,y = 60*2+60,width = 100,height = 60)
btn3 = tk.Button(win2,text = '3',font=('宋体',16),command = lambda:add('3'))
btn3.place(x = 100*2,y = 60*2+60,width = 100,height = 60)
btnx = tk.Button(win2,text = '*',font=('宋体',16),command = lambda:add('*'))
btnx.place(x = 100*3,y = 60*2+60,width = 100,height = 60)
btno = tk.Button(win2,text = '.',font=('宋体',16),command = lambda:add('.'))
btno.place(x = 100*0,y = 60*3+60,width = 100,height = 60)
btn0 = tk.Button(win2,text = '0',font=('宋体',16),command = lambda:add('0'))
btn0.place(x = 100*1,y = 60*3+60,width = 100,height = 60)
btnch = tk.Button(win2,text = '%',font=('宋体',16),command = lambda:add('%'))
btnch.place(x = 100*2,y = 60*3+60,width = 100,height = 60)
btnchu = tk.Button(win2,text = '/',font=('宋体',16),command = lambda:add('/'))
btnchu.place(x = 100*3,y = 60*3+60,width = 100,height = 60)
btnd = tk.Button(win2,text = '=',font=('宋体',16),command = lambda:calculate())
btnd.place(x = 100*0,y = 60*4+60,width = 100*3,height = 60)
btnc = tk.Button(win2,text = 'C',font=('宋体',16),command = lambda:clear())
btnc.place(x = 100*3,y = btnd = tk.Button(win2,text = '=',font=('宋体',16),command = lambda:calculate())
btnd.place(x = 100*0,y = 60*4+60,width = 100*3,height = 60)
btnc = tk.Button(win2,text = 'C',font=('宋体',16),command = lambda:clear())
btnc.place(x = 100*3,y = 60*4+60,width = 100,height = 60)
win2.mainloop()
#按钮
action= ttk.Button(monty,text='点击我',command =clickMe)
action.grid(column = 2, row=1)
#创建一个下拉列表
var = tk.StringVar()
combobox = tk.ttk.Combobox(monty,width=12,textvariable=var,state='readonly')
combobox['values'] = ('按钮计算','历史记录','函数图像')
combobox.grid(column=1,row=1)
combobox.current(0)
#滚动文本框
scrolW = 30
scrolH = 3
scr = scrolledtext.ScrolledText(monty,width=scrolW,height=scrolH)
scr.grid(column=0,columnspan=3)
win.mainloop()
运行结果及报错内容
“#输入、#计算、#清除”那三部分的代码有问题,应该是全局变量e是错误的,但不明白为什么不能用global