Python Forum
Scroll frame with MouseWheel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scroll frame with MouseWheel
#1
Hi,

I would like to make a widget to pass picture with scroll button. Below is my code however I cannot pass images while scroll button is working. Any idea how to change it? Thanks in advance.

root = tkinter.Tk()
root.wm_title("Try MouseWheel")

def imageShow():

    global fig
    fig = Figure()
    fig.add_subplot().imshow(imageDim[:, :, slide], cmap='gray', vmin=minInt, vmax=maxInt)
    canvas = FigureCanvasTkAgg(fig, master=root)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand='true')
    root.bind("<MouseWheel>", mouse_wheel)



def clearfig():
    fig.clear()


def mouse_wheel(event):
    global slide
    print(slide)
    # respond to Linux or Windows wheel event

    if event.num == 5 or event.delta == -120:
        slide -= 1

    if event.num == 4 or event.delta == 120:
        slide += 1
    clearfig()

imageShow()
root.mainloop()
Reply
#2
I have managed to solve it. Below I show you the code. However, I have a question. If I resize the window to the full one it scrolls quite slow. Any ides why it is this why?

# imageDim - volume of the dicom files

root = tkinter.Tk()
root.wm_title("Try MouseWheel")
slide = 128
minInt = np.min(imageDim)
maxInt = np.max(imageDim)
def main_loop():

    fig, ax = plt.subplots()
    ax.imshow(imageDim[:, :, slide], cmap='gray', vmin=minInt, vmax=maxInt, zorder=1)
    canvas = FigureCanvasTkAgg(fig, master=root)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand='true')
    canvas.mpl_connect('scroll_event', mouse_wheel)

def mouse_wheel(event):
    global slide
    fig = event.canvas.figure
    ax = fig.axes[0]
    print(slide)
    print(event.step)

    if event.step < 0:
        slide += event.step
        ax.imshow(imageDim[:, :, int(slide)], cmap='gray', vmin=minInt, vmax=maxInt, zorder=1)

    if event.step > 0:
        slide += event.step
        ax.imshow(imageDim[:, :, int(slide)], cmap='gray', vmin=minInt, vmax=maxInt, zorder=1)
    fig.canvas.draw()


main_loop()
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,769 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] How do I get a QScrollArea to scroll? LavaCreeperKing 9 7,604 Oct-29-2021, 08:33 AM
Last Post: Axel_Erfurt
  Treeview scroll selected node to top rfresh737 1 2,668 Apr-14-2021, 03:27 AM
Last Post: deanhystad
  [Tkinter] canvas widget scroll issue chrisdb 2 3,783 Apr-07-2021, 05:48 AM
Last Post: chrisdb
  [Tkinter] Help with scroll bars kraco 1 2,205 Sep-27-2020, 11:20 PM
Last Post: Larz60+
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 3,897 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  [Kivy] Why I have to click twice to scroll? Hummingbird 0 2,326 Jan-06-2020, 09:08 PM
Last Post: Hummingbird
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 8,944 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] Scroll Bars going backwards goofygoo 2 2,663 Jun-07-2019, 05:07 PM
Last Post: goofygoo
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 11,001 Apr-18-2019, 05:36 PM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020