-1

I have a label and I want to do some actions on hover and click but the below code is not working.

class LoginForm:
    def __init__(self,root):    
        Label(text='Don\'t have an account? Create one!', font='Arial 10').place(relx=0.5, rely=0.8, anchor=CENTER)
        Label.bind( "<Button>",self.mouseClick)
    def mouseClick(self,event):
        print('x')

And I got the next error AttributeError: 'str' object has no attribute '_bind' Does anyone know how to fix this problem?

Viorel
  • 39
  • 7

1 Answers1

1

Your code might be:

class LoginForm:
    def __init__(self,root):    
        exmpleText_widget = Label(text='Don\'t have an account? Create one!', font='Arial 10') # assgin it to a variable 
        exmpleText_widget.place(relx=0.5, rely=0.8, anchor=CENTER)
        # exmpleText_widget.bind( "<Button>",self.mouseClick) # This is mouse button event,All the mouse button pressed on this widget will call the function.
        exmpleText_widget.bind("<Enter>",self.mouseClick) # This can be an easy mouse hover event
        exmpleText_widget.bind("<Button-1>",function) # mouse left button pressed on this widget will call the function.
        # exmpleText_widget.bind("<Leave>",function) # this will call the function when mouse leave this widget.
    def mouseClick(self,event):
        print('x')
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • It shows up this error using your code `AttributeError:'NoneType' object has no attribute 'bind' ` Do I have to import something in my projects for `binds`? – Viorel Apr 18 '20 at 10:02
  • It is working now. I just had to split `Label(text='Don\'t have an account? Create one!', font='Arial 10').place(relx=0.5, rely=0.8, anchor=CENTER)` in two lines – Viorel Apr 18 '20 at 10:05
  • @Viorel No,You should use `var = Label(text='Don\'t have an account? Create one!', font='Arial 10')` and `var.place()`.Read it [in this question](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name).My fault,I made a careless mistake.I edited my post. – jizhihaoSAMA Apr 18 '20 at 11:20
  • 1
    Yes. That's what I meant. – Viorel Apr 18 '20 at 11:29