Python Tkinter:使用Pack()为Canvas添加水平滚动条

huangapple go评论65阅读模式
英文:

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 &quot;RoundFrame&quot;.

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 &quot;RoundFrame&quot;.

Right now only seven of the ten &quot;A&quot; 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>



huangapple
  • 本文由 发表于 2023年5月25日 12:07:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定