【问题标题】:How to setup a Tkinter button to erase once clicked?如何设置 Tkinter 按钮以在单击后擦除?
【发布时间】: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


    【解决方案1】:

    将您的button-变量存储在一个类中,或者将它们传递给您的函数。您遇到问题是因为您的 button2 超出范围!

    试试这个:

       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 username != self.username: 是多余的,因为前面的条件表达式与其完全相反——在这种情况下,else: 就是所需要的。看来这只是常识……
    • @martineau,让我们把它留给 OP 的良心吧。感谢您的讽刺和明显的补充。 (:
    • 是的,我认为 martineau 是正确的。我只是想让它尽可能具体,以消除任何潜在的错误。非常感谢@CommonSense。我对此仍然很陌生,所以这真的很有帮助。对此,我真的非常感激。顺便说一句,您知道如何将回车键连接到初始提交按钮。我找到的所有答案似乎都不起作用。 self.button = Button(self.root, text="SUBMIT", highlightbackground="green", width=48, command=self.correct) self.button.pack() self.button.bind("" , command=self.correct) 你认为最好的方法是什么?
    • @gmonz ,尝试在没有“命令”键的情况下进行绑定,这将起作用。我建议绑定到您的条目小部件!
    • @CommonSense,我试过 self.button.bind("", self.correct), self.button.bind_all("", self.correct), self.entry .bind("", self.correct), self.entry.bind_all("", self.correct)。除了 bind_all 想法之外,我还尝试连接到 self.root 和 self.init 和 self.__init__ 。还有其他想法吗?我的想法出现了这个错误:Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py",第 1699 行,在 call return self.func(*args) TypeError : correct() 接受 1 个位置参数,但给出了 2 个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多