简体   繁体   English

没有可摧毁的属性

[英]no attribute to destroy

I have two functions the top_signup_click and the top_login_click .我有两个函数top_signup_clicktop_login_click The top_signup_click function is below the top_login_click function.top_signup_click功能低于top_login_click功能。 The top_login_click function doesn't work because the_top_signup_click function is below it. top_login_click函数不起作用,因为the_top_signup_click函数在它下面。 To fix that issue I'll have to put the top_signup_click above the top_login_click function, which does solve the issue but it also creates another issue, now the top_signup_click function doesn't work.要解决该问题,我必须将top_signup_click放在top_login_click函数上方,这确实解决了该问题,但也会产生另一个问题,现在top_signup_click函数不起作用。 For both functions to work they both have to be below each other, but I don't think that's possible.要使这两个功能正常工作,它们都必须低于彼此,但我认为这是不可能的。 If you know any other ways to do this please help.如果您知道任何其他方法可以做到这一点,请帮助。 I tried using global functions to fix it but it didn't work.我尝试使用全局函数来修复它,但它没有用。

import tkinter as tk
import tkinter.font as font
import tkinter.messagebox 

signup_button = None
root = tk.Tk()
root.geometry('360x460')

#login stuff
def login_click():
    readfile = open("emails.txt", "r")
    lines = readfile.readlines()
    isIN = False
    for line in lines:
        
        if f'{entry.get()}, {entry1.get()}' in line:
            isIN = True
    if not isIN:
        tk.messagebox.showinfo(message ="You got your email/password wrong, are you sure you signed in?")
    else:
        tk.messagebox.showinfo(message ="You hacker, the email/password is correct") 

def signup_click():
    file = open("emails.txt","a")
    file.write (entry.get())
    file.write(f', {entry1.get()}\n')

def top_login_click():
    global signup_button
    signup_button.destroy()
    login_button = tk.Button(root, text="Login", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
    login_button['font'] = button_font
    login_button.place(x=88,y=330)
#when the top sign up button is pressed

def top_signup_click():
    global login_button
    login_button.destroy()
    signup_button = tk.Button(root, text="Sign-Up", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
    signup_button['font'] = button_font
    signup_button.place(x=88,y=330)



top_font = font.Font(family='Helvetica', size=14, weight='bold')
label_font = font.Font(family='Helvetica', size=20)
button_font = font.Font(family='Helvetica', size=14, weight='bold')

top_login_button = tk.Button(root,text = 'Login',width = 15, height = 3, command = top_login_click)
top_login_button.place(x=0,y=0)
top_login_button['font'] = top_font


top_signup_button = tk.Button(root,text = 'Sign-Up',width = 15, height = 3, command = top_signup_click)
top_signup_button.place(x=180,y=0)
top_signup_button['font'] = top_font


username_label = tk.Label(root, text = 'Username: ' )
username_label['font'] =label_font
username_label.place(x=120,y=120)

entry = tk.Entry(root, width = 40)
entry.place(x=65,y=170, height = 30)

password_label = tk.Label(root, text = 'Password: ' )
password_label['font'] =label_font
password_label.place(x=120,y=230)

entry1 = tk.Entry(root, width = 40)
entry1.place(x=65,y=280, height = 30)

login_button = tk.Button(root, text="Login", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
login_button['font'] = button_font
login_button.place(x=88,y=330)



root.mainloop()

The top_login_click function is on line 29-34 and the top_signup_click function is on line 37-42. top_login_click函数在第 29-34 行, top_signup_click函数在第 37-42 行。 Thanks in advance.提前致谢。

I wanted to comment this but seems like I don't have enough reputation but here is what I think.我想对此发表评论,但似乎我没有足够的声誉,但这就是我的想法。

You have set the variable signup_button = None right below the import statements so signup_button is not really a tk.Button object, instead it's a NoneType variable.您已在 import 语句正下方设置变量signup_button = None因此signup_button并不是真正的tk.Button对象,而是一个NoneType变量。

So just remove that statement and create所以只需删除该语句并创建

signup_button = tk.Button(<args here>)

and You should be good.你应该很好。

and try using place_forget() instead of destroy() IFF destroy doesn't work for you.并尝试使用 place_forget() 而不是 destroy() IFF destroy 对您不起作用。

Edit: What I also think in your case that you may not really need to destroy the signup_up button because you haven't created one yet.编辑:在您的情况下,我还认为您可能不需要销毁signup_up按钮,因为您还没有创建一个。 Think about it this way (based on the GUI that comes up on running your code) - on the landing screen of your app (the first screen that comes up), you have a login form with two entry boxes for email & pword and a login button, along with two more buttons called top_login_button and top_signup_button .以这种方式思考(基于运行代码时出现的 GUI) - 在您的应用程序的登陆屏幕(出现的第一个屏幕)上,您有一个登录表单,其中包含两个用于电子邮件和密码的输入框和一个登录按钮,以及另外两个名为top_login_buttontop_signup_button按钮。 So You DO NOT have a signup_button as of now (and according to your code signup_button = None it's not a button).所以你现在没有signup_button (并且根据你的代码signup_button = None它不是一个按钮)。 So if a user now clicks on the top_login_button , the statement signup_button.destroy() will look for widget with the name signup_button (which is set to None by you) so it raises the error.因此,如果用户现在单击top_login_button ,语句signup_button.destroy()将查找名为signup_button小部件(由您设置为None ),因此会引发错误。

You code will run fine in a case where user clicks on top_signup_button before clicking on top_login_button because in that case the signup_button will be actually set to be a tk.Button object.如果用户在单击top_signup_button之前单击top_login_button您的代码将运行良好,因为在这种情况下, signup_button将实际设置为tk.Button对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM