Ben Yap
Ben Yap

Reputation: 319

python : bind function to button

this is my python code GUI generated from PAGE 4.6.

I want to create a function which will change the textbox value in real time example

tbCapturedImage.set("test 1").

take a look at

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

but it cant seems to change the value of the textbox.

 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()

Upvotes: 0

Views: 135

Answers (1)

furas
furas

Reputation: 142651

You have to use self and set instead of Set

And probably you should use tbCapturedImage instead of tbCapture

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

BTW: you can use command= in Button instead of bind

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

or

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

command= doesn't send event so method doesn't need argument event

Upvotes: 2

Related Questions