我正在尝试使用 Tkinter 创建一个 gui,它获取用户名和密码并连接到远程服务器并执行一项功能。我将一些杂乱的代码拼凑在一起,它或多或少地起作用了,但是当我尝试在一个整洁的模块中重新创建它时,它就坏了。它可能是一个新手python错误,但我无法发现它。编辑:澄清一下,当它起作用时,唯一的类是 setupGui 并且任何方法都在该类下。现在我已经将 gui 与方法分开了,它不起作用。
class setupGui(object):
def __init__(self, parent):
##omited general frame stuff
self.userIn = ttk.Entry(self.topFrame, width = 20)
self.userIn.grid(row = 1, column = 1)
self.passIn = ttk.Entry(self.topFrame, width = 20, show ="*")
self.passIn.grid(row = 2, column = 1)
#Buttons
self.setupbtn = ttk.Button(self.topFrame, text = "Start Setup", command = setup().startSetup())
self.setupbtn.grid(row = 3, column = 0, pady = 10)
class setup(object):
def__init__(self):
self.userName = setupGui.userIn.get()
self.userPass = setupGui.passIn.get()
def startSetup(self):
self.another_related_fucntion # about 4 related functions actually
if __name__ == '__main__':
root = Tk()
gui = setupGui(root)
root.mainloop()
而且,如果我没有将命令附加到按钮上,那么一切正常(但显然除了看起来很漂亮之外确实蹲着)。当我附加命令时,我收到了这个错误:
Traceback (most recent call last):
File "macSetup.py", line 211, in <module>
gui = setupGui(root)
File "macSetup.py", line 45, in __init__
self.setupbtn = ttk.Button(self.topFrame, text = "Start Setup", command = setup().startSetup())
File "macSetup.py", line 69, in __init__
self.userName = setupGui.userIn.get()
AttributeError: type object 'setupGui' has no attribute 'userIn'