如何在 tkinter 中删除或销毁标签? [英] How to delete or destroy Label in tkinter?

查看:112
本文介绍了如何在 tkinter 中删除或销毁标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 Tkinter 代码没有小部件,只有一个标签,所以它只在屏幕上显示一个文本,所以我想在一段时间后销毁或删除标签!.当方法 label.after(1000 , label.destroy) 不起作用时,我该怎么做?

This Tkinter code doesn't have a widget, just a label so it displays just a text on the screen so I want to destroy or delete the label after a certain time !. How can I do this when method label.after(1000 , label.destroy) doesn't work?

import tkinter, win32api, win32con, pywintypes

label = tkinter.Label(text='Text on the screen', font=('Times New Roman','80'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+250+250")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")

hWindow = pywintypes.HANDLE(int(label.master.frame(), 16))
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)

label.pack()

label.after(1000 , lambda: label.destroy())   #doesn't work anyway..

label.mainloop()

推荐答案

在您提供的代码中,我相信您正在寻找的解决方案是更改此:

In the code you have provided I believe the fix you are looking for is to change this:

label.after(1000 , lambda: label.destroy())

为此:

label.after(1000, label.master.destroy)

您需要销毁 label.master(我猜这实际上是一个根窗口),因为如果您不这样做,那么您最终会在屏幕上看到一个不透明的大框.

You need to destroy label.master (I am guessing this is actually a root window) because if you do not then you end up with a big box on the screen that is not transparent.

也就是说,我不确定您为什么要以这种方式编写应用程序.我想它可以工作,我实际上并不知道您可以这样做,但我个人仍然会使用根窗口来编写它.

That said I am not sure why you are writing your app in this way. I guess it works and I was not actually aware you could do this but still I personally would write it using a root window to work with.

import tkinter as tk

root = tk.Tk()


label = tk.Label(root, text='Text on the screen',
                 font=('Times New Roman','80'), fg='black', bg='white')
label.pack()

root.overrideredirect(True)
root.geometry("+250+250")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")

root.after(1000, root.destroy)

root.mainloop()

这篇关于如何在 tkinter 中删除或销毁标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆