【问题标题】:no attribute to destroy没有可破坏的属性
【发布时间】:2020-08-28 04:40:36
【问题描述】:

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

【问题讨论】:

  • 我认为您将global 混为一谈。在top_signup_click() 里面试着说global signup_buttontop_login_click()global login_button,请告诉我

标签: python function tkinter


【解决方案1】:

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

您在导入语句的正下方设置了变量signup_button = None,因此signup_button 并不是真正的tk.Button 对象,而是NoneType 变量。

所以只需删除该语句并创建

signup_button = tk.Button(<args here>)

你应该很好。

并尝试使用 place_forget() 而不是 destroy() IFF destroy 对您不起作用。

编辑: 在您的情况下,我还认为您可能不需要销毁 signup_up 按钮,因为您还没有创建一个。以这种方式思考(基于运行代码时出现的 GUI) - 在应用程序的登录屏幕(出现的第一个屏幕)上,您有一个登录表单,其中包含两个用于电子邮件和密码的输入框和一个登录按钮,以及另外两个名为 top_login_buttontop_signup_button 的按钮。所以你现在还没有signup_button(根据你的代码signup_button = None,它不是一个按钮)。因此,如果用户现在单击top_login_button,则语句signup_button.destroy() 将查找名称为signup_button(您设置为None)的小部件,因此会引发错误。

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

【讨论】:

  • 现在你有足够的声望了 ;) 虽然我不认为这可能是正确的原因
  • @CoolCloud hehe 谢谢 :) 如果您查看所有函数定义之后的代码,没有名称为“signup_button”的小部件将在函数调用中被销毁。 OP 实际上可能不需要“signup_button”(我是根据运行他的代码时出现的 GUI 说的)。最后,我通过上述更改使其工作。
  • @ChociiMilk 只需将它们再次放在 GUI 上。比如 signup_button.place(x=10, y=10)。 place_forget() 方法只是从 GUI 中删除(或隐藏)小部件。小部件仍在程序内存中,因此您甚至不需要再次创建小部件。 {请记住使用全局版本的 signup_button 以避免 unboundLocalError - 就像您在其他小部件的其他函数中所做的那样。}
  • @ChociiMilk 如果有帮助,请标记为正确答案,以便关闭此Q
猜你喜欢
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多