1

How do I pass parameters to a function through a button?

variable = str()

def RandomFunction(variable): 
    print (variable) 

EntryBox = Entry(MainWindow, textvariable = variable).pack() 
FunctionCall = Button(MainWindow, text="Enter", command=RandomFunction(variable)) 

It seems like it just doesnt print anything when the button is pressed. I've searched around and it seems that using lambda can fix it and allow (variable) to be passed to the function but after experimenting with lambda variable:variable I still can't get it to work.

4
  • What is variable? It isn't defined in that code. Commented Mar 28, 2014 at 21:02
  • string, initialized it at the start Commented Mar 28, 2014 at 21:09
  • How about my answer? I don't think you were using lambda correctly. Commented Mar 28, 2014 at 21:13
  • Also, what is the significance of the Entry widget here? Commented Mar 28, 2014 at 21:14

3 Answers 3

1

The other answers here work, but like a lot of things in life, there's more than one way to do what you're trying to do.

The code in your question actually mixes a couple of methods of getting data from the Entry widget. You're using textvariable and lambda, but you only need one. It seems like lambda has been covered, so here's a quick answer about textvariable:

First, you need to make your variable of a Tkinter string type like this:

variable = StringVar()

Your entry widget is fine, it's connected to the StringVar(). Your button doesn't need lambda, though, because you don't need to pass an argument to your RandomFunction().

FunctionCall = Button(MainWindow, text='Enter', command=RandomFunction).pack()

Lastly, your function needs a little rework, because it's not taking an argument anymore, it's just going to use the .get() method on your StringVar() whenever it's called:

def RandomFunction():
    print(variable.get())

You can read more about StringVar()s here: http://effbot.org/tkinterbook/variable.htm

Sign up to request clarification or add additional context in comments.

6 Comments

I've also tried that method, i get the error message: Attribute error: 'str' object has no attribute 'get'.
Try removing .get() then. Sometimes some simple debugging will be required.
but then i can't access the variable within the function
How so? Is this your exact code or a facsimile? You've been given three answers that should work with the code provided, but I think there must be something up with your code if nothing is working
That was it, thank you! Had just assigned it to a string, not a StringVar.
|
1

You use .get() to get the contents of an Entry. From the effbot page http://effbot.org/tkinterbook/entry.htm

from Tkinter import *

master = Tk()

e = Entry(master)
e.pack()
e.focus_set()

def callback():
    print e.get()

b = Button(master, text="get", width=10, command=callback)
b.pack()

master.mainloop()

Comments

1

Using lambda to create a function that calls the function with the argument is fine (as long as you do it correctly):

FunctionCall = Button(MainWindow, text="Enter", command=lambda: RandomFunction(EntryBox.get))

Python will be happy with this because the lambda doesn't take any arguments.

5 Comments

I've tried that and while it doesn't give me any errors it still seems like what it is printing is blank?
Well, of course it doesn't, nothing comes up as you didn't set your string an actual value. Set variable to something like 'Hello, world!' and see what happens.
The entry widget is what allows you to enter text, this is then assigned to the variable. I've entered text into the entry box and it still isn't printed when the button is pressed.
That's because you never extract the data from the Entry. See edits.
How would i go about extracting the data? in previous programs it has worked without any specific way of extracting the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.