Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Passing values from Tkinter Button to separate function

ChozenChozen Posts: 7Member
Okay, let's say that I have 5 Entry boxes, and the user enters the required info into each. Now, I want to collect that info, and then send it to a function when the user clicks on a Button.

Example:

[code]a_value = Entry(master=frame, width = 15).pack(side=LEFT, anchor=N, padx=2)
a = Entry.get(a_value)

b_value = Entry(master=frame, width = 15).pack(side=LEFT, anchor=N, padx=2)
b = Entry.get(b_value)

etc...

go = Button(master=frame, command=send(a, b, c, d, e))
go.pack(side=LEFT, anchor=N, padx=2)[/code]

But, the function send immediately runs this way, so I thought I could use lambda, making go = :

[code]go = Button(master=frame, command=lamda a, b, c, d, e: send(a, b, c, d, e))
go.pack(side=LEFT, anchor=N, padx=2)[/code]

However, I get the following:
[code]Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python23liblib-tkTkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: () takes exactly 5 arguments (0 given)[/code]

So, I'm guessing lambda won't work (unless I'm doing it wrong ;)). Any ideas on how I can get the values from each entry to be passed to a separate function? I'm sure this is a simple problem, but I just can't figure it out. Any help would be greatly appreciated, thanks.

Comments

  • infidelinfidel Posts: 2,900Member
    : Okay, let's say that I have 5 Entry boxes, and the user enters the required info into each. Now, I want to collect that info, and then send it to a function when the user clicks on a Button.
    :
    : Example:
    :
    : [code]a_value = Entry(master=frame, width = 15).pack(side=LEFT, anchor=N, padx=2)
    : a = Entry.get(a_value)
    [/code]

    It's been a while since I tinkered with Tkinter, so I'll have to do some more research on your questions. One thing that looked odd to me is that you're setting a_value equal to the return value of the pack() method. Is that what you really wanted to do?


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • ChozenChozen Posts: 7Member
    : It's been a while since I tinkered with Tkinter, so I'll have to do some more research on your questions.

    Thanks, I appreciate the help.

    : One thing that looked odd to me is that you're setting a_value equal to the return value of the pack() method. Is that what you really wanted to do?

    Well, thats not what is in my code, I wasn't at my home comp, and typed it all on one line, but it does work that way.


  • infidelinfidel Posts: 2,900Member
    : Okay, let's say that I have 5 Entry boxes, and the user enters the required info into each. Now, I want to collect that info, and then send it to a function when the user clicks on a Button.
    :
    : Example:
    :
    : [code]a_value = Entry(master=frame, width = 15).pack(side=LEFT, anchor=N, padx=2)
    : a = Entry.get(a_value)
    :
    : b_value = Entry(master=frame, width = 15).pack(side=LEFT, anchor=N, padx=2)
    : b = Entry.get(b_value)
    :
    : etc...
    :
    : go = Button(master=frame, command=send(a, b, c, d, e))
    : go.pack(side=LEFT, anchor=N, padx=2)[/code]
    :
    : But, the function send immediately runs this way, so I thought I could use lambda, making go = :
    :
    : [code]go = Button(master=frame, command=lamda a, b, c, d, e: send(a, b, c, d, e))
    : go.pack(side=LEFT, anchor=N, padx=2)[/code]
    :
    : However, I get the following:
    : [code]Exception in Tkinter callback
    : Traceback (most recent call last):
    : File "C:Python23liblib-tkTkinter.py", line 1345, in __call__
    : return self.func(*args)
    : TypeError: () takes exactly 5 arguments (0 given)[/code]
    :
    : So, I'm guessing lambda won't work (unless I'm doing it wrong ;)). Any ideas on how I can get the values from each entry to be passed to a separate function? I'm sure this is a simple problem, but I just can't figure it out. Any help would be greatly appreciated, thanks.

    I haven't spent much time thinking about this, but the solution seems to be to assign your button a command function that doesn't take any arguments. Something like:

    [code]
    def send():
    a = Entry.get(a_value)
    b = Entry.get(b_value)
    # etc...

    go = Button(master=frame, command=send)
    go.pack(side=LEFT, anchor=N, padx=2)
    [/code]

    Maybe that will help?


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • ChozenChozen Posts: 7Member
    : I haven't spent much time thinking about this, but the solution seems to be to assign your button a command function that doesn't take any arguments. Something like:
    :
    : [code]
    : def send():
    : a = Entry.get(a_value)
    : b = Entry.get(b_value)
    : # etc...
    :
    : go = Button(master=frame, command=send)
    : go.pack(side=LEFT, anchor=N, padx=2)
    : [/code]
    :
    : Maybe that will help?

    I thought about doing that, but, The "go" button is part of a toolbar-esque thing, which is its own function. So if I do that, the values won't be able to be attained.
  • infidelinfidel Posts: 2,900Member
    : : I haven't spent much time thinking about this, but the solution seems to be to assign your button a command function that doesn't take any arguments. Something like:
    : :
    : : [code]
    : : def send():
    : : a = Entry.get(a_value)
    : : b = Entry.get(b_value)
    : : # etc...
    : :
    : : go = Button(master=frame, command=send)
    : : go.pack(side=LEFT, anchor=N, padx=2)
    : : [/code]
    : :
    : : Maybe that will help?
    :
    : I thought about doing that, but, The "go" button is part of a toolbar-esque thing, which is its own function. So if I do that, the values won't be able to be attained.

    Then it sounds maybe like you'll need to do a class like this:

    [code]
    class ToolbaresqueThing(Frame):
    def __init__(self, master = None, ???):
    Frame.__init__(self, master)
    self.a_value = Entry(self).pack()
    self.b_value = Entry(self).pack()
    #etc...
    self.go = Button(self, command = self.send)
    def send(self):
    a = Entry.get(self.a_value)
    b = Entry.get(self.b_value)
    #etc...
    [/code]

    It's been a while, but I believe you can subclass Tk Frames to make your own composite controls.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.