python - 使用 tkinter 找不到功能
问题描述
我是 tkinter 的新手,我不能 100% 确定整个frame、self等是如何工作的。
我有这个:
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 还不够熟悉,无法弄清楚。
解决方案
你应该使用:
command=self.GetNotes
由于 usingcommand=self.GetNotes()
只会在此处调用该函数,而仅在按下按钮时才需要调用它。
不需要使用括号()
,如果你想无缘无故地使用它,你将不得不使用lambda
.
像这样:
command=lambda: self.GetNotes()
推荐阅读
- java - 服务层和控制器中的代码分离
- windows - 用于自动解锁用户帐户的 Eventviewer eventid
- android - react-native ios app webview在屏幕关闭时播放静音
- java - 空参数错误计算节点上的 id
- python - 模块内的变量范围
- json - Firebase 实时数据库验证数组中的子对象
- react-router-v4 - 使用connected-react-router在每次路由更改时更改的“关键”是什么?
- jax-rs - Glassfish/Payara 中带有 Singleton 的 jax-rs sse 客户端
- c++ - 如何将一对向量中一对的第一个元素(字符串)与另一个字符串进行比较?
- jdbc - 未加载 Logstash 'com.mysql.jdbc.Driver'