我正在创建一个非常简单的计算器来学习python并使用Tkinter。出于某种原因,按下按钮后,将显示输出的消息框未更新。
应该发生什么- 1.在输入字段中输入两个数字。 2.按下操作按钮。 3.相关输出显示在消息字段中。
import tkinter as tk
#CalcLayout class to make GUI
class CalcLayout(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.grid()
self.create_widgets()
'''
create_widgets sets up the widets on the master frame
Uses grid geometry management
Creates:
Entry fields
Operation Buttons
'''
def create_widgets(self):
self.outputNum = tk.StringVar()
self.firstNum = tk.StringVar()
self.secondNum = tk.StringVar()
#Enter Field
self.entry1 = tk.Entry(self, textvariable=self.firstNum, width=10, justify="center", bd=4, selectborderwidth=3)
self.entry1.grid(row=0, column=0)
self.entry2 = tk.Entry(self, textvariable=self.secondNum, width=10, justify="center", bd=4, selectborderwidth=3)
self.entry2.grid(row=0, column=1)
#Operation Buttons
self.add_button = tk.Button(self, width=10, text="+", bg ="grey", command= self.set_output())
self.add_button.grid(row=3, column=0)
self.subtract_button = tk.Button(self, width=10, text="-", bg="grey", command= self.set_output())
self.subtract_button.grid(row=3,column=1)
self.mult_button = tk.Button(self, width=10, text="*", bg ="grey", command= self.set_output())
self.mult_button.grid(row=4, column=0)
self.divide_button = tk.Button(self, width=10, text="/", bg="grey", command= self.set_output())
self.divide_button.grid(row=4, column=1)
self.output = tk.Message(self, width=100, textvariable=self.outputNum)
self.output.grid(row=2, columnspan=2)
self.outputNum.set("INITIAL")
def set_output(self):
self.outputNum.set("123")
self.master.update_idletasks()
root = tk.Tk()
app = gui.CalcLayout(master = root)
app.mainloop()
我在项目中使用了不同的模块,但是我将所有代码都放在这里以简化它。我在做什么错了?