tkinter自定义切换帧函数难度

2024-03-29 00:57:30 发布

您现在位置:Python中文网/ 问答频道 /正文

以这段代码为指导,试着做一些我自己的东西:

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

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

49号线,开关柜内 自我毁灭() AttributeError:“function”对象没有属性“destroy”

现在,我不是专家,所以我很难遵循所有这些'自我的。。。所以我不太清楚为什么它不能破坏前一帧。。。因为我引用的是另一个类中另一个函数中的swtich\u 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()

有谁能告诉我为什么会出现这个错误,它的意思是什么,以及如何消除它?谢谢!你知道吗


Tags: textselfinitloginplacewidthframetk
1条回答
网友
1楼 · 发布于 2024-03-29 00:57:30

问题是您正在将一个函数传递到switch\u框架中。您需要传递主窗口的实例。所以有两个改变可以解决这个问题。你知道吗

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\u框架声明收件人:

main_window.switch_frame(self.master, First_Login)

相关问题 更多 >