python - 没有可破坏的属性
问题描述
我有两个函数 thetop_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", 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()
top_login_click
函数在第 29-34 行,函数top_signup_click
在第 37-42 行。提前致谢。
解决方案
我想对此发表评论,但似乎我没有足够的声誉,但这就是我的想法。
您已将变量设置signup_button = None
在 import 语句的正下方,因此signup_button
它实际上不是一个tk.Button
对象,而是一个NoneType
变量。
所以只需删除该语句并创建
signup_button = tk.Button(<args here>)
你应该很好。
并尝试使用 place_forget() 而不是 destroy() IFF destroy 对您不起作用。
编辑:
在您的情况下,我还认为您可能不需要销毁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
对象。
推荐阅读
- html - 检测nofollow
- azure - HTTP 错误 500.30 Azure.RequestFailedException
- wpf - 特殊附加属性绑定有效,但不适用于 DataTemplate
- pine-script - 将策略限价买入(或卖出)转换为警报
- php - Laravel 7 eloquent nested conditions for filtering with pivot
- excel - 范围 RemoveDuplicates 无效的过程调用或参数
- oracle - 即使在 oracle 12c 中使用 DBMS_REDACT 成功添加策略后,也无法看到被屏蔽的数据
- react-native - 提供给 SecureStore 的密钥无效
- python - 使用烧瓶python将图像从数据库加载到网页
- python - 等待多个反应 Discord.py