首页 > 解决方案 > 使用 tkinter 找不到功能

问题描述

我是 tkinter 的新手,我不能 100% 确定整个frameself等是如何工作的。

我有这个:

class ScalesPage(Frame):
        def GetNotes():
                key = KeyEntry.get()
                intervals = ScaleEntry.get()
                WholeNotes = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B']*4
                Root = WholeNotes.index(key)
                Chromatic = WholeNotes[Root:Root+12]
                print([Chromatic[i] for i in scales[intervals]])

        def __init__(self, parent, controller):
                Frame.__init__(self, parent)
                global KeyEntry
                KeyEntry = Entry(self)
                KeyEntry.pack()
                global ScaleEntry
                ScaleEntry = Entry(self)
                ScaleEntry.pack()
                ScaleButtonEnter = Button(self, text="Enter", command=GetNotes)
                ScaleButtonEnter.pack()

我正在尝试ScaleButtonEnter执行GetNotes(),但我不断收到错误

NameError:名称“GetNotes”未定义

我假设我错过了一些简单的东西,但我对 tkinter 还不够熟悉,无法弄清楚。

标签: pythontkinter

解决方案


你应该使用:

command=self.GetNotes

由于 usingcommand=self.GetNotes()只会在此处调用该函数,而仅在按下按钮时才需要调用它。

不需要使用括号(),如果你想无缘无故地使用它,你将不得不使用lambda.

像这样:

command=lambda: self.GetNotes()


推荐阅读