如何使用“ tkinter”使用pythin创建多列文本布局?
tkinter 允许我们在Python中创建GUI应用程序。我的问题是创建一个响应式窗口:
- 一列具有固定宽度,但高度是灵活的。
- 当窗口的宽度增加时,添加了更多列。
- 当窗口的宽度减小时,列会删除列。
- 当窗口的高度增加时,列会变长。
- 当窗口的高度降低时,列会变短。
每列的文本根据其大小而移至其他列。例如:
- 如果列的高度增加,则其中显示了更多文本。第一列将从第二列中获取更多文本,第二列将从第三列(或缓冲区)中获取文本。
我的问题是:如何使用TKINTER实现此效果?
tkinter allows us to create GUI applications in Python. My question is to create a responsive window that:
- A column has a fixed width, but a flexible height.
- When window's width increases, more columns are added.
- When window's width decreases, columns are removed.
- When window's height increases, columns become longer.
- When window's height decreases, columns become shorter.
Each column has texts that moves to other columns depending on their sizes. For example:
- If columns' heights increase, then more text is shown inside them. The 1st column will be taking more texts from the 2nd column, and the 2nd column will be taking texts from the 3rd (or the buffer).
My question is: how to achieve this effect with tkinter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有内置的小部件实现此类列功能。 TKINTER也没有以这种方式行为的“列”布局管理器。因此,有必要创建一个自定义小部件。
我的解决方案是用
text
widgets在容器frame
中创建列。您设置所需的列宽度(以字符为单位),然后在窗口大小时更新列的列数,并绑定到
< configure>
(请参阅的第一部分。在下面的示例中resize()
方法。text
用.grid(row = 0,column =< lt; column>,sticky =“ ns”)
在单行上显示并适应该行高度感谢选项sticky =“ ns”
。要在不同列之间拆分内容,我使用
text
小部件的功能。最左列是主text
小部件,我创建了对其他列具有相同内容的对等小部件(请参阅示例中的.resize()
方法的第一部分以下)。这样,所有列的内容都具有相同的内容,但是显示的部分可以独立更改。为此,我使用.yview_moveto(<列号>/<)列的总数>)
在每列中显示内容的适当部分。但是,为了使内容短于可用的显示空间时,我需要使用newlines添加内容以获取一个不错的列显示(请参阅.resize> .resize()
方法的第二部分 in下面的示例)。这是代码:
There is no built-in widget implementing this kind of column feature. Tkinter does not have a "column" layout manager that behaves this way either. It is therefore necessary to create a custom widget.
My solution is to create the columns with
Text
widgets in a containerFrame
.You set the desired column width (in character) and then update the number of columns when the window is resized with a binding to
<Configure>
(see the first part of.resize()
method in the example below). TheText
widgets are displayed with.grid(row=0, column=<column number>, sticky="ns")
on a single row and adapt to the row height thanks to the optionsticky="ns"
.To split the content between the different columns, I use the peer feature of the
Text
widget. The leftmost column is the mainText
widget and I create peer widgets that have the same content for the other columns (see the first part of.resize()
method in the example below). This way all the columns have the same content but the part of it which is displayed can be changed independently. To do so, I use.yview_moveto(<column number>/<total number of columns>)
to display the proper part of the content in each column. However, for this to work when the content is shorter than the available display space, I need to pad the content with newlines to get a nice column display (see the second part of.resize()
method in the example below).Here is the code: