I'm doing a desktop application with Tkinter and Python. I have inserted a drop-down control ttk.Combobox
for several options but I have a problem. The combobox is inside a frame with scroll bar and when under the mouse wheel the combobox it goes down together with all the frame , it's very rare. Does anyone know how to solve this?
This is my code, the class VerticalScrolledFrame
is downloaded from GitHub:
import tkinter as tk
from tkinter import ttk
class VerticalScrolledFrame(tk.Frame):
def __init__(self, parent, *args, **kw):
tk.Frame.__init__(self, parent, *args, **kw)
# create a canvas object and a vertical scrollbar for scrolling it
vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
canvas = tk.Canvas(self, bd=0, highlightthickness=0, bg='green',
yscrollcommand=vscrollbar.set)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.TRUE)
vscrollbar.config(command=canvas.yview)
# reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = tk.Frame(canvas)
interior_id = canvas.create_window(0, 0, window=interior,
anchor=tk.NW)
def _on_mousewheel(event):
canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
self.interior.bind_all("<MouseWheel>", _on_mousewheel)
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the canvas's width to fit the inner frame
canvas.config(width=interior.winfo_reqwidth())
interior.bind('<Configure>', _configure_interior)
def _configure_canvas(event):
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
canvas.bind('<Configure>', _configure_canvas)
class Application(ttk.Frame):
def __init__(self, main_window):
super().__init__(main_window)
main_window.geometry("400x400")
frame = VerticalScrolledFrame(main_window)
frame.pack(expand=True, fill='both')
optionList = ['option1','option2','option3','option4','option5','option6','option7','option8',]
combobox = ttk.Combobox(frame.interior, state="readonly", height=4, values=optionList)
combobox.pack(side='top')
for i in range(20):
tk.Button(frame.interior, text= 'Button'+str(i)).pack()
main_window = tk.Tk()
app = Application(main_window)
app.mainloop()