python tkinter中的滚动条Toplevel()显示但不滚动

时间:2018-05-08 09:26:53

标签: python macos tkinter scrollbar

我已经查看了这里提供的所有已回答的问题,但无济于事。我正在使用Mac OS X High Sierra,我的Scrollbar小部件显示但是没有滚动窗口,有什么建议吗?

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 个答案:

答案 0 :(得分:1)

scrollFrame内没有任何内容。

标签打包在popup中,而不是scrollCanvas

scrollCanvas.config(scrollregion=scrollCanvas.bbox('all'))似乎没有做到这一点,不明白为什么。

这是一个适用于Windows 10上的Python 3.6.5的示例:

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