Python 2.7 Python Tkinter实例没有属性';tk';
AttributeError:MyGUI实例没有属性“tk” 另外,如何使创建的窗口具有固定大小,而不能用鼠标调整大小?或者在通过单击按钮更改标签值之后 我的代码:Python 2.7 Python Tkinter实例没有属性';tk';,python-2.7,tkinter,Python 2.7,Tkinter,AttributeError:MyGUI实例没有属性“tk” 另外,如何使创建的窗口具有固定大小,而不能用鼠标调整大小?或者在通过单击按钮更改标签值之后 我的代码: from Tkinter import* class MyGUI(Frame): def __init__(self): self.__mainWindow = Tk() #lbl self.labelText = 'label message' self
from Tkinter import*
class MyGUI(Frame):
def __init__(self):
self.__mainWindow = Tk()
#lbl
self.labelText = 'label message'
self.depositLabel = Label(self.__mainWindow, text = self.labelText)
#buttons
self.hi_there = Button(self.__mainWindow)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.testeo
self.QUIT = Button(self.__mainWindow)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
#place on view
self.depositLabel.pack()
self.hi_there.pack() #placed in order!
self.QUIT.pack()
#What does it do?
mainloop()
def testeo(self):
self.depositLabel['text'] = 'c2value'
print "testeo"
def depositCallBack(self,event):
self.labelText = 'change the value'
print(self.labelText)
self.depositLabel['text'] = 'change the value'
myGUI = MyGUI()
怎么了?
谢谢您应该为
框架
调用超级构造函数。不确定,但我想这将设置quit
命令所依赖的tk
属性。之后,就不需要创建自己的Tk()
实例了
def __init__(self):
Frame.__init__(self)
# self.__mainWindow = Tk()
当然,您还必须相应地更改小部件的构造函数调用,例如
self.hi_there = Button(self) # self, not self.__mainWindow
或更好(或至少更短):直接在构造函数中设置所有属性:
self.hi_there = Button(self, text="Hello", command=self.testeo)
还要将self.pack()
添加到构造函数中
(或者,您可以将quit命令更改为
self.\uu main window.quit
,但我认为上面的样式更适合创建帧,请参见示例)此错误通常意味着您正在调用SomeTKSuperClass.\uuu init\uu
并忘记第一个参数,该参数必须是self
。请记住,\uuuuu init\uuuu
在此上下文中是类方法(静态函数),而不是实例方法,这意味着您必须显式地传递它self