首页 > 解决方案 > 没有可破坏的属性

问题描述

我有两个函数 thetop_signup_clicktop_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 行。提前致谢。

标签: pythonfunctiontkinter

解决方案


我想对此发表评论,但似乎我没有足够的声誉,但这就是我的想法。

您已将变量设置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_buttonNone由您设置)的小部件,因此会引发错误。

在用户单击top_signup_button之前单击的情况下,您的代码将运行良好,top_login_button因为在这种情况下,signup_button实际上将设置为tk.Button对象。


推荐阅读