How to pass arguments to a Button command in Tkinter?

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

    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.

提交回复
热议问题