简体   繁体   English

Tkinter滚动条

[英]Tkinter Scrollbar

If I call text_area.pack() before scrollbar.pack() (ie switch them), the scrollbar doesn't show. 如果我在scrollbar.pack() text_area.pack()之前调用text_area.pack() (即切换它们),则不会显示滚动条。 Why is that? 这是为什么? If I am going to create a larger program, I would have absolutely no chance to find out where the problem is. 如果我要创建一个更大的程序,我绝对没有机会找出问题所在。

from tkinter import *
import tkinter.filedialog



root = Tk()
root.geometry("200x100")
frame = Frame(root,width=150, height=90)
frame.pack()
scrollbar = Scrollbar(frame)
text_area = Text(frame, width=200, height=50,yscrollcommand=scrollbar.set)
scrollbar.config(command=text_area.yview)
scrollbar.pack(side="right", fill="y")
text_area.pack()
root.mainloop()

The reason the scrollbar doesn't show is because there's simply no room for it. 滚动条不显示的原因是因为根本没有空间。 You're specifying a window size of 200x100 pixels and an inner frame size of 150x90 pixels, but you are trying to put a much larger text widget in that space. 您要指定200x100像素的窗口大小和150x90像素的内部帧大小,但是您试图在该空间中放置更大的文本小部件。 You're specifying a size of 200x50 characters (roughly 1400x750, depending on the fonts you're using) which is much too wide for the available space. 您指定的尺寸为200x50个字符 (大约1400x750,具体取决于您使用的字体),对于可用空间而言,该尺寸太宽了。

The way pack works is that it looks at the available space, puts the widget in that space, and subtracts the spaced needed for that widget from the space available for the next widget. pack工作方式是查看可用空间,将小部件放置在该空间中,然后从下一个小部件的可用空间中减去该小部件所需的空间。 Because you put the text widget first, and it requested more than the available space, it used up all of the available space. 因为您首先放置了文本小部件,并且它请求的空间超出了可用空间,所以它耗尽了所有可用空间。 Then, when you call pack on the scrollbar, there's simply nowhere to put it. 然后,当您在滚动条上调用pack时,根本没有地方放它。

When you reverse the order, the scrollbar takes up only a fraction of the available space, so there's room to fit the text widget in. 反转顺序时,滚动条仅占用可用空间的一小部分,因此有足够的空间容纳文本小部件。

The best solution is to change the order in which you call pack . 最好的解决方案是更改pack调用顺序。 In general, it's best to call pack so that the last widget you call pack on is the "hero" -- the one that takes up all remaining space and grows or shrinks as the window grows and shrinks. 通常,最好调用pack以便最后调用pack小部件是“ hero”,即占据所有剩余空间并随着窗口的增大和缩小而增大或缩小的小部件。 Usually that's a text widget or a canvas widget, or a frame that itself contains many widgets. 通常,这是文本小部件或画布小部件,或者本身包含许多小部件的框架。

The key to success with tkinter is to not try to force a tkinter widget or window to be a particular size in pixels (except, perhaps, for the canvas). tkinter成功的关键是不要尝试将tkinter小部件或窗口强制设置为特定的像素大小(也许画布除外)。 Instead, either let a widget use it's default size (particularly with buttons and scrollbars), or pick a sensible default (number of rows and/or columns). 相反,要么让小部件使用其默认大小(尤其是按钮和滚动条),要么选择一个合理的默认值(行和/或列数)。 Tkinter will then compute the right size to fit everything in based on the font, the screen resolution, and other factors. 然后,Tkinter将根据字体,屏幕分辨率和其他因素计算合适的大小以适合所有内容。

When you switch them, scrollbar.pack() simply is unaware that it needs to go top, instead it goes next place from top to bottom, and right. 当您切换他们, scrollbar.pack()根本不知道它需要去顶,而是去下一个地方从上到下右。 You can see that when you expand window size. 扩大窗口大小时,您会看到。

You can resolve the issue by replacing: 您可以通过以下方法解决此问题:

text_area.pack()

with: 与:

text_area.pack(side="left")

When you want to design more complex geometry structures I'd suggest you use grid instead of pack . 当您要设计更复杂的几何结构时,建议您使用grid而不是pack

The other 2 answers are really good - I thought I would add an example. 其他两个答案真的很好-我想我想举一个例子。 Personally, I don't like to use .pack - I like to place things instead like this: self.set_label_logpage.place(x=175, y=100) 就个人而言,我不喜欢使用.pack-我喜欢这样放置东西: self.set_label_logpage.place(x=175, y=100)

Example code: 示例代码:

faultlogframe_logs = tk.Label(self, textvariable=logging_screen_label, font=Roboto_Normal_Font,
                                   height=180, width=400, background='white', foreground='black')

faultlogframe_logs.place(x=605, y=600)

self.scrollbar = Scrollbar(self, orient=VERTICAL, elementborderwidth=4, width=32)
self.scrollbar.pack(pady=60,padx=0, ipady=4, ipadx=4, side=RIGHT, fill=Y)
self.scrollbar_y = Scrollbar(self, orient=HORIZONTAL, width=12, takefocus=1)
self.scrollbar_y.pack(expand=TRUE, ipady=9, ipadx=9, padx=0, pady=0, side=BOTTOM, fill=X, anchor=S)

self.set_label_logpage = tk.Listbox(self, yscrollcommand=self.scrollbar.set, xscrollcommand=self.scrollbar_y.set)
self.set_label_logpage.config(font=Roboto_Normal_Font, background='white', foreground='black', height=16, width=55) #textvariable=self.label_to_display_log
self.set_label_logpage.place(x=175, y=100)

self.scrollbar_y.config(command=self.set_label_logpage.xview)
self.scrollbar.config(command=self.set_label_logpage.yview)

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

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