| | |
tkinter button widget - passing parameter to function
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi all
I've been messing about with this bit of code. Basically what it's
supposed to do is create these six buttons, which when clicked pass a parameter to a function and then call that function. The function basically removes one condition from a larger list depending on which button is clicked The problem is that the way I've written it, the functions runs automatically before the user actually clicks the button.
[code = python]
firstCond = ''
condNames = ['1', '2', '3', '4', '5', '6']
for item in condNames:
button = Button(frame, name = str(item), text = str(item), command = getCond(item)).pack()
def getCond(par):
firstCond = par
conditions.remove(firstCond)
print firstCond
print conditions
[/code]
So I want the function to run when the button is clicked, not automatically on its own. Now I know that the reason it does this is because of the brackets following getCond(item). However, I cannot see any other way of passing the parameter to the function.
Obviously, I could make separate functions for each condition, but I'm just wondering if I could do it more elegantly in this way? It would be a pain if I had 100 conditions instead of 6...
I've been messing about with this bit of code. Basically what it's
supposed to do is create these six buttons, which when clicked pass a parameter to a function and then call that function. The function basically removes one condition from a larger list depending on which button is clicked The problem is that the way I've written it, the functions runs automatically before the user actually clicks the button.
[code = python]
firstCond = ''
condNames = ['1', '2', '3', '4', '5', '6']
for item in condNames:
button = Button(frame, name = str(item), text = str(item), command = getCond(item)).pack()
def getCond(par):
firstCond = par
conditions.remove(firstCond)
print firstCond
print conditions
[/code]
So I want the function to run when the button is clicked, not automatically on its own. Now I know that the reason it does this is because of the brackets following getCond(item). However, I cannot see any other way of passing the parameter to the function.
Obviously, I could make separate functions for each condition, but I'm just wondering if I could do it more elegantly in this way? It would be a pain if I had 100 conditions instead of 6...
Here is an example how to use lambda to pass arguments in Tkinter's button command:
Also, create your function before you call it.
python Syntax (Toggle Plain Text)
import Tkinter as tk def getCond(par): firstCond = par #conditions.remove(firstCond) print firstCond #print conditions root = tk.Tk() firstCond = '' condNames = ['1', '2', '3', '4', '5', '6'] for item in condNames: # use lambda to pass arguments to command-function tk.Button(root, text=str(item), command=lambda i=item: getCond(i)).pack() root.mainloop()
No one died when Clinton lied.
![]() |
Other Threads in the Python Forum
- Previous Thread: pycurl cookies
- Next Thread: Help with fixing "str object is not callable" error
Views: 1476 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy corners curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o iframe images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive screensaverloopinactive scrolledtext server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






