How to pass arguments to a Button command in Tkinter?

前端 未结 18 2772
夕颜
夕颜 2020-11-21 07:42

Suppose I have the following Button made with Tkinter in Python:

import Tkinter as Tk
win = Tk.Toplevel()
frame = Tk.Frame(master=win).grid(row=         


        
相关标签:
18条回答
  • 2020-11-21 08:04

    Just to make the answer of Nae a little bit more elaborate, here is a full blown example which includes the possibility to pass a variable to the callback which contains different values for each button:

    import tkinter as tk
        
    def callback(text):
        print(text)
    
    top = tk.Tk()
    Texts=["text1", "text2", "text3"]
    Buttons=[]
    
    for i, z in enumerate(Texts):
        Buttons.append(tk.Button(top, text=z, command= lambda ztemp=z : callback(ztemp)))
        Buttons[i].pack(side=tk.LEFT, padx=5)
    
    top.mainloop()
    

    By defining a temporary variable ztemp, the value of that variable gets fixed at the moment when the button is defined.

    0 讨论(0)
  • 2020-11-21 08:07

    I personally prefer to use lambdas in such a scenario, because imo it's clearer and simpler and also doesn't force you to write lots of wrapper methods if you don't have control over the called method, but that's certainly a matter of taste.

    That's how you'd do it with a lambda (note there's also some implementation of currying in the functional module, so you can use that too):

    button = Tk.Button(master=frame, text='press', command= lambda: action(someNumber))
    
    0 讨论(0)
  • Python's ability to provide default values for function arguments gives us a way out.

    def fce(x=myX, y=myY):
        myFunction(x,y)
    button = Tk.Button(mainWin, text='press', command=fce)
    

    See: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/extra-args.html

    For more buttons you can create a function which returns a function:

    def fce(myX, myY):
        def wrapper(x=myX, y=myY):
            pass
            pass
            pass
            return x+y
        return wrapper
    
    button1 = Tk.Button(mainWin, text='press 1', command=fce(1,2))
    button2 = Tk.Button(mainWin, text='press 2', command=fce(3,4))
    button3 = Tk.Button(mainWin, text='press 3', command=fce(9,8))
    
    0 讨论(0)
  • 2020-11-21 08:07

    I am extremely late, but here is a very simple way of accomplishing it.

    import tkinter as tk
    def function1(param1, param2):
        print(str(param1) + str(param2))
    
    var1 = "Hello "
    var2 = "World!"
    def function2():
        function1(var1, var2)
    
    root = tk.Tk()
    
    myButton = tk.Button(root, text="Button", command=function2)
    root.mainloop()
    

    You simply wrap the function you want to use in another function and call the second function on the button press.

    0 讨论(0)
  • Lambdas are all well and good, but you can also try this (which works in a for loop btw):

    root = Tk()
    
    dct = {"1": [*args], "2": [*args]}
    def keypress(event):
        *args = dct[event.char]
        for arg in args:
            pass
    for i in range(10):
        root.bind(str(i), keypress)
    

    This works because when the binding is set, a key press passes the event as an argument. You can then call attributes off the event like event.char to get "1" or "UP" ect. If you need an argument or multiple arguments other than the event attributes. just create a dictionary to store them.

    0 讨论(0)
  • 2020-11-21 08:10

    I have encountered this problem before, too. You can just use lambda:

    button = Tk.Button(master=frame, text='press',command=lambda: action(someNumber))
    
    0 讨论(0)
提交回复
热议问题