如何解决没有属性可以破坏
我有两个功能top_signup_click
和top_login_click
。 top_signup_click
函数在top_login_click
函数之下。 top_login_click
函数不起作用,因为它位于the_top_signup_click
函数下面。要解决该问题,我必须将top_signup_click
放在top_login_click
函数上方,虽然确实可以解决该问题,但它还会产生另一个问题,现在top_signup_click
函数不起作用。为了使这两个功能同时起作用,它们都必须位于彼此之间,但我认为这是不可能的。如果您知道执行此操作的其他方法,请提供帮助。我尝试使用全局函数来修复它,但是没有用。
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",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',weight='bold')
top_login_button = tk.Button(root,text = 'Login',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',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,height = 2)
login_button['font'] = button_font
login_button.place(x=88,y=330)
root.mainloop()
top_login_click
函数在第29-34行,而top_signup_click
函数在第37-42行。预先感谢。
解决方法
我想对此发表评论,但似乎我没有足够的声誉,但这就是我的想法。
您已在import语句下方设置了变量signup_button = None
,因此signup_button
并不是一个tk.Button
对象,而是一个NoneType
变量。
所以只需删除该语句并创建
signup_button = tk.Button(<args here>)
你应该很好。
并尝试使用place_forget()代替destroy()IFF破坏不适合您。
编辑:
在您的情况下,我还认为您可能真的不需要销毁signup_up
按钮,因为您尚未创建按钮。以这种方式思考(基于运行代码时出现的GUI)-在应用的登录屏幕(出现的第一个屏幕)上,您有一个登录表单,其中包含两个用于输入电子邮件和密码的输入框和一个登录按钮,以及另外两个名为top_login_button
和top_signup_button
的按钮。因此,截至目前您还没有signup_button
(根据您的代码signup_button = None
,这不是按钮)。因此,如果用户现在单击top_login_button
,则语句signup_button.destroy()
将查找名称为signup_button
(您将其设置为None
)的小部件,从而引发错误。
在用户单击top_signup_button
之前先单击top_login_button
的情况下,您的代码将运行良好,因为在这种情况下,signup_button
实际上将被设置为tk.Button
对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。