0

I'm creating line numbers in my notepad app and I'm trying to get the line numbers text widget and the one where you enter text into to scroll with each other. Currently I have gotten the scroll bar part working. Its the mouse wheel that's giving me issues. I cant seem to get the mouse wheel par to work right.

Code for scroll:

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

def _bound_to_mousewheel(event):
    root.bind_all("<MouseWheel>", _on_mousewheel)

def _bound_to_mousewheel(self, event):
    self.root.bind_all("<MouseWheel>", self._on_mousewheel)

my_frame = Frame(root, background="#2A2F31") 
my_frame.pack(pady=5, fill=BOTH, expand=True) 
my_frame.bind('<Enter>', _bound_to_mousewheel)


text_scroll = Scrollbar(my_frame) 
text_scroll.pack(side=RIGHT, fill=Y) 

text_scroll2 = Scrollbar(my_frame, orient='horizontal')  
text_scroll2.pack(side=BOTTOM, fill=X)  

lines = Text(my_frame, width= 5, borderwidth = '0', font=("Helvetica", 16), yscrollcommand=text_scroll.set, fg="WHITE", bg="#2f3136", wrap='none', undo=True)
lines.pack(side=LEFT, fill=Y)
lines.config(state='disabled')


my_text = Text(my_frame, font=("Helvetica", 16), selectbackground="Light Blue", selectforeground="Black", undo=True, yscrollcommand=text_scroll.set, bg="#2f3136", fg="WHITE", wrap='none', xscrollcommand=text_scroll2.set, borderwidth = '0') 
my_text.pack(fill=BOTH, expand=True) 


text_scroll.config(command=scroll)
text_scroll2.config(command=my_text.xview)  

With the code here, there are no errors in the console but also nothing happens. Ive looked at other similar questions but they weren't much help. I could not get them implamented into my code. Could someone help me with this.

No-Question123
  • 61
  • 1
  • 1
  • 6
  • Does this answer your question? [tkinter: binding mousewheel to scrollbar](https://stackoverflow.com/questions/17355902/tkinter-binding-mousewheel-to-scrollbar) – Matiiss Nov 26 '21 at 18:31
  • no, i could not get that to work right. @Matiiss – No-Question123 Nov 27 '21 at 00:15
  • you don't seem to have even tried that, did you try applying the solution to your code? because I don't see that here, it really should be the same as in the answer but just in the function do this for two widgets – Matiiss Nov 27 '21 at 11:05
  • i applied the soulution and it keeps saying this: TypeError: _bound_to_mousewheel() missing 1 required positional argument: 'event' @Matiiss – No-Question123 Nov 27 '21 at 16:28
  • yes, remove the `self` argument, it is used when using classes and remove `self.` prefix as that refers to that `self` argument, you had to use the idea from there, which is explained very well – Matiiss Nov 27 '21 at 20:15
  • i got rid of self and now it says: AttributeError: '_tkinter.tkapp' object has no attribute 'yview_scroll' @Matiiss – No-Question123 Nov 27 '21 at 21:12
  • you need to scroll the text widgets not root – Matiiss Nov 27 '21 at 21:13
  • now i got: `_tkinter.TclError: expected integer but got "-1.0"` @Matiiss – No-Question123 Nov 27 '21 at 21:21
  • use `int(-1 * (event.delta / 120))` so that it is an integer as it is expecting which can be read from the error – Matiiss Nov 27 '21 at 22:16
  • it gave me the same error but with a positive 1.0 this time @Matiiss – No-Question123 Nov 27 '21 at 22:32
  • can you show how the code currently looks because I didn't have that issue after explicitly converting the value to an integer as I have shown above – Matiiss Nov 27 '21 at 22:33
  • my bad. i made a mistake but its how you said now but now its saying: `AttributeError: 'function' object has no attribute 'int'` – No-Question123 Nov 27 '21 at 23:35
  • `my_text.yview_scroll(int(-1*(event.delta/120)), "units")` should work but you really seem to need to learn the basics and simple debugging and reading error messages, like I get not understanding the tkapp has no attribute (even tho its a simple attribute error) but the other errors were pretty much self explanatory, and you should have known how to convert to an integer, based on that you are creating a GUI already – Matiiss Nov 28 '21 at 06:54

0 Answers0