英文:
Python Tkinter: Adding A Horizontal Scrollbar To A Canvas With Pack()
问题
我刚刚开始使用Tkinter尝试构建一个具有垂直和水平可滚动框架的GUI。
我已经成功使用画布将垂直滚动条添加到框架中,但我很难理解为什么水平滚动条没有被正确地添加到框架中。
以下是您可以查看我的当前进展的最小可重现示例:
```python
from tkinter import *
MainWindow = Tk()
MainWindow.geometry('1350x680')
GameFrame = Frame(MainWindow)
canvas = Canvas(GameFrame, bg="green")
canvas.pack(side=LEFT, fill=BOTH, expand=1)
verticalBar = Scrollbar(GameFrame, orient=VERTICAL, command=canvas.yview)
verticalBar.pack(side=RIGHT, fill=Y)
horizontalBar = Scrollbar(GameFrame, orient=HORIZONTAL, command=canvas.xview)
horizontalBar.pack(side=BOTTOM, fill=X)
canvas.configure(yscrollcommand=verticalBar.set,
xscrollcommand=horizontalBar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(
scrollregion=canvas.bbox(ALL)))
canvas.pack(side=LEFT, fill=BOTH, expand=1)
RoundFrame = Frame(canvas, bg="green")
canvas.create_window((0, 0), window=RoundFrame,
anchor="nw", width=1900, height=1900)
GameFrame.pack(fill=BOTH, expand=1)
horizontalPos = 0
verticalPos = 0
for i in range(10):
Label(RoundFrame, text="A", font=('Helvetica 40 bold')).grid(
row=0, column=0, padx=(horizontalPos, 0), pady=(verticalPos, 0))
horizontalPos += 170
verticalPos += 50
MainWindow.mainloop()
我已经创建了一个带有我的画布的窗口。
在这个过程中涉及到两个框架:GameFrame和RoundFrame。
我想在"RoundFrame"上放置标签。
请帮助我理解我做错了什么。谢谢。
<details>
<summary>英文:</summary>
I have just begun working with Tkinter in an effort to build a GUI that has a vertically and horizontally scroll-able frame.
I managed to add the vertical scrollbar to the frame with the help of a canvas, but am struggling to understand why the horizontal scrollbar is not being properly added to the frame.
The following is my minimal reproducible example for you to see where my current progress is:
from tkinter import *
MainWindow = Tk()
MainWindow.geometry('1350x680')
GameFrame = Frame(MainWindow)
canvas = Canvas(GameFrame, bg="green")
canvas.pack(side=LEFT, fill=BOTH, expand=1)
verticalBar = Scrollbar(GameFrame, orient=VERTICAL, command=canvas.yview)
verticalBar.pack(side=RIGHT, fill=Y)
horizontalBar = Scrollbar(GameFrame, orient=HORIZONTAL, command=canvas.xview)
horizontalBar.pack(side=BOTTOM, fill=X)
canvas.configure(yscrollcommand=verticalBar.set,
xscrollcommand=horizontalBar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(
scrollregion=canvas.bbox(ALL)))
canvas.pack(side=LEFT, fill=BOTH, expand=1)
RoundFrame = Frame(canvas, bg="green")
canvas.create_window((0, 0), window=RoundFrame,
anchor="nw", width=1900, height=1900)
GameFrame.pack(fill=BOTH, expand=1)
horizontalPos = 0
verticalPos = 0
for i in range(10):
Label(RoundFrame, text="A", font=('Helvetica 40 bold')).grid(
row=0, column=0, padx=(horizontalPos, 0), pady=(verticalPos, 0))
horizontalPos += 170
verticalPos += 50
MainWindow.mainloop()
I have created a window with the canvas I have.
There are two frames involved in this process: GameFrame and RoundFrame
I want to position labels on "RoundFrame".
Please assist me in understanding what I am doing wrong. Thank you.
I have created a window with the canvas I have.
There are two frames involved in this.
I want to make labels appear on the "RoundFrame".
Right now only seven of the ten "A" labels appear because the horizontal scrollbar has not been added properly.
</details>
# 答案1
**得分**: 0
为了使水平滚动条位于画布下方使用 `pack`,最简单的解决方案是在打包画布之前打包滚动条。如果你将部件的创建与部件布局分开,并将所有同级部件的布局命令分组,这样做会更容易。
verticalBar.pack(side=RIGHT, fill=Y)
horizontalBar.pack(side=BOTTOM, fill=X)
canvas.pack(side=LEFT, fill=BOTH, expand=1)
这是因为打包程序为每个部件分配了可用空间的整个一侧。当你将画布打包到左侧,然后再打包其他部件时,由于画布占据窗口的整个左侧,就无法在它上方或下方放置任何东西。
<details>
<summary>英文:</summary>
For the horizontal scrollbar to be below the canvas using `pack`, the easiest solution is to pack the scrollbar before packing the canvas. This is easier if you separate your widget creation from widget layout, and group all layout commands for siblings.
verticalBar.pack(side=RIGHT, fill=Y)
horizontalBar.pack(side=BOTTOM, fill=X)
canvas.pack(side=LEFT, fill=BOTH, expand=1)
This is because the packer allocates an entire side of the available empty space for each widget. When you pack the canvas to the left before the other widgets, nothing will be able to be placed above or below it since it takes up the entire left side of the window.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论