How to pass arguments to a Button command in Tkinter?

前端 未结 18 2809
夕颜
夕颜 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: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.

提交回复
热议问题