本文介绍了python tkinter Toplevel()中的滚动条显示但不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经仔细阅读了这里所有可用的回答问题,但无济于事.我正在Mac OS X High Sierra上工作,并且显示了Scrollbar小部件,但没有滚动窗口,有什么建议吗?
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()
推荐答案
scrollFrame
中没有任何内容.
标签包装在 popup
中,而不包装在 scrollCanvas
The labels are packed in popup
, not in scrollCanvas
scrollCanvas.config(scrollregion = scrollCanvas.bbox('all'))
似乎没有完成任务,不清楚原因.
The scrollCanvas.config(scrollregion=scrollCanvas.bbox('all'))
doesn't seem to do the job, not clear as to why.
以下是适用于Windows 10上的Python 3.6.5的示例:
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()
这篇关于python tkinter Toplevel()中的滚动条显示但不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!