【问题标题】:Fuction can't be found using tkinter使用 tkinter 找不到函数
【发布时间】:2019-12-29 04:46:48
【问题描述】:

我是 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 还不够熟悉,无法弄清楚。

【问题讨论】:

标签: python tkinter


【解决方案1】:

你应该使用:

command=self.GetNotes

因为使用command=self.GetNotes() 只会在那里调用该函数,而您需要仅在按下按钮时调用它。

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

像这样:

command=lambda: self.GetNotes()

【讨论】:

    【解决方案2】:

    使用self.GetNotes() 而不是GetNotes()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多