How to pass arguments to a Button command in Tkinter?

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

    Use lambda

    import tkinter as tk
    
    root = tk.Tk()
    def go(text):
        print(text)
    
    b = tk.Button(root, text="Click", command=lambda: go("hello"))
    b.pack()
    root.mainloop()
    

    output:

    hello
    

提交回复
热议问题