python:将函数绑定到按钮 [英] python : bind function to button

查看:96
本文介绍了python:将函数绑定到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从 PAGE 4.6 生成的 Python 代码 GUI.

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

tbCapturedImage.set("test 1").

看看

self.btnCapture.bind('', self.Capture_a)

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

 self.tbCapturedImage = Text(self.TLabelframe1)self.tbCapturedImage.place(relx=0.04, 依赖=0.65, relheight=0.33, 相对宽度=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 = 按钮(主)self.btnCapture.place(relx=0.01, 依赖=0.92, 高度=45, 宽度=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('', self.Capture_a)self.Labelframe1 = LabelFrame(master)self.Labelframe1.place(relx=0.25, 依赖=0.48, relheight=0.43, 相对宽度=0.74)self.Labelframe1.configure(relief=GROOVE)self.Labelframe1.configure(text='''颜色检测''')self.Labelframe1.configure(width=740)self.Labelframe2 = LabelFrame(master)self.Labelframe2.place(relx=0.25, 依赖=0.05, relheight=0.43, 相对宽度=0.35)self.Labelframe2.configure(relief=GROOVE)self.Labelframe2.configure(text='''透视变换''')self.Labelframe2.configure(width=350)self.Labelframe3 = LabelFrame(master)self.Labelframe3.place(relx=0.61, 依赖=0.05, relheight=0.43, 相对宽度=0.38)self.Labelframe3.configure(relief=GROOVE)self.Labelframe3.configure(text='''Haar-Like Detection''')self.Labelframe3.configure(width=380)定义捕获():tbCapture.Set("测试")def Capture_a(self,event):self.Capture()如果 __name__ == '__main__':vp_start_gui()

解决方案

你必须使用 selfset 而不是 Set

也许你应该使用 tbCapturedImage 而不是 tbCapture

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

<小时>

顺便说一句:您可以在 Button 中使用 command= 而不是 bind

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

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

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

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

解决方案

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

这篇关于python:将函数绑定到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆