本文介绍了使用 Tkinter 绑定传递额外参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个额外的参数 opt 绑定到下面的以下单选按钮.我试图让它在 WSRB_UD 被触发时我可以知道哪个单选按钮触发了它.我该怎么做?

I'm trying to bind an extra argument opt to the following radiobuttons below. I'm trying to make it so that when WSRB_UD is triggered I can know which radiobutton triggered it. How do I go about this?

片段:

        self.WS.SW.SearchFrame = []
        self.WS.SW.SearchRB = []

        self.WS.RBvar  = Tkinter.IntVar()

        i = 0
        while i < 6 :

            Frame = Tkinter.Frame(self.WS.SW.OptFrame, width=125, height=22, bd=1,
                                             bg=self.WSbg)
            Frame.grid(column=0, row=4 + i)
            Frame.grid_propagate(0)

            self.WS.SW.SearchFrame.append(Frame)


            RB = Tkinter.Radiobutton(self.WS.SW.SearchFrame[i], value=i,
                               variable=self.WS.RBvar, indicatoron=0, font=self.WSfo,
                               fg=self.WSfg, activeforeground=self.WSfg, bg=self.WSbg, activebackground=self.WSbg,
                               selectcolor=self.WSbg, bd=self.WSbw)
            RB.grid()
            RB.bind( "<Enter>", self.WSRB_UD, i)
            print i

            self.WS.SW.SearchRB.append(RB)

            i = i + 1

        self.QuickLinkList= []
        self.WS_timer_count = 0

    def WSRB_UD(self, event, opt):
        print self.WS.RBvar.get()

推荐答案

你可以使用 lambda 来定义匿名偏函数:

You can use lambda to define an anonymous partial function:

RB.bind( "<Enter>", lambda event: self.WSRB_UD(event, i) )

如果您不喜欢 lambda 语法,也可以使用 functools.partial.

You could also use functools.partial if you don't like the lambda syntax.

这篇关于使用 Tkinter 绑定传递额外参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:23