Scroll frame with MouseWheel - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Scroll frame with MouseWheel (/thread-25266.html) |
Scroll frame with MouseWheel - Nemesis - Mar-25-2020 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() RE: Scroll frame with MouseWheel - Nemesis - Mar-25-2020 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() |