繁体   English   中英

Tkinter背景颜色问题

[英]Tkinter background colour issue

我有一个脚本,其中包含Tkinter模块,我想每隔3分钟更改一次背景颜色,例如绿色3分钟,然后橙色,然后红色。 我有显示绿色的代码,但无法更改它。

当我在代码中创建函数时,会遇到一些不同的错误,包括“未定义根目录”,“未定义全局名” root”等。

另外,请注意,在15分钟后关闭Tk显示屏,因此一旦所有3种颜色都消失了。

from __future__ import absolute_import
from . import BasePlugin
import os, sys
import time
from Tkinter import *

def Orange (*args,**kwargs):
    root.config(background="Orange")
def Red(*args,**kwargs):
    root.config(background="Red")

class dis(BasePlugin):
       def execute(self, msg, unit, address, when, printer, print_copies):
        mseg = str('%s - %s' % (msg, unit))
        root = Tk()
        root.title('label')
        txt = Label(root, font= 'times 20 bold', bg='Green')
        txt.config(text= mseg)
        txt.pack(fill=BOTH, expand=0)
        root.after(10,Orange)
        root.after(10,Red)

        root.mainloop(0)

PLUGIN = dis

我也尝试过

from __future__ import absolute_import
from . import BasePlugin
import os, sys
import time
from Tkinter import *

def Orange (*args,**kwargs):
    txt.config(background="Orange")
def Red(*args,**kwargs):
    txt.config(background="Red")

class dis(BasePlugin):
       def execute(self, msg, unit, address, when, printer, print_copies):
        mseg = str('%s - %s' % (msg, unit))
        root = Tk()
        root.title('label')
        txt = Label(root, font= 'times 20 bold', bg='Green')
        txt.config(text= mseg)
        txt.pack(fill=BOTH, expand=0)
        txt.after(10,Orange)
        txt.after(10,Red)

        root.mainloop(0)

PLUGIN = dis

如果我将root = Tk()放在其他任何地方,都会得到一个我不想要的灰色TK小框。

附言:我知道将它设置为10秒只是为了可以对其进行测试

您的代码(至少)存在四个问题,但这很难分辨,因为您没有向我们展示所有细节。 特别是,您似乎从未调用过execute ,但是我假设这可能发生在其他地方,也许通过超类。

  • root是在execute内定义的,因此要在您的回调函数中访问它,您必须使其成为全局变量或dis实例的成员,或者将回调函数置于 execute内
  • 在延迟after以毫秒为单位,因此,使用10的颜色会瞬间切换,这可能不是用于测试的最佳设置
  • 因为它的立场,无论是after回调的确切同一时间执行; 要么将一个放在另一个回调函数的末尾,要么使用不同的时间
  • 您更改了root面板的背景,而实际上您想更改txt标签的背景

例如,您可以尝试这样(最小的独立示例)

class dis:
    def execute(self):
        def orange():
            txt.config(bg="Orange")
            root.after(2000, red)
        def red():
            txt.config(bg="Red")
            root.after(2000, kill)
        def kill():
            root.destroy()
        root = Tk()
        txt = Label(root, text="some text", font='times 20 bold', bg='Green')
        txt.pack(fill=BOTH, expand=0)
        root.after(2000, orange)
        root.mainloop()
dis().execute()

或更短,仅使用一堆lambda

class dis:
    def execute(self):
        root = Tk()
        txt = Label(root, text="some text", font='times 20 bold', bg='Green')
        txt.pack(fill=BOTH, expand=0)
        root.after(2000, lambda: txt.config(bg="Orange"))
        root.after(4000, lambda: txt.config(bg="Red"))
        root.after(6000, root.destroy)
        root.mainloop()
dis().execute()

或使用列表更通用

class dis():
    def __init__(self):
        mseg = ("test message")
        self.color_list=["green", "orange", "red"]
        self.ctr=0
        root = Tk()
        root.title('label')
        self.txt = Label(root, font= 'times 20 bold', width=20)
        self.txt.config(text= mseg)
        self.txt.pack(fill=BOTH, expand=0)
        self.change_color()

        root.mainloop()

    def change_color(self):
        self.txt.config(background=self.color_list[self.ctr])
        self.ctr += 1
        if self.ctr > 2:
           self.ctr=0
        self.txt.after(500, self.change_color)

PLUGIN = dis()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM