0

使用此代码作为指南尝试制作我自己的东西:

https://stackoverflow.com/a/49325719/9543223

当 tkinter Toplevel 小部件被销毁时,尝试使用 switch_frame 函数设置新框架。切换过程正在工作,或者至少将一帧放在另一帧前面,但是,最后我得到了这个错误:

第 49 行,在 switch_frame self.frame.destroy() AttributeError: 'function' object has no attribute '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()

谁能告诉我为什么会出现此错误,这意味着什么以及如何摆脱它?谢谢!

4

1 回答 1

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
        ...

在登录方法中,将 main_window.switch_frame 语句更改为:

main_window.switch_frame(self.master, First_Login)
于 2018-04-13T12:20:46.883 回答