Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:37:30+00:00 2026-06-08T02:37:30+00:00

I can’t sort out how to make a custom widget receive mouse scroll events.

  • 0

I can’t sort out how to make a custom widget receive mouse scroll events. If I bind to the root window, then notifications occur. And if I bind to a child of root window other than my widget (here a simple Listbox), the notifications also occur (evidenced by watching the list move when I move the wheel). What am I overlooking?

Example code where roll() never gets called:

#!/usr/bin/python3
from tkinter import *
from tkinter.ttk import *

class CustomWidget(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.width = 200
        self.height = 200
        self.canvas = Canvas(self, width=200, height=200)
        self.canvas.config(background='red')
        self.canvas.pack()

        self.bind('<MouseWheel>', self.roll)
        self.bind('<Button-4>', self.roll)
        self.bind('<Button-5>', self.roll)

    def roll(self, event):
        print("detected mouse roll!");

if __name__ == "__main__":
    root = Tk()
    root.wm_title("TestRoot")

    sb = Scrollbar(root, orient=VERTICAL)
    lb = Listbox(root, yscrollcommand=sb.set)
    sb.config(command=lb.yview)
    cw = CustomWidget(root)

    for char in list("abcdefghijklmnopqrstuvwxyz"):
        lb.insert(END, char)

    cw.pack()
    lb.pack()
    sb.pack()

    root.update()
    root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:37:33+00:00Added an answer on June 8, 2026 at 2:37 am

    So in order for a frame to receive events, it needs to have focus. You can call frame.set_focus() on it, but as soon as you give another widget focus it won’t work. To get around that we could bind <Button-1> to the frame and have that set the focus to the frame, but your canvas takes up the entire size of the frame, so You will need to bind the <Button-1> events to that instead.

    Adding:

    self.canvas.bind("<Button-1>", lambda _: self.focus_set())
    

    after your other bindings in CustomWidget.__init__ will make your bindings work as long as the widget has focus, which it will when the user clicks it (similar how to the listbox works). If the canvas is never as large as it’s frame, you may need to add another <Button-1> binding to the frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't work out a way to make an array of buttons in android. This
Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can anyone help me trying to find out why this doesn't work. The brushes
can someone show me the regex for this preg_match. I want to make sure
Can anyone explain the following behavior to me? This doesn't make any sense. I
Can it be done? Does Facebook API support defining new custom fields / objects
Can anyone let me know how can we change the value of kendo combobox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.