2

I added scroll bar in a frame when the entry grid boxes exceeds the height of the frame. The scroll bar can be dragged and mouse wheel works when the cursor is hovering in the area of scroll bar but when the cursor is elsewhere, it doesn't work. How to make the scrolling possible when cursor is anywhere on the screen?

from tkinter import *

def data():
    for i in range(50):
        if (i % 2) == 0:
            l4 = Label(frame, text="Size of rectangle:")
            l4.grid(row=i, column=0)
        en = Entry(frame)
        en.grid(row=i, column=1)
    b3 = Button(frame, text="Save")
    b3.grid(row=51, column=0)
    b4 = Button(frame, text="Back")
    b4.grid(row=51, column=1)


def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=600,height=600)

root=Tk()
sizex = 650
sizey = 625
posx  = 100
posy  = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

myframe=Frame(root,relief=GROOVE,width=200,height=200,bd=1)
myframe.place(x=20,y=20)


canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)

myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
data()
root.mainloop()
Devin Maharjan
  • 109
  • 2
  • 10
  • self.canvas.bind_all("", self._on_mousewheel) ... def _on_mousewheel(self, event): self.canvas.yview_scroll(-1*(event.delta/120), "units") What should I write on mine? – Devin Maharjan Mar 06 '19 at 08:18
  • 1
    If you want that scrollbar to move as long as you are scrolling inside the GUI, then bind it to `root`. – Henry Yik Mar 06 '19 at 08:29
  • I did this : def _on_mousewheel(event): canvas.yview_scroll(-1*(event.delta/120), "units") canvas.bind_all("", _on_mousewheel) and got errors. – Devin Maharjan Mar 06 '19 at 10:52
  • Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\sel\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "", line 19, in _on_mousewheel canvas.yview_scroll(-1*(event.delta/120), "units") File "C:\Users\sel\Anaconda3\lib\tkinter\__init__.py", line 1751, in yview_scroll self.tk.call(self._w, 'yview', 'scroll', number, what) _tkinter.TclError: expected integer but got "1.0" – Devin Maharjan Mar 06 '19 at 10:52
  • If you read through that post you should find your answer. – Henry Yik Mar 06 '19 at 13:40
  • I got it. Thanks. – Devin Maharjan Mar 07 '19 at 02:39

0 Answers0