tkinter按钮无法识别功能?

时间:2019-06-25 04:29:14

标签: python-3.x tkinter tkinter-entry

我正在尝试获取一个tkinter按钮,以从条目中获取输入并将其放入变量中。该函数获取输入,将其放入变量并打印,按钮无法识别。

我正在用python和tkinter中的GUI制作一个简单的问答游戏。我想将用户输入与设置变量进行比较,以查看它们是否匹配。我试过将函数放在类之外,但仍然是同样的问题。这是我第一次使用tkinter,而且我的很多代码都来自。https://pythonprogramming.net/change-show-new-frame-tkinter/我不仅复制粘贴了他的代码,还观看了他的视频以尝试理解它。

class quizgame(tkinter.Tk):

  def __init__(self, *args, **kwargs):

    tkinter.Tk.__init__(self, *args, **kwargs)
    container = tkinter.Frame(self)

    container.pack(side = 'top', fill = 'both', expand = True)

    container.grid_rowconfigure(0, weight = 1)
    container.grid_columnconfigure(0, weight = 1)

    self.frames = {}

    for F in (StartPage, PageOne, PageTwo, PageThree,
              PageFour, PageFive, PageSix, PageSeven, 
              PageEight, PageNine, PageTen, LastPage):

      frame = F(container, self)

      self.frames[F] = frame

      frame.grid(row = 0, column = 0, sticky = 'nsew')

    self.show_frame(StartPage)

  def show_frame(self, cont):

    frame = self.frames[cont]
    frame.tkraise()


class StartPage(tkinter.Frame):

  def __init__(self, parent, controller):
    tkinter.Frame.__init__(self, parent)
    label = tkinter.Label(self, text = 'start page')
    label.pack(pady = 10, padx = 10)

    button = tkinter.Button(self, text = 'start game',
                command = lambda: controller.show_frame(PageOne))
    button.pack()

class PageOne(tkinter.Frame):

  def __init__(self, parent, controller):
    tkinter.Frame.__init__(self, parent)
    label = tkinter.Label(self, text = question1)
    label.pack(pady = 10, padx = 10)

    page1_entry = tkinter.Entry(self)
    page1_entry.pack()

    button1 = tkinter.Button(self, text = 'next question',
                 command = lambda: controller.show_frame(PageTwo))
    button1.pack()

    inputbtn = tkinter.Button(self, text = 'input', command = getInput())
    inputbtn.pack()

  def getInput():
    user_input = page1_entry.get()
    print(user_input)

app =  quizgame()
app.mainloop

我希望getInput()的输出可以打印用户在条目窗口小部件上写的任何内容。输出是

NameError: name 'getInput' is not defined

0 个答案:

没有答案