Reputation: 123
I am using tkinter to make a status panel that I need to change the color of each label manually. I would like to do this by issuing a command to a console that pops up. I issue a command from the dialog (example: redalert(stat1)
), split it separate the command from the parameter. I could use a separate if statement for each label like this:
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')
but is there a more compact way of doing this? When I try to say param.config(bg='red')
it thinks I am trying to configure the string instead of the value of the string.
Here is the entire code:
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()
Upvotes: 0
Views: 173
Reputation: 76194
I suggest storing all of your stats in a list, instead of having each one in its own variable. Then it's easy to access a particular stat, if you have its index.
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")
(The number extraction technique used in callConsole is just for demonstrative purposes and may not be suitable for your purposes, because it's very tolerant of weird inputs. For example, the user can type "foo 1 redalert 2 troz" and it will make stat 12 light up)
Upvotes: 1