python - [Python][Tkinter]添加一个标志使标签的前景在循环中变成红色?

标签 python python-3.x tkinter flags

所以我似乎不知道如何解决我所面临的这个困境。 我有一个简单的脚本,基本上打印出 5 个标签,每个标签依次打印出 1 个标签,并暂停 2 秒。

我想做的基本上是以“红色”突出显示当前正在运行的标签,然后当第二个标签打印出来时,第一个标签的前景应返回到“黑色”,依此类推。所以基本上最后一个标签应该是红色的,前面的标签应该是黑色的。

基本上,这样做的目的是我有一个用 python 编写的自动化脚本来测试 Android TV,并且我想为其制作一个简单的 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()

关于python - [Python][Tkinter]添加一个标志使标签的前景在循环中变成红色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488650/

相关文章:

python - 下标 itertools.repeat

python-3.x - 如果Python列表是作为动态数组实现的,为什么追加操作后其id没有改变?

python - 为什么这个不断出现在两个单独的窗口中? (Python)

python-2.7 - 如何更改 Tkinter GUI 屏幕的 Logo

python - 新窗口格式

python Pandas : Check if all columns in rows value is NaN

python - “元组”对象在更新图像对象时没有属性 '_committed' 错误?

python - 为什么 pytz localize() 不生成一个 datetime 对象,其 tzinfo 与本地化它的 tz 对象相匹配?

python - 将多索引与 Pandas 中的单索引数据框合并

python - 将张量作为数组进行迭代 Tensorflow