简体   繁体   English

Tkinter 中的滚动条

[英]Scrollbar in Tkinter

I tried many things to do 2 things :我尝试了很多事情来做两件事:

  • center my content (horizontal)居中我的内容(水平)
  • have a scrollbar right and bottom有一个左右滚动条

Here my code : # encoding: utf8 from tkinter import * from tkinter import ttk这是我的代码:# encoding: utf8 from tkinter import * from tkinter import 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()

So the result is the following :所以结果如下:

在此处输入图片说明

So any suggestion ?那么有什么建议吗?

The bottom sidebar is so small !底部侧边栏太小了! I don't know why.我不知道为什么。 Are regarding the text, it's not center at all ;( I want to have it center and changing position if I rezize the window关于文本,它根本不是居中;(如果我调整窗口大小,我想让它居中并改变位置

Thanks a lot非常感谢

pack works by allocating space along an entire empty side of the master, so the order of when you call pack matters. pack工作原理是沿着 master 的整个空侧分配空间,因此调用 pack 的顺序很重要。 When you pack the canvas before packing the scrollbar, the canvas takes up the entire left side from top to bottom.在打包滚动条之前打包画布时,画布从上到下占据了整个左侧。

The simple solution is to pack the scrollbars before you pack the canvas.简单的解决方案是在打包画布之前打包滚动条。

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

To make the UI look a bit more professional, grid is usually a better choice when laying out scrollbars.为了让 UI 看起来更专业一些, grid通常是布局滚动条时更好的选择。 With pack , either one edge of the horizontal scrollbar will be below the vertical scrollbar, or the edge of the vertical scrollbar will be directly beside the horizontal one.使用pack ,水平滚动条的边缘要么位于垂直滚动条的下方,要么垂直滚动条的边缘直接位于水平滚动条的旁边。 That, or you have to add a little bit of padding so the scrollbars don't seem to overlap.那,或者您必须添加一点填充,以便滚动条似乎不会重叠。

When I have a scrollable widget, I almost always put it and the one or two scrollbars together in a frame using grid .当我有一个可滚动的小部件时,我几乎总是使用grid将它和一两个滚动条放在一个框架中。 Then, I can treat them all as if they were a single widget when adding them to the rest of the UI.然后,在将它们添加到 UI 的其余部分时,我可以将它们全部视为单个小部件。


In a comment you ask about how to do it in a grid.在评论中,您询问如何在网格中进行操作。 You simply need to place the widgets in the appropriate rows and columns, and make sure that the row and column with the scrollable widget has a non-zero weight so any extra space is allocated to it.您只需将小部件放置在适当的行和列中,并确保具有可滚动小部件的行和列具有非零权重,以便为其分配任何额外空间。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM