【问题标题】:How to make a flashing text box in tkinter?如何在 tkinter 中制作一个闪烁的文本框?
【发布时间】:2014-12-17 19:23:30
【问题描述】:

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

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

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 2.x 或 3.x 中做的吗?使用这两个标签可能会使试图帮助您的人感到困惑。
  • 对不起,我没有意识到 python 2 已经进入标签,我正在使用 python 3,这是我到目前为止的代码
  • 在我原来的帖子中,它现在在那里

标签: python python-3.x textbox tkinter


【解决方案1】:

创建一个交替文本框颜色的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()

【讨论】:

  • 非常感谢,但我能问一个问题吗,为什么要用box.cget而不是box.get,对不起,我还在学习这一切,我只是好奇
  • Text.get 从文本框中获取文本。 Text.cget获取文本框某个属性的值。
  • box.cget("background") == box["background"](和box.config(background=next_color) == box["background"]=next_color,尽管.config 对于在一个语句中设置多个选项很有用)。
  • 将 Kevin 的答案标记为已接受将使阅读问题列表的人知道这是已回答的。
猜你喜欢
  • 2015-02-10
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 2014-01-06
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
相关资源
最近更新 更多