Python 3 Tkinter - 使用网格创建覆盖100%宽度的文本小部件 [英] Python 3 Tkinter - Create Text Widget covering 100% Width with Grid

查看:725
本文介绍了Python 3 Tkinter - 使用网格创建覆盖100%宽度的文本小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用Pack Geometry Manager轻松创建一个覆盖100%宽度的Tkinter Text小部件:

  from tkinter import * 
root = Tk()
textWidget = Text(root)
textWidget.pack(side = TOP,fill = X)
root.geometry('600x1000 ')
root.mainloop()

但我想知道,我该如何做到这一点使用Grid Geometry Manager和grid_columnconfigure完成相同的任务?

解决方案

使用grid需要执行以下步骤:


  • 使用文本小部件的网格方法,给它一个行和一列。在这种情况下
    可以使用0,0。

  • 也可以使用网格方法,定义您是否希望小部件将
    粘到它被给予的空间。在你的情况下,你这样做,所以你可以给
    字符串nsew(北,南,东,西)。

  • 配置小部件所在的行的权重为1(一)。用
    grid_rowconfigure 来做到这一点。这将导致该行垂直扩展以填充任何额外空间
  • 配置小部件所在的列的权重为1(1)。用
    grid_columnconfigure 来做到这一点。这将导致该列水平扩展以填充
    任何额外的空间。


请注意 grid_rowconfigure grid_columnconfigure 是在包含 文本小部件的
小部件上调用的方法,而不是文本小部件本身。 grid 方法是在文本小部件上调用
,因为您要告诉文本小部件它应该在哪里放置
本身。



您的代码如下所示:

  from tkinter import * 
root = Tk()
textWidget = Text(root)

textWidget.grid(row = 0,column = 0,sticky =nsew)
root .grid_rowconfigure(0,weight = 1)
root.grid_columnconfigure(0,weight = 1)

root.geometry('600x1000')
root.mainloop()

当您有一个用于填充分配给它的所有空间的小部件时,我推荐 pack 只是因为你可以用一行代码而不是三行来完成任何事情。 pack 非常适合这类问题。正如其名称一样, grid 更适合创建小部件网格。话虽如此,无论是完全可以接受的。你可以在同一个程序中混合使用 grid pack ,但是你不能将它们用于小部件分享一个共同的家长。

I know that you can easily create a Tkinter Text widget that covers 100% of the width using the Pack Geometry Manager:

from tkinter import *
root = Tk()
textWidget = Text(root)
textWidget.pack(side=TOP, fill=X)
root.geometry('600x1000')
root.mainloop()

But I was wondering, how do I accomplish this same task using the Grid Geometry Manager with grid_columnconfigure?

解决方案

Using grid requires these steps:

  • use the grid method of the text widget, giving it a row and column. In this case you can use 0,0.
  • also with the grid method, define whether or not you want the widget to "stick" to the sides of the space it was given. In your case you do, so you can give the string "nsew" (north, south, east, west).
  • configure the row that the widget is in to have a weight of 1 (one). Do this with grid_rowconfigure. This will cause the row to expand vertically to fill any extra space
  • configure the column that the widget is in to have a weight of 1 (one). Do this with grid_columnconfigure. This will cause the column to expand horizontally to fill any extra space.

Note that grid_rowconfigure and grid_columnconfigure are methods to be called on the widget that contains the text widget, not on the text widget itself. The grid method is called on the text widget, because you are telling the text widget where it should place itself in its parent.

You code would look like this:

from tkinter import *
root = Tk()
textWidget = Text(root)

textWidget.grid(row=0, column=0, sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

root.geometry('600x1000')
root.mainloop()

When you have a single widget that is to fill all of the space allotted to it, I recommend pack simply because you can do everything with one line of code rather than three. pack is perfect for this type of problem. grid is more suited to creating a grid of widgets, as its name applies. That being said, either is perfectly acceptable. You can mix and match grid and pack within the same program, though you can't use them both for widgets that share a common parent.

这篇关于Python 3 Tkinter - 使用网格创建覆盖100%宽度的文本小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆