用python制作图像flash

2024-06-16 11:59:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用pack()和pack\u forget()来刷新图像。在

我们正在建造一个定制宝马,灯是用覆盆子皮。我有LED闪光灯使用gpiozero工作,但现在我需要在仪表板上的显示器上显示它。我可以隐藏和显示图像使用Label.pack\u忘记()和标签.pack()分别在一个函数中,但我无法使它闪烁。我尝试了以下代码。在

这是有效的:

def showBG():
    background_label.pack()

def hideBG():
    background_label.pack_forget()

hideBttn = tk.Button(window, text="Hide Arrow", command = hideBG)
showBttn = tk.Button(window, text="Show Arrow", command = showBG)

这不会:

^{pr2}$

第一个示例按预期显示和隐藏箭头:按“隐藏”按钮,它将消失,按“显示”按钮,它就会出现。在

第二个例子应该像你车里仪表盘上的闪光灯一样闪烁3次。打开等待7秒,关闭等待3秒X3。。。在

没有错误,我单击Show Hide按钮,当for循环终止时,箭头就会消失。在


Tags: text图像defbuttonwindow按钮labelpack
2条回答

How to make a flashing text box in tkinter?

for循环将在tkinter小部件上产生效果之前完全执行。由于它最后执行的操作是pack\u forget(),因此不会显示任何内容

不应使用pack()pack_forget()来模拟闪烁,因为如果同一容器中有多个小部件,则标签可能不会放在同一位置。在

另外,使用sleep()将阻止mainloop()处理挂起的事件,这会导致background_label不被更新。在

应更改标签的前景色以模拟闪烁:

创建标签后,首先保存标签的前景色和背景色:

flash_colors = (background_label.cget('bg'), background_label.cget('fg'))
# then flash_colors[0] is label background color
# and flash_colors[1] is label foreground color

然后修改flashBG(),如下所示:

^{pr2}$

flashBG(...)将执行6次(关闭3次,打开3次)。在

相关问题 更多 >