简体   繁体   中英

Scrollbar in python tkinter Toplevel() shows up but does not scroll

I have looked through all the answered questions available here but to no avail. I'm working on Mac OS X High Sierra and my Scrollbar widget shows up but doesn't scroll the window, any advice?

from tkinter import *

root = Tk()
root.geometry('400x500')
root.resizable(False, False) 


def window():

    popup = Toplevel()

    vertScrollbar = Scrollbar(popup, orient='vertical')
    vertScrollbar.pack(side='right', fill='y')

    scrollCanvas = Canvas(popup, width='400', height='500', yscrollcommand=vertScrollbar.set)

    vertScrollbar.config(command=scrollCanvas.yview)

    scrollFrame = Frame(scrollCanvas, width='400', height='500') 
    scrollCanvas.create_window(0, 0, window=scrollFrame, anchor='n')

    for words in range(150):
        test = Label(scrollCanvas)
        test.config(text='this is a test')
        test.pack()

    scrollCanvas.config(scrollregion=scrollCanvas.bbox('all'))
#scrollCanvas.config(scrollregion=(0,0,400,800)) doesn't work either
    scrollCanvas.pack(side='top', fill='both')
    scrollFrame.pack(side='top', fill='both')

openWindow = Button(root, text='Push Me', command=window)
openWindow.pack(ipadx='5', ipady='3', pady='10')

root.mainloop()

There is nothing inside scrollFrame .

The labels are packed in popup , not in scrollCanvas

The scrollCanvas.config(scrollregion=scrollCanvas.bbox('all')) doesn't seem to do the job, not clear as to why.

Here is an example that works for Python 3.6.5 on windows 10:

from tkinter import *

root = Tk()
root.geometry('200x200')
root.resizable(False, False) 

vertScrollbar = Scrollbar(root, orient='vertical')
vertScrollbar.pack(side='right', fill='y')

scrollCanvas = Canvas(root, width='400', height='500',
                      scrollregion=(0, 0, 400, 500),
                      yscrollcommand=vertScrollbar.set)

vertScrollbar.config(command=scrollCanvas.yview)

scrollCanvas.pack(side='top', fill='both')

img = PhotoImage(file='test.gif')
scrollCanvas.create_image(2, 2, anchor='nw', image = img)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM