首页 文章

反复将Tkinter窗口颜色更改为生成的颜色

提问于
浏览
0

我有Python代码生成屏幕的平均颜色作为RGB值 and 十六进制代码 . 代码通过 while True 循环重复自身,我想在此循环结束时将指令更改为窗口颜色 .

我现在有这个代码:

from Tkinter import *
from colour import Color

root = Tk()
root.configure(background="grey")
root.geometry("400x400")
root.mainloop()
while True:
    [ COLOUR GENERATING SCRIPT ]
    hexcolour = Color(rgb=(red, green, blue))
    root.configure(background=hexcolour)

有人可以告诉我如何启动Tkinter窗口,然后每次循环运行时更改颜色?

我正在为这个项目运行Python 2.7 .

1 回答

  • 2

    您需要完全删除 while 循环 . 相反,创建一个你将在循环中执行的函数,然后让该函数通过 after 调用自身 . 然后它将运行程序的生命周期 .

    from Tkinter import *
    from colour import Color
    
    def changeColor():
        [ COLOUR GENERATING SCRIPT ]
        hexcolour = Color(rgb=(red, green, blue))
        root.configure(background=hexcolour)
    
        # call this function again in one second
        root.after(1000, changeColor)
    
    root = Tk()
    root.configure(background="grey")
    root.geometry("400x400")
    
    # call it once, it will run forever
    changeColor()
    
    root.mainloop()
    

相关问题