简体   繁体   English

为什么我的 tkinter 水平滚动条在右下角被压扁,但垂直滚动条看起来正常?

[英]Why is my tkinter horizontal scrollbar squashed in the bottom, right-hand corner, but the vertical scrollbar looks normal?

I'm using the code below to create two scrollbars in a tkinter window, after that I fill it with text widgets, etc.我正在使用下面的代码在 tkinter window 中创建两个滚动条,然后用文本小部件等填充它。

The vertical scrollbar looks ok, and seems to be working ok (except that I can't use a mouse-wheel to scroll with it), but the horizontal scrollbar appears to be squashed into the bottom, right-hand corner of the window.垂直滚动条看起来不错,似乎工作正常(除了我不能使用鼠标滚轮滚动),但水平滚动条似乎被挤压到 window 的右下角。

As far as I can tell, I'm setting both scrollbars up the same way, so why would the horizontal one not extend right across the bottom of the window?据我所知,我将两个滚动条设置为相同的方式,那么为什么水平滚动条不会延伸到 window 的底部呢?

# Create a main frame with a canvas so that it's possible use a scroll bar
self.current_rendered_window["main_frame"] = Frame(root)
self.current_rendered_window["main_frame"].pack(fill=BOTH, expand=1)

self.current_rendered_window["main_canvas"] = Canvas(self.current_rendered_window["main_frame"])
self.current_rendered_window["main_canvas"].pack(side=LEFT, fill=BOTH, expand=1)

self.current_rendered_window["main_scrollbar"] = ttk.Scrollbar(
    self.current_rendered_window["main_frame"],
    orient=VERTICAL, command=self.current_rendered_window["main_canvas"].yview
)
self.current_rendered_window["main_scrollbar"].pack(side=RIGHT, fill=Y)

self.current_rendered_window["horizontal_scrollbar"] = ttk.Scrollbar(
    self.current_rendered_window["main_frame"],
    orient=HORIZONTAL, command=self.current_rendered_window["main_canvas"].xview
)
self.current_rendered_window["horizontal_scrollbar"].pack(side=BOTTOM, fill=X)

self.current_rendered_window["main_canvas"].configure(
    yscrollcommand=self.current_rendered_window["main_scrollbar"].set,
    xscrollcommand=self.current_rendered_window["horizontal_scrollbar"].set
)
self.current_rendered_window["main_canvas"].bind(
    '<Configure>',
    lambda e: self.current_rendered_window["main_canvas"].configure(
        scrollregion=self.current_rendered_window["main_canvas"].bbox("all")
    )
)

self.current_rendered_window["secondary_frame"] = Frame(self.current_rendered_window["main_canvas"])
self.current_rendered_window["main_canvas"].create_window(
    (0, 0),
    window=self.current_rendered_window["secondary_frame"],
    anchor="nw"
)

In the code above current_rendered_window is a dictionary which is drawn upon every time the window is reloaded, so all frames, canvases, widgets, etc. need to be added to it as I am doing in the code.在上面的代码中, current_rendered_window是一个字典,每次重新加载 window 时都会绘制该字典,因此需要将所有框架、画布、小部件等添加到其中,就像我在代码中所做的那样。

You need to pack the horizontal scrollbar first:您需要先打包水平滚动条:

self.current_rendered_window["main_canvas"] = Canvas(self.current_rendered_window["main_frame"], bg="yellow")

self.current_rendered_window["main_scrollbar"] = ttk.Scrollbar(
    self.current_rendered_window["main_frame"],
    orient=VERTICAL, command=self.current_rendered_window["main_canvas"].yview
)

self.current_rendered_window["horizontal_scrollbar"] = ttk.Scrollbar(
    self.current_rendered_window["main_frame"],
    orient=HORIZONTAL, command=self.current_rendered_window["main_canvas"].xview
)

self.current_rendered_window["horizontal_scrollbar"].pack(side=BOTTOM, fill=X)
self.current_rendered_window["main_canvas"].pack(side=LEFT, fill=BOTH, expand=1)
self.current_rendered_window["main_scrollbar"].pack(side=RIGHT, fill=Y)

However, better use grid() instead of pack() .但是,最好使用grid()而不是pack()

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

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