python:将函数绑定到按钮

时间:2016-02-03 07:28:24

标签: python-3.x tkinter

这是我从PAGE 4.6生成的python代码GUI。

我想创建一个能够实时更改文本框值的函数

tbCapturedImage.set("test 1").

看看

self.btnCapture.bind('<Button-1>', self.Capture_a)

但似乎无法更改文本框的值。

 self.tbCapturedImage = Text(self.TLabelframe1)
        self.tbCapturedImage.place(relx=0.04, rely=0.65, relheight=0.33
                , relwidth=0.93)
        self.tbCapturedImage.configure(background="white")
        self.tbCapturedImage.configure(font="TkTextFont")
        self.tbCapturedImage.configure(selectbackground="#c4c4c4")
        self.tbCapturedImage.configure(width=206)
        self.tbCapturedImage.configure(wrap=WORD)


            self.btnCapture = Button(master)
            self.btnCapture.place(relx=0.01, rely=0.92, height=45, width=982)
            self.btnCapture.configure(activebackground="#d9d9d9")
            self.btnCapture.configure(disabledforeground="#a7a4a7")
            self.btnCapture.configure(font=font11)
            self.btnCapture.configure(text='''Capture Image''')
            self.btnCapture.bind('<Button-1>', self.Capture_a)

            self.Labelframe1 = LabelFrame(master)
            self.Labelframe1.place(relx=0.25, rely=0.48, relheight=0.43
                    , relwidth=0.74)
            self.Labelframe1.configure(relief=GROOVE)
            self.Labelframe1.configure(text='''Color Detection''')
            self.Labelframe1.configure(width=740)        
            self.Labelframe2 = LabelFrame(master)
            self.Labelframe2.place(relx=0.25, rely=0.05, relheight=0.43
                    , relwidth=0.35)
            self.Labelframe2.configure(relief=GROOVE)
            self.Labelframe2.configure(text='''Perspective Transformation''')
            self.Labelframe2.configure(width=350)        
            self.Labelframe3 = LabelFrame(master)
            self.Labelframe3.place(relx=0.61, rely=0.05, relheight=0.43
                    , relwidth=0.38)
            self.Labelframe3.configure(relief=GROOVE)
            self.Labelframe3.configure(text='''Haar-Like Detection''')
            self.Labelframe3.configure(width=380)                
        def Capture():
            tbCapture.Set("test")            
        def Capture_a(self,event):
            self.Capture()   

    if __name__ == '__main__':
        vp_start_gui()

1 个答案:

答案 0 :(得分:2)

您必须使用selfset代替Set

可能您应该使用tbCapturedImage代替tbCapture

 def Capture(self): # self
     self.tbCapturedImage.set("test")  # self, set and tbCapturedImage

BTW:您可以在command=中使用Button代替bind

 self.btnCapture = Button(master, command=self.Capture)

 self.btnCapture.configure(command=self.Capture)

command=没有发送event所以方法不需要参数event