【问题标题】:Scrollbar in TkinterTkinter 中的滚动条
【发布时间】:2020-11-20 19:23:48
【问题描述】:

我尝试了很多事情来做两件事:

  • 将我的内容居中(水平)
  • 右下角有滚动条

这是我的代码: # 编码:utf8 从 tkinter 导入 * 从 tkinter 导入 ttk

class Program:

    def __init__(self):
        # Tk.__init__(self)
        # Fill the content of the window
        self.window = Tk()
        self.window.geometry("1080x720")

        self.createFrameWithScrollbar()
        self.content()

    def createFrameWithScrollbar(self):
        # Create a Main Frame
        self.mainFrame = Frame(self.window)
        self.mainFrame.pack(fill=BOTH, expand=True)
        # Create a canvas
        self.canvas = Canvas(self.mainFrame)
        self.canvas.pack(side=LEFT, fill=BOTH, expand=True)
        # Add a scrobar
        yScrollbar = ttk.Scrollbar(self.mainFrame, orient=VERTICAL, command=self.canvas.yview)
        yScrollbar.pack(side=RIGHT, fill=Y)
        xScrollbar = ttk.Scrollbar(self.mainFrame, orient=HORIZONTAL, command=self.canvas.xview)
        xScrollbar.pack(side=BOTTOM, fill=X)
        # Configure the canvas
        self.canvas.configure(yscrollcommand=yScrollbar.set)
        self.canvas.configure(xscrollcommand=xScrollbar.set)
        self.canvas.bind('<Configure>', lambda e: self.canvas.configure(scrollregion = self.canvas.bbox("all")))

        self.frame = Frame(self.canvas)
        self.canvas.create_window((0,0), window=self.frame, anchor="nw")

        self.currentFrame = Frame(self.frame)
        self.currentFrame.configure(bg="red")
        self.currentFrame.pack(fill=BOTH, expand=1)

    def content(self):
        label_title = Label(self.currentFrame, text="Title")
        label_title.grid(column=0, row=0, sticky=NSEW)
        label_description = Label(self.currentFrame, text="Title")
        label_description.grid(column=0, row=1, sticky=NSEW)
        label_version = Label(self.currentFrame, text="0.2 beta [Novembre 2020]")
        label_version.grid(column=0, row=2, sticky=NSEW)


# Lauch the program
app = Program()
app.window.mainloop()

所以结果如下:

有什么建议吗?

底部边栏太小了!我不知道为什么。 关于文本,它根本不是中心;( 如果我调整窗口大小,我想让它居中并改变位置

非常感谢

【问题讨论】:

  • 您应该在 两个滚动条之后打包 Canvas。目前,它占据了包含 Frame 的整个左侧,因此其他所有内容都必须完全在它的右侧。
  • 此外,建议使用全部pack 或全部grid 并且不要混合几何管理器....除非您是受虐狂。
  • @jasonharper : 我试过了,但没有任何改变
  • @AndrewAllaire:谢谢你的建议。我正在发现 Tkinter,所以让我们尝试使用网格。不确定是否能找到相同的东西
  • @AndrewAllaire:我几乎总是在应用程序中同时使用packgrid。他们每个人都有长处和短处。如果您尝试只用一个人来做所有事情,我认为您更像是一个受虐狂。

标签: python tkinter


【解决方案1】:

pack 的工作原理是沿主节点的整个空白侧分配空间,因此调用 pack 的顺序很重要。在打包滚动条之前打包画布时,画布从上到下占据了整个左侧。

简单的解决方案是在打包画布之前打包滚动条。

yScrollbar.pack(side=RIGHT, fill=Y)
xScrollbar.pack(side=BOTTOM, fill=X)
self.canvas.pack(side=LEFT, fill=BOTH, expand=True)

为了让 UI 看起来更专业一点,grid 在布置滚动条时通常是更好的选择。使用pack,水平滚动条的一个边缘将位于垂直滚动条的下方,或者垂直滚动条的边缘将直接位于水平滚动条的旁边。那,或者您必须添加一点填充,以便滚动条看起来不会重叠。

当我有一个可滚动的小部件时,我几乎总是使用grid 将它和一两个滚动条放在一个框架中。然后,在将它们添加到 UI 的其余部分时,我可以将它们全部视为一个小部件。


在评论中,您询问如何在网格中执行此操作。您只需将小部件放置在适当的行和列中,并确保带有可滚动小部件的行和列具有非零权重,以便为其分配任何额外空间。

self.mainFrame.grid_rowconfigure(0, weight=1)
self.mainFrame.grid_columnconfigure(0, weight=1)

yScrollbar.grid(row=0, column=1, sticky="ns")
xScrollbar.grid(row=1, column=0, sticky="ew")
self.canvas.grid(row=0, column=0, sticky="nsew")

【讨论】:

  • 谢谢!你知道如何对 grid 做同样的事情吗?我试过了,但目前还没有定论
  • 否则你给我的解决方案,两个滚动条都可以;)只是让我的问题居中内容
  • @f42: gridpack 一样简单。只需阅读 grid 的文档,它包含您需要知道的一切。
  • Oaldey 我试过了,但没有成功。画布中的最后一帧不会占用所有空间。其他都可以
  • @f42: 你不需要设置 minsize 来让它工作。
猜你喜欢
  • 2018-04-25
  • 1970-01-01
  • 2015-04-03
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多