[Python][Tkinter] 添加一个标志使标签的前景循环变为红色?

[Python][Tkinter] adding a flag to make the label's foreground turn to red in a loop?

所以我似乎无法弄清楚如何解决我遇到的这个难题。 我有一个简单的脚本,基本上打印出 5 个标签,每个标签打印出 1 个,停顿 2 秒。

我想做的基本上是在 'red' 中突出显示当前 运行 标签,然后当第二个标签打印出来时,第一个标签的前景应该 return 到 'black' 等等。所以基本上最后一个标签应该是红色的,之前的标签应该是黑色的。

基本上,这样做的目的是我有一个用 python 编写的自动化脚本来测试 Android 电视,我想为它制作一个简单的 GUI。我想突出显示当前的测试用例 运行。执行函数(测试用例)后,我希望它变成黑色。

代码如下:

from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
import time

class SampleTkinterLoop:

    def __init__(self, master):
        # Initialize master as the Tk() instance
        self.master = master
        master.title("Loop Tests")
        master.geometry("768x480")

        # Create main frame as app
        self.app = ttk.Frame(root)
        self.app.pack(fill="both", expand=True)

        # Create a custom font
        self.mainFont = tkFont.Font(family="Helvetica", size=12)

        # Initialize flags for BG and FG change
        self.bgCounter = 0

    def test1(self):
        x = True # set fgChooser to True to make foreground red
        ttk.Label(
            self.app, text=f'Test case 1',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont).pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)

    def test2(self):
        x = True # set fgChooser to True to make foreground red
        ttk.Label(
            self.app, text=f'Test case 2',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont).pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)

    def test3(self):
        x = True # set fgChooser to True to make foreground red
        ttk.Label(
            self.app, text=f'Test case 3',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont).pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)

    def test4(self):
        x = True # set fgChooser to True to make foreground red
        ttk.Label(
            self.app, text=f'Test case 4',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont).pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)

    def test5(self):
        x = True # set fgChooser to True to make foreground red
        ttk.Label(
            self.app, text=f'Test case 5',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont).pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)

    def repeatIt(self):
        for i in range(0, 5):
            # self.anotherLoop()
            self.test1()
            self.test2()
            self.test3()
            self.test4()
            self.test5()
            self.reset()
            root.update()
            time.sleep(1)
            print(i)

    def bgChooser(self):
        # make label background alternate to white and gray for easy reading
        if (self.bgCounter % 2) == 0:
            return str("#fff")
        return str("#ccc")

    def fgChooser(self, isActive=False):
        # this is where the  problem is, can't seem to find a way to make a flag for foreground color of label
        if isActive:
            return str("#a5120d")
        return str("#000")

    def reset(self):
        '''reset the UI'''
        for child in self.app.winfo_children():
            child.destroy()

root = Tk()
LoopTest = SampleTkinterLoop(root)
LoopTest.repeatIt()
root.mainloop()

上面的代码使所有标签都变成红色,并没有将之前的标签变成黑色。

您不需要为该任务使用 ttk。这是因为您可以稍后更改标签颜色。
例如:
进口:

from tkinter import *  #<-- so you don't have to edit your full code
import tkinter as ttk     #<---
import tkinter.font as tkFont
import time

您可以像这样编辑 test1 函数:

x = True 
xyz = ttk.Label(   #assign to a variable rather than packing <---
    self.app, text=f'Test case 1',
    background=self.bgChooser(),
    foreground=self.fgChooser(x),
    font=self.mainFont)
xyz.pack()   #pack later  <---
self.bgCounter += 1
x = False 
root.update()
time.sleep(2)
xyz.config(fg="black")   #change color after 2 sec  <---

这是一个示例代码:

from tkinter import *
import tkinter as ttk
import tkinter.font as tkFont
import time

class SampleTkinterLoop:

    def __init__(self, master):
        # Initialize master as the Tk() instance
        self.master = master
        master.title("Loop Tests")
        master.geometry("768x480")

        # Create main frame as app
        self.app = ttk.Frame(root)
        self.app.pack(fill="both", expand=True)

        # Create a custom font
        self.mainFont = tkFont.Font(family="Helvetica", size=12)

        # Initialize flags for BG and FG change
        self.bgCounter = 0

    def test1(self):
        x = True # set fgChooser to True to make foreground red
        xyz = ttk.Label(
            self.app, text=f'Test case 1',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont)
        xyz.pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)
        xyz.config(fg="black")

    def test2(self):
        x = True # set fgChooser to True to make foreground red
        xyz = ttk.Label(
            self.app, text=f'Test case 2',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont)
        xyz.pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)
        xyz.config(fg="black")

    def test3(self):
        x = True # set fgChooser to True to make foreground red
        xyz = ttk.Label(
            self.app, text=f'Test case 3',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont)
        xyz.pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)
        xyz.config(fg="black")

    def test4(self):
        x = True # set fgChooser to True to make foreground red
        xyz = ttk.Label(
            self.app, text=f'Test case 4',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont)
        xyz.pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)
        xyz.config(fg="black")

    def test5(self):
        x = True # set fgChooser to True to make foreground red
        xyz = ttk.Label(
            self.app, text=f'Test case 5',
            background=self.bgChooser(),
            foreground=self.fgChooser(x),
            font=self.mainFont)
        xyz.pack()
        self.bgCounter += 1
        x = False # set it back to false to make foreround black
        root.update()  # allow window to catch up
        time.sleep(2)
        xyz.config(fg="black")

    def repeatIt(self):
        for i in range(0, 5):
            # self.anotherLoop()
            self.test1()
            self.test2()
            self.test3()
            self.test4()
            self.test5()
            self.reset()
            root.update()
            time.sleep(1)
            print(i)

    def bgChooser(self):
        # make label background alternate to white and gray for easy reading
        if (self.bgCounter % 2) == 0:
            return str("#fff")
        return str("#ccc")

    def fgChooser(self, isActive=False):
        # this is where the  problem is, can't seem to find a way to make a flag for foreground color of label
        if isActive:
            return str("#a5120d")
        return str("#000")

    def reset(self):
        '''reset the UI'''
        for child in self.app.winfo_children():
            child.destroy()

root = Tk()
LoopTest = SampleTkinterLoop(root)
LoopTest.repeatIt()
root.mainloop()