简体   繁体   English

通过按钮调用函数时出现 Tkinter 错误

[英]Tkinter error when calling function through a button

So I'm trying to use some entry widgets in tkinter and then use a function that does multiple .get() commands at the same time.所以我试图在 tkinter 中使用一些入口小部件,然后使用一个同时执行多个 .get() 命令的函数。 However I get an error that the function is not defined, even though they are within the same class.但是我得到一个错误,该函数未定义,即使它们在同一个类中。 Here's the samples of the code:以下是代码示例:

def GetSubjects():
        subject1 = subject1entry.get()
        subject2 = subject2entry.get()
        subject3= subject3entry.get()
subjectConfirm.grid(row=3, column=0, command=GetSubjects)

As I said these are both in the same class and yet the button cannot call the function.正如我所说,它们都在同一个类中,但按钮无法调用该函数。 I am relatively new to tkinter and this site so forgive me if this is easy stuff, but I just couldn't find a solution anywhere else.我对 tkinter 和这个网站比较陌生,所以如果这很简单,请原谅我,但我在其他任何地方都找不到解决方案。

Edit: Here's the whole class, I know my code is probably very suboptimal but this is my first large project I've tried编辑:这是整个班级,我知道我的代码可能不太理想,但这是我尝试过的第一个大型项目

class Menu:

    def __init__(self, master):
        frame = tk.LabelFrame(master, text="Main Menu", padx=100, pady=10)
        frame.grid(row=0, column=0, padx=15, pady=15)
        # Create timetable button
        createTimetable = tk.Button(frame, text="Create Timetable", command=self.CreateTimetable)
        createTimetable.grid(row=0, column=0)
        # Exit program button
        exitProgram = tk.Button(frame, text="Exit Program", command=self.CloseWindow)
        exitProgram.grid(row=1, column=0)

    def CloseWindow(self):
        root.destroy()

    def GetSubjects():
        subject1 = subject1entry.get()
        subject2 = subject2entry.get()
        subject3 = subject3entry.get()

    def CreateTimetable(self):
        tableWindow = tk.Toplevel(root)
        tableWindow.title('Timetable Maker Window')
        tableWindow.geometry("800x500+400+200")
        # Subject labels
        subjectlabel1 = tk.Label(tableWindow, text="Enter your first subject:")
        subjectlabel2 = tk.Label(tableWindow, text="Enter your second subject:")
        subjectlabel3 = tk.Label(tableWindow, text="Enter your third subject:")
        # Subject entry boxes
        subject1entry = tk.Entry(tableWindow)
        subject2entry = tk.Entry(tableWindow)
        subject3entry = tk.Entry(tableWindow)
        # Puts subject entry boxes on screen
        subject1entry.grid(row=0, column=1)
        subject2entry.grid(row=1, column=1)
        subject3entry.grid(row=2, column=1)
        # Puts subject labels on screen
        subjectlabel1.grid(row=0, column=0)
        subjectlabel2.grid(row=1, column=0)
        subjectlabel3.grid(row=2, column=0)
        # Creates subject confirm button
        subjectConfirm = tk.Button(tableWindow,text="Press to confirm subjects")
        # Puts subject confirm button on screen
        subjectConfirm.grid(row=3, column=0, command=GetSubjects)
        print(subject1)
        print(subject2)
        print(subject3)

Your GetSubjects() method should look like this您的GetSubjects()方法应如下所示

def GetSubjects(self):
    subject1 = self.subject1entry.get()
    subject2 = self.subject2entry.get()
    subject3 = self.subject3entry.get()

Your CreateTimetable method should look like this (incomplete but give you the right idea)你的CreateTimetable方法应该是这样的(不完整但给你正确的想法)

def CreateTimetable(self):
    # Code removed for clarity
    self.subject1entry = tk.Entry(tableWindow)
    self.subject2entry = tk.Entry(tableWindow)
    self.subject3entry = tk.Entry(tableWindow)

And your callback for the button should look like this你的按钮回调应该是这样的

subjectConfirm = tk.Button(tableWindow,text="Press to confirm subjects", command=self.GetSubjects)

The properties/methods need to be part of the Menu class so you use self.属性/方法需要是Menu类的一部分,因此您可以使用self. to tell python to make these properties of the current class (self is the typical convention but could be anything as long as you are consistent).告诉 python 创建当前类的这些属性(self 是典型的约定,但只要你是一致的,就可以是任何东西)。

Oh and the print(subject1) parts need to be at the end of the GetSubjects method instead of where they currently are.哦, print(subject1)部分需要在GetSubjects方法的末尾而不是它们当前所在的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM