我正在使用tkinter创建一个状态面板,我需要手动更改每个标签的颜色。我想通过向弹出的控制台发出命令来做到这一点。我从对话框中发出命令(例如:redalert(stat1)
),将命令与参数分开。我可以为每个标签使用单独的if语句,如下所示:
if 'redalert' in command:
param = command.split('(')[1]
param = 'self.' + param.split(')')[0]
if param == 'self.stat1':
self.stat1.config(bg='red')
elif param == 'self.stat2':
self.stat2.config(bg='red')
但有更紧凑的方式吗?当我试着说param.config(bg='red')
时,它认为我正在尝试配置字符串而不是字符串的值。
以下是整个代码:
from Tkinter import *
class App:
def __init__(self, master):
self.stat1 = Label(text="Stat 1", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat2 = Label(text="Stat 2", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat3 = Label(text="Stat 3", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat4 = Label(text="Stat 4", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat5 = Label(text="Stat 5", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat6 = Label(text="Stat 6", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat7 = Label(text="Stat 7", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat8 = Label(text="Stat 8", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat9 = Label(text="Stat 9", bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
self.stat10 = Label(text="Stat 10", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat11 = Label(text="Stat 11", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat12 = Label(text="Stat 12", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat13 = Label(text="Stat 13", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat14 = Label(text="Stat 14", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat15 = Label(text="Stat 15", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat16 = Label(text="Stat 16", bg='#990000', bd=3, relief=SOLID, padx=26, pady=20)
self.stat1.grid(row=0, column=0)
self.stat2.grid(row=0, column=1)
self.stat3.grid(row=0, column=2)
self.stat4.grid(row=0, column=3)
self.stat5.grid(row=1, column=0)
self.stat6.grid(row=1, column=1)
self.stat7.grid(row=1, column=2)
self.stat8.grid(row=1, column=3)
self.stat9.grid(row=2, column=0)
self.stat10.grid(row=2, column=1)
self.stat11.grid(row=2, column=2)
self.stat12.grid(row=2, column=3)
self.stat13.grid(row=3, column=0)
self.stat14.grid(row=3, column=1)
self.stat15.grid(row=3, column=2)
self.stat16.grid(row=3, column=3)
root.bind("<F1>", self.callConsole)
def callConsole(self, master):
self.commandConsole = Console(root)
root.wait_window(self.commandConsole.top)
command = self.commandConsole.commandVar
if 'redalert' in command:
param = command.split('(')[1]
param = 'self.' + param.split(')')[0]
if param == 'self.stat1':
self.stat1.config(bg='red')
elif param == 'self.stat2':
self.stat2.config(bg='red')
class Console:
def __init__(self, master):
self.top = Toplevel()
self.top.transient(root)
self.command = Entry(self.top, font=("Helvetica", 15))
self.command.grid(row=0, column=0)
self.command.focus_set()
self.top.bind("<Return>", self.execute)
def execute(self, master):
self.commandVar = self.command.get()
self.top.destroy()
root = Tk()
app = App(root)
root.mainloop()
root.destroy()
答案 0 :(得分:1)
我建议将所有统计信息存储在列表中,而不是将每个统计信息存储在自己的变量中。如果您有索引,那么很容易访问特定的统计数据。
class App:
def __init__(self, master):
self.stats = []
idx = 0
for i in range(4):
for j in range(4):
stat = Label(text="Stat {}".format(idx+1), bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
stat.grid(row=i, column=j)
self.stats.append(stat)
idx += 1
root.bind("<F1>", self.callConsole)
def callConsole(self, master):
self.commandConsole = Console(root)
root.wait_window(self.commandConsole.top)
command = self.commandConsole.commandVar
if 'redalert' in command:
digits = [c for c in command if c.isdigit()]
number = int("".join(digits))
self.stats[number-1].config(bg="red")
(callConsole中使用的数字提取技术仅用于演示目的,可能不适合您的目的,因为它非常容忍奇怪的输入。例如,用户可以键入&#34; foo 1 redalert 2 troz&#34;它将使12号亮起来)