2

Trying to call hii function in python tkinter, but nothing happens.

My Code:-

def hii():
    print("hii")


m_root = Tk()
m_frame = Frame(m_root)
m_display = Label(m_frame)

label = Label(m_root,text="hii") #set your text
label.bind("<Enter>",hii)
label.pack()

m_display.pack()
m_frame.pack()
m_display.update()

m_root.mainloop()
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
Deoj
  • 71
  • 1
  • 5
  • 2
    Are you sure _nothing_ happens? You should be getting an error when your mouse hovers over the label. – Bryan Oakley May 20 '21 at 18:51
  • @Matiiss Don't confuse `""` with `""`. BryanOakley is correct in saying that an error should be raised – TheLizzard May 20 '21 at 18:55
  • Ohh yes @BryanOakley , when I move mouse to text, i have message on console: `Traceback (most recent call last): File "C:\Python\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) TypeError: hi() takes 0 positional arguments but 1 was given` – Deoj May 20 '21 at 18:56
  • @Deoj Do you want the event to be triggered when you press the enter key on your keyboard or when the mouse cursor `Enter`s the widget? – TheLizzard May 20 '21 at 18:58
  • 1
    @Deoj that is because `.bind()` passes another argument `event` so it should be handled either like this: `.bind("Event", lambda e: func())` or `def func(event):` – Matiiss May 20 '21 at 18:59

1 Answers1

1

First line should be: def hii(event): and it works fine. The function should expect an event as an argument.

zozo128
  • 59
  • 1
  • 3