How to pass arguments to a Button command in Tkinter?

前端 未结 18 2782
夕颜
夕颜 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 07:51

    For posterity: you can also use classes to achieve something similar. For instance:

    class Function_Wrapper():
        def __init__(self, x, y, z):
            self.x, self.y, self.z = x, y, z
        def func(self):
            return self.x + self.y + self.z # execute function
    

    Button can then be simply created by:

    instance1 = Function_Wrapper(x, y, z)
    button1  = Button(master, text = "press", command = instance1.func)
    

    This approach also allows you to change the function arguments by i.e. setting instance1.x = 3.

提交回复
热议问题