Python: Scrolling with mouse wheel in a textbox while mouse curser is not in the textbox (tkinter)

Question:

I created a textbox in a GUI where mouse wheel scrolling works when the mouse curser is on the textbox widget. I want to be able to do that when the mouse curser is not on the textbox widget. I have several labels with mouse over events. Sometimes the text is too long for the entire textbox widget, meaning I have to scroll down while the mouse curser is on my label. Reason for that is, the text gets deleted when the mouse curser isn’t on the label anymore so scrolling has to be done while on the label.

def mousewheel(event):
        textbox.yview_scroll(-1*(event.delta/120), "units")

textbox = Text(ctr_mid, height=30, width=60, font="Arial")
    textbox.grid(row=6,sticky=S)
    textbox.bind_all("<MouseWheel>", mousewheel)

I looked for solutions online and found the mouswheel function online but when trying to execute it I get this error:

Traceback (most recent call last):   
  File "C:UsersHomieAppDataLocalProgramsPythonPython36-   
 32libtkinter__init__.py", line 1699, in __call__  
    return self.func(*args)  
  File "C:UsersHomiePycharmProjectsbluescreenGame.py", line 402, in   
mousewheel  
self.textbox.xview_scroll(-1*(event.delta / 120), "units")  
  File "C:UsersHomieAppDataLocalProgramsPythonPython36-
32libtkinter__init__.py", line 1724, in xview_scroll  
self.tk.call(self._w, 'xview', 'scroll', number, what)  
_tkinter.TclError: expected integer but got "1.0"  

It’s my first post here so I apologize if I made any mistakes.

Asked By: Chris

||

Answers:

I am also a newbie and this is my first post. But I had the same error and solved it pretty easily.

The error message you posted said it expected an integer but got something else, a float (“1.0”). So you need to ensure that the result of “-1*(event.delta/120)” is an integer, not a float. So your new code for your function mousewheel would look like this:

def mousewheel(event):
    textbox.yview_scroll(int(-1*(event.delta/120)), "units")

This worked for me:

my_canvas.bind_all('<MouseWheel>', lambda e: my_canvas.yview_scroll(int(-1*(e.delta/120)),'units'))
Answered By: JPM