我是新手,我不确定整个框架、自我等是如何工作的。
我有这个:
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还不太熟悉,无法弄清楚。
发布于 2019-12-29 04:59:39
使用self.GetNotes()而不是GetNotes()。
发布于 2019-12-29 16:03:24
你应该使用:
command=self.GetNotes
因为使用command=self.GetNotes()只会在那里调用函数,而只在按下按钮时才需要调用它。
没有必要使用括号(),如果您想使用它而没有任何理由,您将不得不使用lambda。
如下所示:
command=lambda: self.GetNotes()
https://stackoverflow.com/questions/59516924
复制相似问题