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

那年仲夏 提交于 2021-01-29 19:24:07

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/50230309/scrollbar-in-python-tkinter-toplevel-shows-up-but-does-not-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!