Python 如何设置Tkinter按钮以在单击后擦除?
正如标题所说,我想在单击按钮时将其删除。我尝试过许多不同的风格,这一款似乎是最简单的,但我不断地犯错误:Python 如何设置Tkinter按钮以在单击后擦除?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,正如标题所说,我想在单击按钮时将其删除。我尝试过许多不同的风格,这一款似乎是最简单的,但我不断地犯错误: 第34行,格式不正确 button2.Button.destroy() NameError:未定义名称“button2” 当尝试下面所示的其他方法时,此方法: name错误:未定义名称“button2” 在开始尝试定义时,我会收到以下错误: UnboundLocalError:赋值前引用了局部变量'button2' 任何帮助都将不胜感激,谢谢 我的代码: from tkinter im
第34行,格式不正确
button2.Button.destroy()
NameError:未定义名称“button2”
当尝试下面所示的其他方法时,此方法:
name错误:未定义名称“button2”
在开始尝试定义时,我会收到以下错误:
UnboundLocalError:赋值前引用了局部变量'button2'
任何帮助都将不胜感激,谢谢
我的代码:
from tkinter import *
class Application(object):
def __init__(self):
self.root = Tk()
self.root.configure(bg="darkorchid1", padx=10, pady=10)
self.root.title("WELCOME TO THIS PROGRAM)")
self.username = "Bob"
program = Label(self.root, text="MY PROGRAM", bg="lightgrey", fg="darkorchid1")
program.pack()
label0 = Label(self.root, text="ENTER USERNAME:", bg="purple", fg="white", height=5, width=50)
label0.pack()
self.entry = Entry(self.root, width=25)
self.entry.configure(fg= "white",bg="grey20")
self.entry.pack()
button = Button(self.root, text="SUBMIT", highlightbackground="green", width=48, command=self.correct)
button.pack()
def correct(self):
username = self.entry.get()
if username == self.username:
button1 = Button(self.root, text='LOGIN', highlightbackground="green", width=28, command=self.root.destroy)
button1.pack()
elif username != self.username:
button2 = Button(self.root, text="INCORRECT- CLICK TO DIMISS THIS MESSAGE", highlightbackground="red", width=48, command=self.incorrect)
button2.pack()
def incorrect(self):
button2.destroy()
app=Application()
mainloop()
将
按钮
-变量存储在类中,或者将它们传递给函数。您的按钮2
超出了范围,因此出现了问题
请尝试以下方法:
def correct(self):
username = self.entry.get()
if username == self.username:
self.button1 = Button(self.root, text='LOGIN', highlightbackground="green",
width=28, command=self.root.destroy)
self.button1.pack()
elif username != self.username:
self.button2 = Button(self.root,
text="INCORRECT- CLICK TO DIMISS THIS MESSAGE",
highlightbackground="red", width=48,
command=self.incorrect)
self.button2.pack()
def incorrect(self):
self.button2.destroy()
将
按钮
-变量存储在类中,或者将它们传递给函数。您的按钮2
超出了范围,因此出现了问题
请尝试以下方法:
def correct(self):
username = self.entry.get()
if username == self.username:
self.button1 = Button(self.root, text='LOGIN', highlightbackground="green",
width=28, command=self.root.destroy)
self.button1.pack()
elif username != self.username:
self.button2 = Button(self.root,
text="INCORRECT- CLICK TO DIMISS THIS MESSAGE",
highlightbackground="red", width=48,
command=self.incorrect)
self.button2.pack()
def incorrect(self):
self.button2.destroy()
elif用户名!=self.username:
是多余的,因为前面的条件表达式正好相反,在这种情况下,else:
就是所需的全部内容。看起来这只是常识…@martineau,让我们让OP的良心来决定吧。谢谢你的讽刺和明显的补充。(:是的,我认为martineau是正确的。我只是想把它尽可能具体化,以消除任何潜在的错误。非常感谢@CommonSense。我对这方面还是很陌生,所以这非常有帮助。我非常感谢。顺便问一下,你知道如何将enter键连接到初始提交按钮。我找到的所有答案都可以似乎不起作用。self.button=button(self.root,text=“SUBMIT”,highlightbackground=“green”,width=48,command=self.correct)self.button.pack()self.button.bind(“,command=self.correct”)你认为什么最好?@gmonz,尝试在没有“command”的情况下绑定我建议绑定到你的入口小部件!@CommonSense,我试过self.button.bind(“,self.correct”),self.button.bind\u all(“,self.correct),self.entry.bind(“,self.correct”),self.entry.bind\u all(“,self.correct)。我尝试连接到self.root和self.init以及self。uuu init_uuuu以及bind_uall的想法。还有其他想法吗?我从你的想法中得到了这个错误:Python.framework/Versions/3.6/lib/python3.6/tkinter/u init_uuu.py”,第1699行,在call return self.func(*args)TypeError:correct()接受1个位置参数,但2个被赋予elif username!=self。username:
是多余的,因为前面的条件表达式正好相反,在这种情况下,else:
就是所需的全部。似乎这只是常识…@martineau,让我们把它留给OP的良心。谢谢讽刺和明显的补充激动。(:是的,我认为martineau是正确的。我只是想把它尽可能具体化,以消除任何潜在的错误。非常感谢@CommonSense。我对这方面还是很陌生,所以这非常有帮助。我非常感谢。顺便问一下,你知道如何将enter键连接到初始提交按钮。我找到的所有答案都可以似乎不起作用。self.button=button(self.root,text=“SUBMIT”,highlightbackground=“green”,width=48,command=self.correct)self.button.pack()self.button.bind(“”,command=self.correct)你认为什么最好?@gmonz,尝试不使用“command”绑定“键,这会起作用的。我建议绑定到您的条目小部件!@CommonSense,我尝试了self.button.bind(”,self.correct),self.button.bind_all(“,self.correct),self.entry.bind(“,self.correct),self.entry.bind_all(”,self.correct)。我尝试连接到self.root和self.init以及self。uuu init_uuu以及bind_uall的想法。还有其他想法吗?我从你的想法中得到了这个错误:Python.framework/Versions/3.6/lib/python3.6/tkinter/u init_uu.py”,第1699行,在调用返回self.func(*args)TypeError:correct()接受1个位置参数,但给出了2个