【发布时间】:2017-07-30 04:28:26
【问题描述】:
正如标题所说,我想在单击按钮时删除它。我尝试了许多不同的样式,这个似乎是最简单的,但我一直收到错误:
line 34, in incorrect
button2.Button.destroy()
NameError: name 'button2' is not defined
当尝试不同的方法时,如下所示:
NameError: name 'button2' is not defined
在开始时尝试定义它时,我会收到此错误:
UnboundLocalError: local variable 'button2' referenced before assignment
任何帮助将不胜感激,谢谢。
我的代码:
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()
【问题讨论】:
标签: python python-3.x tkinter