ですから、私が持っているこのジレンマにどのように取り組むかを理解できないようです。基本的に5つのラベルを印刷する簡単なスクリプトがあり、それぞれが2秒の休止で次々に印刷されます。
私がやりたかったのは、基本的に現在実行中のラベルを「赤」で強調表示し、2番目のラベルが印刷されると、1番目のラベルの前景が「黒」に戻るというようになります。したがって、基本的に最後のラベルは赤で、前のラベルは黒である必要があります。
基本的に、これの目的は、Android TVをテストするためにPythonで記述された自動化スクリプトを用意し、そのための簡単な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()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加