python 控件闪烁_如何在Tkinter中制作一个闪烁的文本框? - python

因此,我的计算机课正在用python制作xmas卡,并且其中之一将是带有消息的文本框,但是如何使背景从绿色和红色交替呢?

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

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()

python大神给出的解决方案

创建一个change_color回调以替换文本框的颜色,并在将来使用after自行调用。

示例实施:

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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值