简体   繁体   English

无法对齐 tkinter 中的三个文本小部件

[英]Can't align three text widget in tkinter

I'd like to create three text areas in a tkinter window and make them dinamically resizable.我想在 tkinter window 中创建三个文本区域,并使其可动态调整大小。 I thought that one solution was to pass the width and height parameters in pixels (such as height=int(win_height/2)), but as I read it isn't possible, in fact the width and height parameters in a tk.Text widget are calculated by characters for each line and column.我认为一种解决方案是以像素为单位传递宽度和高度参数(例如 height=int(win_height/2)),但正如我所读到的那样,这是不可能的,实际上是 tk.Text 中的宽度和高度参数小部件由每行和每列的字符计算。 I've also tried to pass the width and height parameters in percentages (such as height=50%) but it returns me a syntax error.我还尝试以百分比形式传递宽度和高度参数(例如高度 = 50%),但它返回给我一个语法错误。

I've been trying to find out a solution for this problem in the net, and the best code I've found is this:我一直试图在网上找到解决这个问题的方法,我找到的最好的代码是这样的:

import tkinter as tk

root = tk.Tk()
root.geometry("500x500")

# Text Box
first_textbox = tk.Text(root, width=25, height=10, bg='yellow')
second_textbox = tk.Text(root, width=25, height=10, bg='blue')
third_textbox = tk.Text(root, width=50, height=20, bg='red')

# Packing
first_textbox.grid(column=1, row=1)  
second_textbox.grid(column=1, row=2)
third_textbox.grid(column=2, row=1, rowspan=2)

root.mainloop()

By running this code I obtain a window with three different text areas which aren't dinamically resizabled and which take more space than the actual window width.通过运行此代码,我获得了一个 window ,其中包含三个不同的文本区域,这些文本区域没有动态调整大小,并且比实际的 window 宽度占用更多空间。 I hope you can help me.我希望你能帮助我。 Sorry for any English mistake, it is my second lenguage抱歉有任何英语错误,这是我的第二语言

grid has several documented parameters to help you do what you want. grid有几个文档化的参数来帮助你做你想做的事。 You simply need to use them.你只需要使用它们。

By default, grid won't give widgets any extra space -- they take up only the space they need and no more.默认情况下, grid不会给小部件任何额外的空间——它们只占用他们需要的空间,不会更多。 If you widgets to be allocated extra space, you have to explicitly arrange for that.如果要为小部件分配额外空间,则必须明确安排。

For example, if you want all widgets to grow and shrink equally, you need to configure the rows and columns to have an equal weight greater than zero.例如,如果您希望所有小部件均等地增长和收缩,则需要将行和列配置为具有大于零的相等weight That will tell grid how to allocate any extra space when the window is bigger than the size requested by all of the widgets.当 window 大于所有小部件请求的大小时,这将告诉网格如何分配任何额外空间。

For example:例如:

root.grid_rowconfigure((1,2), weight=1)
root.grid_columnconfigure((1,2), weight=1)

That just tells grid what to do with extra space.这只是告诉grid如何处理额外的空间。 If instead, you want two or more rows or columns to have exactly the same size, you can use the uniform option to tell grid that you want the rows or columns to have a uniform (identical) size.相反,如果您希望两个或更多行或列具有完全相同的大小,则可以使用uniform选项告诉grid您希望行或列具有统一(相同)的大小。

For example, if you want both columns 1 and 2 to have the same width, you can give each column the same value for the uniform option.例如,如果您希望第 1 列和第 2 列具有相同的宽度,则可以为uniform选项赋予每列相同的值。 Note: the value passed to uniform can be anything you want.注意:传递给uniform的值可以是任何你想要的。 The important thing is that they are configured to have the same value.重要的是它们被配置为具有相同的值。

root.grid_columnconfigure((1, 2), uniform="equal")

That alone won't solve the problem.仅凭这一点并不能解决问题。 You also must tell grid that you want the widgets to fill the space given to them.您还必须告诉grid您希望小部件填充给它们的空间。 You do that with the sticky parameter, which tells grid to "stick" the widget to one or more sides of the allocated space.您可以使用sticky参数来执行此操作,该参数告诉网格将小部件“粘贴”到分配空间的一侧或多侧。

To get the widgets to fill all allocated space you can give the string "nsew" which stands for "north, south, east, and west" which represent the four sides of the given space.要让小部件填充所有分配的空间,您可以给出字符串"nsew" ,它代表“北、南、东和西”,代表给定空间的四个边。

first_textbox = tk.Text(root, width=25, height=10, bg='yellow')
second_textbox = tk.Text(root, width=25, height=10, bg='blue')
third_textbox = tk.Text(root, width=50, height=20, bg='red')

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

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