Python Tkinter 报错:'NameError: name 'select1' is not defined' 解决方法
在函数 zonghechaxunwindow() 中定义了 select1() 函数,但在创建 Button 时却出现了 select1 未定义的错误。这是因为在创建 Button 时,Python 解释器会先扫描整个文件,而此时 select1() 函数还未被定义,所以会报错。
解决方法是将 Button 创建放在函数 zonghechaxunwindow() 的末尾,这样 select1() 函数就能被正确引用了。具体代码如下:
def zonghechaxunwindow():
def select1():
# select1 函数的代码
pass
# 将 Button 创建放在函数末尾
select_button1 = tk.Button(zonghechaxunwindow, bg='pink', text='查询家在北京朝阳区的民警', font=('微软雅黑 -20'), width=40, height=1, command=select1)
select_button1.place(x=10, y=370)
# 其他代码
pass
另外,代码中还有一个小错误:Button 的 height 属性写成了 hight,需要改正。
原文地址: https://www.cveoy.top/t/topic/osoS 著作权归作者所有。请勿转载和采集!