-1

I wonder is there a shorter way to declare a lot of variables at the same time

    self.lal500 = ttk.Label(self.root, text='$500')
    self.ent500 = ttk.Entry(self.root, textvariable=self.int500, width=5, state='readonly')
    self.lal200 = ttk.Label(self.root, text='$200')
    self.ent200 = ttk.Entry(self.root, textvariable=self.int200, width=5, state='readonly')
    self.lal100 = ttk.Label(self.root, text='$100')
    self.ent100 = ttk.Entry(self.root, textvariable=self.int100, width=5, state='readonly')
    self.lal50 = ttk.Label(self.root, text='$50')
    self.ent50 = ttk.Entry(self.root, textvariable=self.int50, width=5, state='readonly')
    self.lal20 = ttk.Label(self.root, text='$20')
    self.ent20 = ttk.Entry(self.root, textvariable=self.int20, width=5, state='readonly')
    self.lal10 = ttk.Label(self.root, text='$10')
    self.ent10 = ttk.Entry(self.root, textvariable=self.int10, width=5, state='readonly')
    self.lal5 = ttk.Label(self.root, text='$5')
    self.ent5 = ttk.Entry(self.root, textvariable=self.int5, width=5, state='readonly')
    self.lal2 = ttk.Label(self.root, text='$2')
    self.ent2 = ttk.Entry(self.root, textvariable=self.int2, width=5, state='readonly')
    self.lal1 = ttk.Label(self.root, text='$1')
    self.ent1 = ttk.Entry(self.root, textvariable=self.int1, width=5, state='readonly')
  • There are several ways, but can you clarify exactly how you plan to use these member variables? I'm unclear on tkinter, but this code snippet is just begging to be replaced by two dictionary member variables (one for `ent`s and one for `lal`s). – BowlingHawk95 Jun 05 '18 at 20:09
  • 2
    https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables relevant – timgeb Jun 05 '18 at 20:09
  • `self.lal = {} ; self.ent = {} ; self.int = {} ; for i in [1,2,5,10,20,50,100,200,500]: self.lal[i] = ttk.Label(self.root, text='${}'.format(i)) ; self.ent[i] = ttk.Entry(self.root, textvariable=self.int[i], width=5, state='readonly')` – Aif Jun 05 '18 at 20:10
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/608639) – jww Jun 05 '18 at 20:42

2 Answers2

0

Use a dictionary to store references to the widgets, then just iterate over a list of values:

self.labels = {}
self.entries = {}
for value in [1, 2, 5, 10, 20, 50, 100, 200, 500]:
    self.labels[value] = entry = tk.Entry(self, width=5, state='readonly')
    self.entries[value] = label = tk.Label(self, text="$%s" % value)

You can get the the value for any entry with something like this: self.entries[100].get()

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

First, I would probably structure this differently, as a dict, i.e. something like

d = {500:
        {'lal': ttk.Label ...
        'ent': ttk.Entry ...
...
}

Then I'd create it with a comprehension or loop, something like this:

for n in 1,2,5,10,20,50, ...:
    d[n] = {'lal': ttk.label.... text='$'+str(n),
            'ent': ttk. ... }

With out knowing more about what self.int500 &c. are, it's hard to fill in the blanks completely.

If it's a regular pattern, you could also iterate through a list comprehension:

for n in [i*f for f in [1,10,100] for i in [1,2,5]]:
    ...

Fundamentally, though, I think all the repetition in your code comes from using a bunch of individual variables rather than a structured data structure like a list or dict.

OldGeeksGuide
  • 2,888
  • 13
  • 23
  • 1
    I think you want to be assigning to `d[n]` not `d['n']` in your second example block. Also the brackets seem misleading in the first block (you probably don't want to be starting a new dict before the `'ent'` key, it's supposed to be in the same dict at `'lal'` on the prvious line). – Blckknght Jun 05 '18 at 20:20