使用此代码作为指导,尝试制作我自己的东西:
https://stackoverflow.com/a/49325719/9543223
尝试使用switch_frame函数在销毁tkinter Toplevel小部件时设置新帧。切换过程正在工作,或者至少它将一帧放在另一帧之前,但是,我最后得到了这个错误:
第49行,在switch_frame中 self.frame.destroy() AttributeError:'function'对象没有属性'destroy'
现在,我不是专家,所以我很难跟上所有这些'自我...所以我不太确定为什么它不能破坏前一帧......我需要什么改变,因为我在另一个类中的另一个函数内引用了swtich_frame函数?
这是我的代码:
import os
import tkinter as tk
from tkinter import ttk
import sys
import time
Users = [('Admin','AdminPassword')]
class main_window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title('Pilot Flight and Duty Tracker')
self.geometry('1000x700+250+50')
self.resizable(width = False, height = False)
self.frame = None
self.switch_frame(Blank_Frame)
Login_Window = Login(self)
def switch_frame(self, frame_class):
new_frame = frame_class()
if self.frame is not None:
self.frame.destroy()
self.frame = new_frame
self.frame.pack()
else:
self.frame = new_frame
self.frame.pack()
class Login(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
tk.Toplevel.__init__(self, master, *args, **kwargs)
self.resizable(width = False, height = False)
self.title('User Login')
self.attributes('-topmost', True)
self.geometry('230x200+625+275')
self.grab_set()
self.transient(master)
userlabel = tk.Label(self, text="Employee ID:", font='arial 10 bold')
userlabel.place(x=25, y=5)
self.user_entry = tk.Entry(self, relief='groove', width=25, font='arial 10', bd=1)
self.user_entry.place(x=25, y=30)
passwordlabel = tk.Label(self, text="Password:", font='arial 10 bold')
passwordlabel.place(x=25, y=70)
self.password_entry = tk.Entry(self, relief='groove', width=25, font='arial 10', show="*", bd=1)
self.password_entry.place(x=25, y=95)
self.warn = tk.Label(self, font='arial 10 bold', relief='sunken', width=25)
self.warn.place(x=12, y=121)
button = tk.Button(self, text="Login", relief='groove', width=12, font='arial 15 bold', justify='center', command=self.login)
button.place(x=38, y=150)
def login(self):
global Users
username = self.user_entry.get()
pw = self.password_entry.get()
if (username, pw) in Users:
if (username, pw) == ('Admin', 'AdminPassword'):
self.warn.config(text='Login Successful!', bg='green', justify='center')
self.after(500, self.destroy)
main_window.switch_frame(main_window, First_Login)
else:
self.warn.config(text='Login Successful!', bg='green', justify='center')
self.after(500, self.destroy)
else:
self.warn.config(text="Invalid Username or Password", fg="black", bg='red', justify ='center')
class Blank_Frame(tk.Frame):
def __init__(self):
tk.Frame.__init__(self, width=1000, height=700)
class First_Login(tk.Frame):
def __init__(self):
tk.Frame.__init__(self, width=300, height=700, bg='grey60', relief='ridge', bd=3)
self.place(anchor='nw', x=350)
usercreationlabel = tk.Label(self, text='New User Creation', font='arial 20 bold', justify='center', bg='grey60')
usercreationlabel.place(anchor='nw', x=25)
newuserlabel = tk.Label(self, text='Employee Number:', font='arial 10 bold', bg='grey70', relief='sunken', width=17, justify='center')
newuserlabel.place(anchor='nw', x=3, y=40)
self.newuserentry = tk.Entry(self, font='arial 10 bold', width=10, relief='sunken', bd=2, justify='center')
self.newuserentry.place(anchor='nw', x=35, y=63)
newpasswordlabel = tk.Label(self, text='Enter New Password:', font='arial 10 bold', bg='grey70', relief='sunken', width=17)
newpasswordlabel.place(anchor='nw', x=149, y=40)
self.newpasswordentry = tk.Entry(self, font='arial 10 bold', width=19, relief='sunken', bd=2)
self.newpasswordentry.place(anchor='nw', x=150, y=63)
run = main_window()
run.mainloop()
谁能告诉我为什么我会收到这个错误,它意味着什么,以及如何摆脱它?谢谢!
答案 0 :(得分:0)
问题在于您将函数传递给switch_frame。您需要传递main_window的实例。因此,两个更改将解决此问题。
class Login(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
tk.Toplevel.__init__(self, master, *args, **kwargs)
# Add this line.
self.master = master
...
在login方法中,将main_window.switch_frame语句更改为:
main_window.switch_frame(self.master, First_Login)