Tkinter uses Canvas and Scrollbar to create a list box with Checkbutton.
I'd like to expand the scrollbar range.
Currently, scrolling works only if there is a cursor on the scroll bar.
I want to make it work even if there is a cursor on Canvas or Checkbutton.
import tkinter ask
from tkinter import ttk
classItemListFrame(tk.Frame):
def__init__(self, master):
tk.Frame.__init__(self, master, width=100, height=100,
borderwidth=1,relief=tk.GROOVE)
# Widget Deployment
self.place_widget()
place_widget(self):
#Create Canvas
canvas=tk.Canvas (master=self, width=300, height=200, bg='white')
canvas.grid (row=0, column=0)
# scrollbar
vbar=ttk.Scrollbar(master=self, orient=tk.VERTICAL)#vertical
vbar.grid (row=0, column=1, sticky=tk.NS)
# Process to notify Canvas of scrollbar control
vbar.config (command=canvas.yview)
# The process of notifying the scrollbar of the Canvas range of motion.
canvas.config (yscrollcommand=vbar.set)
# scroll range
canvas.config(scrollregion=(0,0,300,5000))
# Create Frame and place it in canvas
frame=tk.Frame(master=canvas,bg='white')
canvas.create_window(0,0), window=frame, anchor=tk.NW, width=canvas.cget('width')))
self.vars = [ ]
for i in range (50):
var=tk.BooleanVar()
self.vars.append(var)
check_button=tk.Checkbutton(master=frame, text=i, variable=var)
check_button.grid(row=i, column=0)
root=tk.Tk()
frame = ItemListFrame(root)
frame.pack()
root.mainloop()
Python 3.10
Windows 10
VScode
Assuming the source code is as intended, mouse wheel scrolling will look like this:
In this case, bind the "event" to the canvas, frame, and check buttons to reflect the amount of scroll movement.
import tkinter ask
from tkinter import ttk
classItemListFrame(tk.Frame):
def__init__(self, master):
tk.Frame.__init__(self, master, width=100, height=100,
borderwidth=1,relief=tk.GROOVE)
# widget placement
self.place_widget()
place_widget(self):
inframe=tk.Frame(master=self,bg='white')
inframe.grid (row=0, column=0)
# Canvas Creation
canvas=tk.Canvas(master=inframe, width=300, height=200, bg='white')
canvas.grid (row=0, column=0)
# scroll bar
vbar=ttk.Scrollbar(master=inframe, orient=tk.VERTICAL)#vertical
vbar.grid (row=0, column=1, sticky=tk.NS)
# The process of notifying Canvas of scrollbar control
vbar.config (command=canvas.yview)
# The process of notifying the scrollbar of the Canvas range of motion.
canvas.config (yscrollcommand=vbar.set)
# scroll range
canvas.config(scrollregion=(0,0,300,5000))
# Create a Frame and Place it in Canvas
frame=tk.Frame(master=canvas,bg='white')
canvas.create_window(0,0), window=frame,
anchor=tk.NW, width=canvas.cget('width'))
frame.bind("<MouseWheel>",
lambda event:self._mousewheel_(event,canvas))
canvas.bind("<MouseWheel>",
lambda event:self._mousewheel_(event,canvas))
self.vars = [ ]
for i in range (50):
var=tk.BooleanVar()
self.vars.append(var)
check_button=tk.Checkbutton(master=frame, text=i, variable=var)
check_button.grid(row=i, column=0)
check_button.bind("<MouseWheel>",
lambda event:self._mousewheel_(event,canvas))
def_mousewheel_(self, event, canvas):
canvas.yview_scroll (int(-1*(event.delta/120)), "units")
print(event)
root=tk.Tk()
frame = ItemListFrame(root)
frame.pack()
root.mainloop()
© 2024 OneMinuteCode. All rights reserved.