How to pass arguments to a Button command in Tkinter?

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

    JasonPy - a few things...

    if you stick a button in a loop it will be created over and over and over again... which is probably not what you want. (maybe it is)...

    The reason it always gets the last index is lambda events run when you click them - not when the program starts. I'm not sure 100% what you are doing but maybe try storing the value when it's made then call it later with the lambda button.

    eg: (don't use this code, just an example)

    for entry in stuff_that_is_happening:
        value_store[entry] = stuff_that_is_happening
    

    then you can say....

    button... command: lambda: value_store[1]
    

    hope this helps!

提交回复
热议问题