问题描述
限时送ChatGPT账号..我意识到这里以及之前在其他论坛上曾有人问过这个问题的某种形式.我已经通读了所有这些,并为这些解决方案努力了几个小时,但我仍然无法得到它.目标只是一个新窗口(Toplevel),其中有一个可滚动的画布,其中有一个包含一些内容的框架.我仍然无法让画布上的滚动条工作:
I realize some form of this question has been asked on here as well as in other forums before. I've read through all of them and worked towards those solutions for hours each and I still cant get it. The goal was just a new window (Toplevel) with a scrollable canvas in it that has a frame with some content in it. I still can't get the scroll bars on the canvas to work:
#make new window
self.edit_window = Tkinter.Toplevel()
self.edit_window.title("Data Refinement")
self.edit_window.maxsize(height='50', width='300')
#make scrollbar for canvas
cScrollbar = Tkinter.Scrollbar(self.edit_window)
cScrollbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
#make canvas
canvas = Tkinter.Canvas(self.edit_window)
#attach canvas to scrollbar
canvas.config(yscrollcommand=cScrollbar.set)
cScrollbar.config(command=canvas.yview)
#make frame and put everything in frame
frame = Tkinter.Frame(self.edit_window)
#random fill
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
#scrollbar for listbox
scrollbar = Tkinter.Scrollbar(frame)
scrollbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
listbox = Tkinter.Listbox(frame)
listbox.pack()
#insert some random data for now into listbox
for i in range(100):
listbox.insert(Tkconstants.END, i)
# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
#pack and attach to canvas
frame.pack(fill=Tkconstants.BOTH, expand=Tkconstants.YES)
canvas.create_window(0,0, anchor = Tkconstants.NW, window = frame)
canvas.pack(fill=Tkconstants.BOTH, expand=Tkconstants.YES)
canvas.config(scrollregion=canvas.bbox(Tkconstants.ALL))
这样就成功地用内容制作了框架.并且框架成功地附加到画布上.我不明白的是,附加到列表框的滚动条有效,而连接到画布的滚动条显示但实际上不起作用.滚动条就像所有内容都已显示.它就像是在可见的内容上滚动,而不是在画布的整个内容上滚动.
So the frame is successfully made with the contents. And the frame is successfully attached to the canvas. What I dont get is that the scroll bar attached to the listbox works while the one connected to the canvas displays but does not actually work. The scroll bar acts like everything is already displayed. It's like it is scrolling on what is visible instead of scrolling on the entirety of the contents of the canvas.
推荐答案
我现在不在电脑前验证,但我的猜测是:框架的高度将为 1,直到小部件被映射,在它会增长或缩小以适应其内容.但是,您在此之前设置了画布滚动区域,因此滚动区域实际上为零.您可以通过打印命令 canvas.bbox(Tkconstants.ALL)
I'm not at a computer right now to verify, but my guess is this: the height of the frame will be 1 until the widget is mapped, at which point it will grow or shrink to fit its contents. However, you are setting the canvas scrollregion before this happens so the scroll region is effectively zero. You can verify this by printing out the result of the command canvas.bbox(Tkconstants.ALL)
在配置滚动区域之前尝试添加对 self.update_idletasks
的调用,看看是否能修复它.
Try adding a call to self.update_idletasks
before configuring the scroll region and see if that fixes it.
这篇关于Tkinter 中画布上的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论