如何在按键后删除标签

问题描述 投票:0回答:2

我试图在玩家按任意键后删除标签。但是,Python shell显示错误消息。我该如何解决?

我尝试了label.destroy(),但由于我已经有一个调用此函数的标签,label.destroy()函数不起作用。

from tkinter import *

root = Tk()

def testing(event):
    print("You have pressed a key.")
    root.unbind_all('<Key>')
    label.destroy() # There are two of these.


def countdown(count, label):
    label['text'] = count
    if count > -1:
        root.after(1000, countdown, count-1, label)
    elif count == 0:
        label['text'] = 'Time \nExpired'
    elif count < 0:
        label.destroy() # The second "label.destroy()"

# any_key = root.create_text(250, 400, anchor=CENTER, font=('Calibri', 20), text='Press any key to start.')
# I commented the previous line out because that was my previous code.

any_key = Label(root, anchor=CENTER, font=('Calibri', 20), text='Press any key to start.')
any_key.place(250, 400) # Error 2

root.bind('<Key>', testing)
label = Label(root, anchor=CENTER, font=('Calibri', 48))
label.place(x=50, y=100)
countdown(10, label)

root.bind_all('<Key>', testing)

root.pack()
root.mainloop()

我希望这个程序会删除名为any_key的标签,但在我按下一个键之前它甚至都没有显示出来。此外,Tkinter显示一个显示TypeError: place_configure() takes from 1 to 2 positional arguments but 3 were given的错误,即使我只给出了2个用于放置any_key标签的参数。如果你注释掉any_key.place(),则会出现另一个错误,阅读。 Tkinter回调中的异常

Traceback (most recent call last):
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 749, in callit
    func(*args)
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\Programs\Tests\test 8.py", line 22, in countdown
    label['text'] = count
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1492, in __setitem__
    self.configure({key: value})
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1485, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1476, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!canvas.!label2"

我该如何解决这些错误?

python python-3.x tkinter label runtime-error
2个回答
2
投票

在我在评论中描述的更改后,我得到了工作代码

place需要名字为place(x=..., y=...)的论据

root.pack()不起作用 - root没有方法pack()。你不能把窗口root放在窗口root内。

我也使用count > 0而不是count > -1现在它可以检查count == 0

from tkinter import *

def testing(event):
    print("You have pressed a key.")
    root.unbind_all('<Key>')
    label.destroy() # There are two of these.

def countdown(count, label):
    label['text'] = count
    if count > 0: # not -1
        root.after(1000, countdown, count-1, label)
    elif count == 0:
        label['text'] = 'Time \nExpired'
        # to destroy after 1s
        root.after(1000, countdown, count-1, label)
    elif count < 0:
        label.destroy() # The second "label.destroy()"

root = Tk()

any_key = Label(root, anchor=CENTER, font=('Calibri', 20), text='Press any key to start.')
any_key.place(x=250, y=400) # need x=, y=

label = Label(root, anchor=CENTER, font=('Calibri', 48))
label.place(x=50, y=100)
countdown(10, label)

root.bind_all('<Key>', testing)
#root.pack() # you try to put window `root` inside window `root`

root.mainloop()

因为你可以在testing()结束倒计时之前销毁标签,所以通知countdown小部件不存在可能会很好。我将使用label = None

def testing(event):
    global label

    print("You have pressed a key.")

    root.unbind_all('<Key>')

    if label is not None:
         label.destroy() # There are two of these.
         label = None


def countdown(count, label):
    global label

    if label is not None:
        label['text'] = count
        if count > 0: # not -1
            root.after(1000, countdown, count-1, label)
        elif count == 0:
            label['text'] = 'Time \nExpired'
            # to destroy after 1s
            root.after(1000, countdown, count-1, label)
        elif count < 0:
            label.destroy() # The second "label.destroy()"
            label = None

-1
投票

要解决ERROR 2,你应该这样做:

any_key.place(x=250, y=400) # SOLVE ERROR

我还有一个错误:“root.pack()”因为应该是“label.pack()”

做两次破坏,因为有两行“root.bind('',testing)”和“root.bind_all('',testing)”只保留一行。

控制台中的错误发生是因为两个方法“测试”和“倒计时”是异步的,所以倒计时在测试之前开始并且在他的销毁之后尝试在标签上做某事。所以你需要同步这个方法并共享标签的状态(也用label == null)

from tkinter import *
import threading
import functools

root = Tk()
state = True

def synchronized(wrapped):
    lock = threading.Lock()
    @functools.wraps(wrapped)
    def _wrap(*args, **kwargs):
        print("Calling '%s' with Lock %s" % (wrapped.__name__, id(lock)))
        with lock:
            return wrapped(*args, **kwargs)
    return _wrap

@synchronized
def testing(event):
    global state
    print(event)
    print("You have pressed a key.")
    root.unbind_all('<Key>')
    state = False
    label.destroy() # There are two of these.

@synchronized
def countdown(count, label):
    global state
    print(count)
    print(state)
    if state: 
        label['text'] = count
        if count > 0:
            root.after(1000, countdown, count-1, label)
        elif count == 0:
            label['text'] = 'Time \nExpired'
        elif count < 0:
            label.destroy() # The second "label.destroy()"

# any_key = root.create_text(250, 400, anchor=CENTER, font=('Calibri', 20), text='Press any key to start.')
# I commented the previous line out because that was my previous code.

any_key = Label(root, anchor=CENTER, font=('Calibri', 20), text='Press any key to start.')
any_key.place(x=250, y=400) # SOLVE ERROR

root.bind('<Key>', testing)
label = Label(root, anchor=CENTER, font=('Calibri', 48))
label.place(x=50, y=100)
countdown(10, label)



label.pack() # SOLVE ERROR
root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.