[Tutor] Shortening the code
ALAN GAULD
alan.gauld at btinternet.com
Sat Nov 26 13:34:40 CET 2011
> > and text in your data list and configure each button directly:
> Is there any difference between a list and a data list?
Not really, but a list can handle any kind of data, even functions,
objects etc (but they are all types of data too)
I was just being specific that I meant the list of data used to
configure your widgets. I should probably just have used its name!
###################
import tkinter as tk
from functools import partial
def button_clicked(button):
if button["bg"] == "green":
button.configure(bg="red")
else:
button.configure(bg="green")
class Window(tk.Frame):
def __init__(self, master):
super (Window, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
list_chair=[(0, 0, '01'), (0, 1, '02'),
(0, 3, '03'), (0, 4, '04'),
(1, 0, '05')]
for row, column, name in list_chair:
command = partial(button_clicked, button)
button = tk.Button(self, color='green',
command=command, text=name)
button.grid(row=row, column=column)
root = tk.Tk()
root.title("Test")
root.geometry("200x200")
app = Window(root)
root.mainloop()
######################
> However, nothing happens when I run the program.
Really? I get an error:
Traceback (most recent call last):
File "/home/alang/src/Python/chairs.py", line 32, in <module>
app = Window(root)
File "/home/alang/src/Python/chairs.py", line 14, in __init__
self.create_widgets()
File "/home/alang/src/Python/chairs.py", line 21, in create_widgets
command = partial(button_clicked, button)
UnboundLocalError: local variable 'button' referenced before assignment
Which correctly reports that you are using button before it is created.
There is also a bug in that the color parameter should, of course,
be bg...
When I change create_widgets() to:
def create_widgets(self):
list_chair=[(0, 0, '01'), (0, 1, '02'),
(0, 3, '03'), (0, 4, '04'),
(1, 0, '05')]
for row, column, name in list_chair:
button = tk.Button(self, bg='green',
text=name)
button['command'] = partial(button_clicked, button)
button.grid(row=row, column=column)
It works as expected
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111126/8e909f81/attachment-0001.html>
More information about the Tutor
mailing list