闪烁的Tkinter标签 [英] Flashing Tkinter Labels

查看:228
本文介绍了闪烁的Tkinter标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,尽管遇到了无法解决的问题,但最近开始使用tkinter.

I'm a beginner programmer in python and have recently began using tkinter though I have come across a problem which I can't solve.

基本上我有两个输入框.

Basically I have two entry boxes.

  • Entry1 =消息
  • 条目2 =否.闪烁

(这只是我所需要的一个示例.)

(This is just an example of what I need.)

我需要的是for循环,标签弹出并刷新entry1的次数与entry2相同,是的,我知道如何获取输入项,但是我不知道如何使标签连续闪烁,我尝试在循环中为标签尝试使用pack_forget.destroy方法,但是不幸的是它无法显示,因为它几乎立即又将其从屏幕上清除了.

All I need is a for loop for a label to pop up and flash entry1 as many times as entry2, yes I realize how to get the entry inputs but I have no idea how to get the label to continuously flash, I have tried pack_forget and .destroy methods for the label in a loop, but unfortunately it does not display as it almost instantly clears it from the screen again.

推荐答案

基本思想是创建一个执行Flash(或Flash的一半)的函数,然后使用after重复为as调用该函数.只要您希望闪光发生.

The basic idea is to create a function that does the flash (or half of a flash), and then use after to repeatedly call the function for as long as you want the flash to occur.

这是一个切换背景和前景色的示例.它永远运行,只是因为我想使示例简短.您可以轻松添加计数器,停止按钮或其他任何所需的内容.要摆脱的问题是具有一个功能,该功能执行动画的一帧(在这种情况下为切换颜色),然后安排自己在一段时间后再次运行.

Here's an example that switches the background and foreground colors. It runs forever, simply because I wanted to keep the example short. You can easily add a counter, or a stop button, or anything else you want. The thing to take away from this is the concept of having a function that does one frame of an animation (in this case, switching colors), and then scheduling itself to run again after some amount of time.

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.label = tk.Label(self, text="Hello, world", 
                              background="black", foreground="white")
        self.label.pack(side="top", fill="both", expand=True)
        self.flash()

    def flash(self):
        bg = self.label.cget("background")
        fg = self.label.cget("foreground")
        self.label.configure(background=fg, foreground=bg)
        self.after(250, self.flash)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

这篇关于闪烁的Tkinter标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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