gpt4 book ai didi

python - 我试图通过 def 函数破坏一个窗口到另一个窗口,但我不断收到此错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:33 25 4
gpt4 key购买 nike

当我点击注册时出现这个错误

AttributeError: 'function' object has no attribute 'destroy'

我不明白为什么它不起作用我已经尝试了所有方法。我的程序现在的目标是,当我运行程序时,它从登录屏幕开始,如果我单击注册,它将关闭窗口,转到 log_to_reg 定义,其中显示 login_screen 并打开 register_screen

    #importing moduels
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

def login_screen():
#adding the login screen
login_screen = Tk()
login_screen.geometry("400x250")
login_screen.title("Tree Road School server")
login_screen.resizable(False,False)
login_screen.configure(background = "Light blue")
#adding the title label
label_title = Label(login_screen, text="LOGIN", width = '6',
font=('Arial', 25)).place(x=135, y= 25)

#adding the labels for loging in
label_username = Label(login_screen, text = "username: ", font=(15))\
.place(x=100, y= 100)
label_username1 = Label(login_screen, text = " ", font=(15), width='13')\
.place(x=175, y= 100)

label_space = Label(login_screen, text = " ", font=(15), width='13')\
.place(x=100, y= 120)
label_space1 = Label(login_screen, text = " ", font=(15), width='13')\
.place(x=175, y= 120)

label_password = Label(login_screen, text = "password: ", font=(15))\
.place(x=100, y= 130)
label_password1 = Label(login_screen, text = " ", font=(15), width='13')\
.place(x=175, y= 130)

#adding the entry
username = Entry(login_screen, width = 15, bg = "White")
username.place(x=200, y=102)

#space to make it look nicer
password = Entry(login_screen, width = 15, bg = "White", show = "*")
password.place(x=200, y=132)

#adding the buttons
login_button = Button(login_screen, text = "login",
width = 10).place(x=100, y=175)

sign_up_button = Button(login_screen, text = "Register",
width = 14, command = log_to_reg).place(x=190, y=175)

#run mainloop
login_screen.mainloop()

def register_screen():
register_screen = Tk()
register_screen.geometry("400x250")
register_screen.title("Register")
register_screen.resizable(False,False)
register_screen.configure(background = "Light blue")

def log_to_reg():
login_screen.destroy()
rigister_screen()



login_screen()

最佳答案

错误的答案:

AttributeError: 'function' object has no attribute 'destroy'

您有一个名为 login_screen 的函数,您正试图对该函数调用 destroy(),因此它会引发错误。

这部分是因为您将函数和根窗口命名为同一名称。另一部分是因为根窗口只是函数内的一个局部变量,所以 login_screen 函数之外的函数看不到里面的 tkinter 实例。

你可以使用 global 来管理这样的东西,但这并不是最好的选择。你真的想避免在 tkinter 中多次使用 Tk() 并避免使用 global

相反,我们可以将每个项目设置为一个类,并使用 tkinters Toplevel() 打开注册窗口。

这是您的代码的修改版本,使用 grid() 来管理窗口中的所有内容。我们还可以使用 withdrow()deiconify() 来管理哪些窗口可见。

import tkinter as tk

class Login(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("400x250")
self.title("Tree Road School server")
self.resizable(False,False)
self.configure(background = "Light blue")
self.columnconfigure(0, weight=1)
self.columnconfigure(2, weight=1)
self.rowconfigure(0, weight=1)
self.rowconfigure(2, weight=1)

center_frame = tk.Frame(self)
center_frame.grid(row=1, column=1, sticky="nsew")

tk.Label(center_frame, text="LOGIN", width = '6', font=('Arial', 25)).grid(row=0, column=0, columnspan=2, padx=10, pady=5)
tk.Label(center_frame, text="username: ", font=(15)).grid(row=1, column=0, padx=10, pady=5)
tk.Label(center_frame, text="password: ", font=(15)).grid(row=2, column=0, padx=10, pady=5)

self.username = tk.Entry(center_frame, width=15, bg="White")
self.username.grid(row=1, column=1, padx=10, pady=5)
self.password = tk.Entry(center_frame, width=15, bg="White", show="*")
self.password.grid(row=2, column=1, padx=10, pady=5)

tk.Button(center_frame, text="login", width=10).grid(row=3, column=0, padx=10, pady=5)
tk.Button(center_frame, text="Register", width=14, command=self.atempt_register).grid(row=3, column=1, padx=10, pady=5)
self.mainloop()

def atempt_register(self):
self.withdraw()
RegisterScreen(self, self.username.get(), self.password.get())



class RegisterScreen(tk.Toplevel):
def __init__(self, controller, username, password):
tk.Toplevel.__init__(self)
self.controller = controller
self.protocol("WM_DELETE_WINDOW", self.on_close)
self.geometry("400x250")
self.title("Register")
self.resizable(False, False)
self.configure(background = "Light blue")
self.username = username
self.password = password

tk.Label(self, text=username).grid(row=0, column=0)
tk.Label(self, text=password).grid(row=1, column=0)

def on_close(self):
self.controller.deiconify()
self.destroy()


Login()

关于python - 我试图通过 def 函数破坏一个窗口到另一个窗口,但我不断收到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com