Python 3.4.1,Tkinter 8.6.3,类内事件抛出&x27;名称未定义';查找类按钮时出错
我试图在用户单击另一个按钮后将按钮的状态设置为“正常”,但无论出于何种原因,事件似乎无法找到我正在引用的按钮,并抛出一个Python 3.4.1,Tkinter 8.6.3,类内事件抛出&x27;名称未定义';查找类按钮时出错,python,class,tkinter,nameerror,Python,Class,Tkinter,Nameerror,我试图在用户单击另一个按钮后将按钮的状态设置为“正常”,但无论出于何种原因,事件似乎无法找到我正在引用的按钮,并抛出一个NameError:name'rawButton'未定义“错误。我尝试用self.预先设置按钮,但随后出现self not defined错误。我找遍了所有的地方,我一辈子都不明白为什么这不管用。。。提前感谢您提供的任何帮助 相关代码如下: import tkinter as tk from imaging import * class MainClass: root
NameError:name'rawButton'未定义“
错误。我尝试用self.
预先设置按钮,但随后出现self not defined
错误。我找遍了所有的地方,我一辈子都不明白为什么这不管用。。。提前感谢您提供的任何帮助
相关代码如下:
import tkinter as tk
from imaging import *
class MainClass:
root = tk.Tk()
root.title('Main Window')
def call_bgFrame(self):
self.background = bgFrame()
rawButton.config(state = 'normal')
labels = ['Calibration','Background','Raw Data','Bin','Plot']
calibButton = tk.Button(root,text = labels[0], width = 20, height = 5)
bgButton = tk.Button(root,text = labels[1], width = 20, height = 5)
rawButton = tk.Button(root,text = labels[2], width = 20, height = 5, state = 'disabled')
binButton = tk.Button(root,text = labels[3], width = 20, height = 5, state = 'disabled')
plotButton = tk.Button(text = labels[3], width = 40, height = 5, state = 'disabled')
calibButton.grid(row = 0, column = 0)
bgButton.grid(row=0,column=1)
rawButton.grid(row=0,column=2)
binButton.grid(row=1,column=0)
plotButton.grid(row=1,column=1,columnspan = 2)
bgButton.bind('<Button-1>', call_bgFrame)
tk.mainloop()
将tkinter作为tk导入
从图像导入*
类main类:
root=tk.tk()
root.title('主窗口')
def呼叫帧(自身):
self.background=bgFrame()
rawButton.config(状态='normal')
标签=[‘校准’、‘背景’、‘原始数据’、‘箱’、‘绘图’]
calibButton=tk.按钮(根,文本=标签[0],宽度=20,高度=5)
bgButton=tk.Button(根,文本=标签[1],宽度=20,高度=5)
rawButton=tk.Button(根,文本=标签[2],宽度=20,高度=5,状态='disabled')
binButton=tk.Button(根,文本=标签[3],宽度=20,高度=5,状态='disabled')
plotButton=tk.Button(文本=标签[3],宽度=40,高度=5,状态='disabled')
calibButton.grid(行=0,列=0)
bgButton.grid(行=0,列=1)
rawButton.grid(行=0,列=2)
binButton.grid(行=1,列=0)
plotButton.grid(行=1,列=1,列span=2)
bgButton.bind(“”,调用bgFrame)
tk.mainloop()
注意:
bgFrame()
函数是从imaging导入的函数之一,用于返回数组(使用numpy)。您的编码风格非常混乱。这个问题可以通过坚持一种更常见的编码方式来解决:将代码移到\uuuu init\uuuu
中,并将对小部件的引用保存为实例变量
import Tkinter as tk
from imaging import *
class MainClass:
def __init__(self):
root = tk.Tk()
root.title('Main Window')
labels = ['Calibration','Background','Raw Data','Bin','Plot']
self.calibButton = tk.Button(root,text = labels[0], width = 20, height = 5)
self.bgButton = tk.Button(root,text = labels[1], width = 20, height = 5)
self.rawButton = tk.Button(root,text = labels[2], width = 20, height = 5, state = 'disabled')
self.binButton = tk.Button(root,text = labels[3], width = 20, height = 5, state = 'disabled')
self.plotButton = tk.Button(text = labels[3], width = 40, height = 5, state = 'disabled')
self.calibButton.grid(row = 0, column = 0)
self.bgButton.grid(row=0,column=1)
self.rawButton.grid(row=0,column=2)
self.binButton.grid(row=1,column=0)
self.plotButton.grid(row=1,column=1,columnspan = 2)
self.bgButton.configure(command=self.call_bgFrame)
root.mainloop()
def call_bgFrame(self):
self.background = bgFrame()
self.rawButton.config(state = 'normal')
app = MainClass()
我还想更改一些其他内容,但我尽量使您的代码与原始代码尽可能相似 您的编码风格非常混乱。这个问题可以通过坚持一种更常见的编码方式来解决:将代码移到
\uuuu init\uuuu
中,并将对小部件的引用保存为实例变量
import Tkinter as tk
from imaging import *
class MainClass:
def __init__(self):
root = tk.Tk()
root.title('Main Window')
labels = ['Calibration','Background','Raw Data','Bin','Plot']
self.calibButton = tk.Button(root,text = labels[0], width = 20, height = 5)
self.bgButton = tk.Button(root,text = labels[1], width = 20, height = 5)
self.rawButton = tk.Button(root,text = labels[2], width = 20, height = 5, state = 'disabled')
self.binButton = tk.Button(root,text = labels[3], width = 20, height = 5, state = 'disabled')
self.plotButton = tk.Button(text = labels[3], width = 40, height = 5, state = 'disabled')
self.calibButton.grid(row = 0, column = 0)
self.bgButton.grid(row=0,column=1)
self.rawButton.grid(row=0,column=2)
self.binButton.grid(row=1,column=0)
self.plotButton.grid(row=1,column=1,columnspan = 2)
self.bgButton.configure(command=self.call_bgFrame)
root.mainloop()
def call_bgFrame(self):
self.background = bgFrame()
self.rawButton.config(state = 'normal')
app = MainClass()
我还想更改一些其他内容,但我尽量使您的代码与原始代码尽可能相似 您的编码风格非常混乱。这个问题可以通过坚持一种更常见的编码方式来解决:将代码移到
\uuuu init\uuuu
中,并将对小部件的引用保存为实例变量
import Tkinter as tk
from imaging import *
class MainClass:
def __init__(self):
root = tk.Tk()
root.title('Main Window')
labels = ['Calibration','Background','Raw Data','Bin','Plot']
self.calibButton = tk.Button(root,text = labels[0], width = 20, height = 5)
self.bgButton = tk.Button(root,text = labels[1], width = 20, height = 5)
self.rawButton = tk.Button(root,text = labels[2], width = 20, height = 5, state = 'disabled')
self.binButton = tk.Button(root,text = labels[3], width = 20, height = 5, state = 'disabled')
self.plotButton = tk.Button(text = labels[3], width = 40, height = 5, state = 'disabled')
self.calibButton.grid(row = 0, column = 0)
self.bgButton.grid(row=0,column=1)
self.rawButton.grid(row=0,column=2)
self.binButton.grid(row=1,column=0)
self.plotButton.grid(row=1,column=1,columnspan = 2)
self.bgButton.configure(command=self.call_bgFrame)
root.mainloop()
def call_bgFrame(self):
self.background = bgFrame()
self.rawButton.config(state = 'normal')
app = MainClass()
我还想更改一些其他内容,但我尽量使您的代码与原始代码尽可能相似 您的编码风格非常混乱。这个问题可以通过坚持一种更常见的编码方式来解决:将代码移到
\uuuu init\uuuu
中,并将对小部件的引用保存为实例变量
import Tkinter as tk
from imaging import *
class MainClass:
def __init__(self):
root = tk.Tk()
root.title('Main Window')
labels = ['Calibration','Background','Raw Data','Bin','Plot']
self.calibButton = tk.Button(root,text = labels[0], width = 20, height = 5)
self.bgButton = tk.Button(root,text = labels[1], width = 20, height = 5)
self.rawButton = tk.Button(root,text = labels[2], width = 20, height = 5, state = 'disabled')
self.binButton = tk.Button(root,text = labels[3], width = 20, height = 5, state = 'disabled')
self.plotButton = tk.Button(text = labels[3], width = 40, height = 5, state = 'disabled')
self.calibButton.grid(row = 0, column = 0)
self.bgButton.grid(row=0,column=1)
self.rawButton.grid(row=0,column=2)
self.binButton.grid(row=1,column=0)
self.plotButton.grid(row=1,column=1,columnspan = 2)
self.bgButton.configure(command=self.call_bgFrame)
root.mainloop()
def call_bgFrame(self):
self.background = bgFrame()
self.rawButton.config(state = 'normal')
app = MainClass()
我还想更改一些其他内容,但我尽量使您的代码与原始代码尽可能相似 你在那里的安排似乎很奇怪;你到底为什么要在一个类定义中运行整个代码?!请注意,
call\u bgFrame
的参数,尽管您将其命名为self
,但它将是一个事件
而不是main类
@jornsharpe老实说,我不太确定。我对结构非常糟糕,通常我使用的是小脚本,看起来没有什么不同,但现在我实际上在尝试制作程序,我发现我需要制作一个类,但我想我真的不知道如何…你似乎有一个非常奇怪的安排;你到底为什么要在一个类定义中运行整个代码?!请注意,call\u bgFrame
的参数,尽管您将其命名为self
,但它将是一个事件
而不是main类
@jornsharpe老实说,我不太确定。我对结构非常糟糕,通常我使用的是小脚本,看起来没有什么不同,但现在我实际上在尝试制作程序,我发现我需要制作一个类,但我想我真的不知道如何…你似乎有一个非常奇怪的安排;你到底为什么要在一个类定义中运行整个代码?!请注意,call\u bgFrame
的参数,尽管您将其命名为self
,但它将是一个事件
而不是main类
@jornsharpe老实说,我不太确定。我对结构非常糟糕,通常我使用的是小脚本,看起来没有什么不同,但现在我实际上在尝试制作程序,我发现我需要制作一个类,但我想我真的不知道如何…你似乎有一个非常奇怪的安排;你到底为什么要在一个类定义中运行整个代码?!请注意,call\u bgFrame
的参数,尽管您将其命名为self
,但它将是一个事件
而不是main类
@jornsharpe老实说,我不太确定。我对结构非常不熟悉,通常我使用的是小脚本,看起来没有什么不同,但现在我实际上在尝试制作程序,我发现我需要制作一个类,但我想我真的不知道如何制作…感谢您的快速响应。我注意到您使用了.configure(command=event)
而不是.bind(按钮,事件)
。这是将事件绑定到按钮的go-to方法吗?我对制作GUI非常陌生,我发现的所有教程都说是使用.bind
。还有,代码有什么让人困惑的地方吗?我不想防御性,我只是不知道自己什么时候做错了。@wes3449:是的。除非需要一些独特的非标准行为,否则不应在按钮上使用bind
。要从按钮调用函数,应始终使用命令
attribute