如何在 tkinter 中制作一个闪烁的文本框? [英] How to make a flashing text box in tkinter?

查看:56
本文介绍了如何在 tkinter 中制作一个闪烁的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的计算课正在用 python 制作一张圣诞贺卡,对于其中一个位,将有一个带有消息的文本框,但是我如何使背景从绿色和红色交替出现?

So my computing class are making a xmas card in python, and for one of the bits there is going to be a text box with a message, but how do I make the background alternate from green and red ?

如果有人能够提供帮助那就太棒了:)

If someone would be able to help that would be amazing :)

from tkinter import *
root = Tk()
root.title("Xmas Message")

#command for the button
def test_com():
    #removing the button
    act_btn.grid_remove() 

#adding the textbox for the message
msg_box = Text(root, height = 1, width = 30)
msg_box.grid(row=0, column=0)

#adding the message
msg_box.insert(END, "Happy Xmas")

#changing the background to green
msg_box.config(background="green")


#changing the background to red
msg_box.config(background="red")

root.after(250, test_com)


#button for activating the command
act_btn = Button(root, text = "1", command = test_com)
act_btn.grid(row=0, column=0)






root.mainloop()

推荐答案

创建一个 change_color 回调来交替文本框的颜色,并使用 after 调用自己一秒钟未来.

Create a change_color callback that alternates the text box's color, and uses after to call itself a second in the future.

示例实现:

from tkinter import *

def change_color():
    current_color = box.cget("background")
    next_color = "green" if current_color == "red" else "red"
    box.config(background=next_color)
    root.after(1000, change_color)

root = Tk()
box = Text(root, background="green")
box.pack()
change_color()
root.mainloop()

这篇关于如何在 tkinter 中制作一个闪烁的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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