我这个代码遇到了很多问题,它给了我一个奇怪的错误。它发生在我试图关闭程序时;我收到此错误(显示在底部)。我之前也在添加.protocol之前,但是在模块中遇到错误。这是我导入tkinter的方式吗?或者我试图摧毁的是什么?
import tkinter.ttk
from tkinter.constants import *
from tkinter import *
class App(ttk.Frame):
@classmethod
def main(cls):
GUI = tkinter.Tk()
app = cls(GUI)
app.grid(sticky=NSEW)
GUI.grid_columnconfigure(0, weight=1)
GUI.grid_rowconfigure(0, weight=1)
GUI.resizable(True, False)
GUI.mainloop()
self.protocol("WM_DELETE_WINDOW", self.destroy())
GUI.protocol("WM_DELETE_WINDOW", GUI.destroy())
def __init__(self, GUI):
super().__init__(GUI)
self.create_variables()
self.create_widgets()
self.grid_widgets()
self.grid_columnconfigure(0, weight=1)
def create_variables(self):
pass
def create_widgets(self):
self.Logo = tkinter.PhotoImage(file="Logo.gif")
self.x = Label(image=self.Logo)
##Top bar Widgets##
self.button1 =ttk.Button(self, text="Profile", command=self.GetProfile)
if self.CheckLogin() == False:
self.button2 = ttk.Button(self, text="Log in", command=self.Login)
self.button3 = ttk.Button(self, text="Download",command=self.download)
self.Label2 = ttk.Label(self,text="")
def grid_widgets(self):
options = dict(sticky=NSEW, padx=3, pady=4)
options1 = dict(sticky=N)
self.x.grid(column=0,row=1, **options1)
#top bar
self.button1.grid(column = 1,row = 1,**options1)
self.button2.grid(column = 2,row = 1,**options1)
self.button3.grid(column = 3,row = 1,**options1)
#To be completed functions
def download(self):
pass
def GetProfile(self):
pass
def Login(self):
if self.Logindefault() == True:
print("login here")
elif self.Logindefault() == False:
self.v = StringVar()
print("Not logged in.")
options = dict(sticky=NSEW, padx=3, pady=4)
self.t = Toplevel(self)
self.t.title("Login")
self.t.grid_columnconfigure(0, weight=1)
self.t.grid_rowconfigure(0, weight=1)
self.t.entry1 = ttk.Entry(self.t)
self.t.entry2 = ttk.Entry(self.t)
self.t.button1 = ttk.Button(self.t,text="login",command=self.destroy)
self.t.entry1.grid(column = 0 ,row = 0, **options)
self.t.entry1.insert(0,"Username")
self.t.entry2.grid(column = 0 ,row = 1, **options)
self.t.entry2.insert(0,"Password")
self.t.button1.grid(column = 1,row = 0,rowspan=2, **options)
self.t.checkbox = ttk.Checkbutton(self.t,text="Remember me",variable=self.v)
self.t.checkbox.grid(column =0,row=2,**options)
def destroy(self):
self.usernameGO = self.t.entry1.get()
self.passwordGO = self.t.entry2.get()
print(self.usernameGO,self.passwordGO,self.v)
self.t.destroy()
def CheckLogin(self):
return False #If not logged in.
def Logindefault(self):
try:
file = open("UserLog.txt","rt")
for i in file:
if i[0:6] =="__usr__":
self.username = i.sptrip("__usr__")
elif i[0:6] =="__pss__":
self.password = i.strip("__pss__")
return True
except Exception:
#username file not found
print("error")
return False
if __name__ == '__main__':
App.main()
以下是我尝试关闭主窗口时出现的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Python34\lib\tkinter\__init__.py", line 1892, in destroy
for c in list(self.children.values()): c.destroy()
File "C:\Users\charlie\Desktop\Yahtzee - Copy.py", line 74, in destroy
self.usernameGO = self.t.entry1.get()
AttributeError: 'App' object has no attribute 't'
答案 0 :(得分:1)
self.protocol("WM_DELETE_WINDOW", self.destroy())
GUI.protocol("WM_DELETE_WINDOW", GUI.destroy())
通常,注册回调方法时,需要省略括号。否则,将立即调用这些方法,而是注册它们的返回值。这意味着destroy
将在执行Login
之前执行,因此self.t
将不存在。尝试:
self.protocol("WM_DELETE_WINDOW", self.destroy)
GUI.protocol("WM_DELETE_WINDOW", GUI.destroy)