Bottom Page

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()
Quote
#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()

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Kivy] Why I have to click twice to scroll? Hummingbird 0 624 Jan-06-2020, 09:08 PM
Last Post: Hummingbird
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 1,933 Sep-30-2019, 07:30 AM
Last Post: Maksim
  Require scroll bars horizontal and vertical throughout the window tejgandhi 2 533 Jun-28-2019, 03:13 AM
Last Post: tejgandhi
  [Tkinter] Scroll Bars going backwards goofygoo 2 421 Jun-07-2019, 05:07 PM
Last Post: goofygoo
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 2,041 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] Scroll at cursor position lollo 4 2,257 Jan-31-2018, 11:33 PM
Last Post: lollo
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 2,244 Jan-29-2018, 10:44 PM
Last Post: Larz60+
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 6,688 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi

Forum Jump:


Users browsing this thread: 1 Guest(s)