R
robbie
Hello Group,
I'm a programmer with many years experience, but am new to Python and
to object oriented programming. I'm working my way through a book on
Python, and have been tweaking the examples to see what they do. I've
run into a situation that I don't understand, though I expect it's
pretty simple for anyone more skilled than I am. Here's a snippet of
code:
This first example works. It creates three windows with a button in
each that prints a word when I press the button:
from Tkinter import *
root = Tk()
trees = [('The Larch!', 'light blue'),
('The Pine!', 'light green'),
('The Giant Redwood!', 'red')]
def salutation():
print "Something!"
for (tree, color) in trees:
win = Toplevel(root)
win.title('Sing...')
win.protocol('WM_DELETE_WINDOW', lambda:0)
win.iconbitmap('py-blue-trans-out.ico')
msg = Button(win, text='Write Something', command=salutation)
msg.pack(expand=YES, fill=BOTH)
msg.config(fg='black', bg=color, font=('times', 30, 'bold
italic'))
root.title('Lumberjack demo')
Label(root, text='Main window', width=30).pack()
Button(root, text='Quit All', command=root.quit).pack()
win.mainloop()
This second example doesn't work. The only difference is that I gave
the salutation function a parameter and tried to pass a string as the
parameter from the button. It appears to call the function once for
each window as they're being created, but doesn't do anything when the
buttons in the windows are pressed.
from Tkinter import *
root = Tk()
trees = [('The Larch!', 'light blue'),
('The Pine!', 'light green'),
('The Giant Redwood!', 'red')]
def salutation(j=""):
print j + " Something!"
for (tree, color) in trees:
win = Toplevel(root)
win.title('Sing...')
win.protocol('WM_DELETE_WINDOW', lambda:0)
win.iconbitmap('py-blue-trans-out.ico')
msg = Button(win, text='Write Something',
command=salutation(tree))
msg.pack(expand=YES, fill=BOTH)
msg.config(fg='black', bg=color, font=('times', 30, 'bold
italic'))
I figure there's something very basic I'm missing about passing a
parameter from a Tkinter widget to a function. Can anyone tell me what
I'm doing wrong?
Thank you so much!
Robbie in Montana
I'm a programmer with many years experience, but am new to Python and
to object oriented programming. I'm working my way through a book on
Python, and have been tweaking the examples to see what they do. I've
run into a situation that I don't understand, though I expect it's
pretty simple for anyone more skilled than I am. Here's a snippet of
code:
This first example works. It creates three windows with a button in
each that prints a word when I press the button:
from Tkinter import *
root = Tk()
trees = [('The Larch!', 'light blue'),
('The Pine!', 'light green'),
('The Giant Redwood!', 'red')]
def salutation():
print "Something!"
for (tree, color) in trees:
win = Toplevel(root)
win.title('Sing...')
win.protocol('WM_DELETE_WINDOW', lambda:0)
win.iconbitmap('py-blue-trans-out.ico')
msg = Button(win, text='Write Something', command=salutation)
msg.pack(expand=YES, fill=BOTH)
msg.config(fg='black', bg=color, font=('times', 30, 'bold
italic'))
root.title('Lumberjack demo')
Label(root, text='Main window', width=30).pack()
Button(root, text='Quit All', command=root.quit).pack()
win.mainloop()
This second example doesn't work. The only difference is that I gave
the salutation function a parameter and tried to pass a string as the
parameter from the button. It appears to call the function once for
each window as they're being created, but doesn't do anything when the
buttons in the windows are pressed.
from Tkinter import *
root = Tk()
trees = [('The Larch!', 'light blue'),
('The Pine!', 'light green'),
('The Giant Redwood!', 'red')]
def salutation(j=""):
print j + " Something!"
for (tree, color) in trees:
win = Toplevel(root)
win.title('Sing...')
win.protocol('WM_DELETE_WINDOW', lambda:0)
win.iconbitmap('py-blue-trans-out.ico')
msg = Button(win, text='Write Something',
command=salutation(tree))
msg.pack(expand=YES, fill=BOTH)
msg.config(fg='black', bg=color, font=('times', 30, 'bold
italic'))
I figure there's something very basic I'm missing about passing a
parameter from a Tkinter widget to a function. Can anyone tell me what
I'm doing wrong?
Thank you so much!
Robbie in Montana