简体   繁体   English

如何在python中放置文本框?

[英]How do you position a text box in python?

Could you help me with positioning my textbox named "stuff" (If you have any good tutorials could you post those, too?). 您能帮我放置名为“ stuff”的文本框吗(如果您有任何好的教程,您也可以发布它们吗?)。

import tkinter
from tkinter import font
from datetime import datetime

now = datetime.now()
month = now.month
day = now.day
year = now.year

stuff = "the month is %s the day is %d and they year is %s" % (month, day, 
year)

topwindow = tkinter.Tk()
textbox = tkinter.Text(topwindow)
textbox.configure(font=("Times New Roman", 50, "bold"))
textbox.insert( tkinter.INSERT, str(stuff))
textbox.pack()
topwindow.mainloop()

There are three ways to position widgets in tkinter. 在tkinter中有三种定位小部件的方法。 Tkinter has three geometry managers : pack , place , and grid which are used for placement. Tkinter具有三个几何管理器packplacegrid用于放置。

pack operates on the concept of a box. pack的概念。 you "pack" items to one of the sides of an empty box. 您将物品“包装”到一个空盒子的一侧。 It is best for when you need to place a single widget, or a group of widgets in a single row or a single column. 最适合需要在单个行或单个列中放置单个小部件或一组小部件的情况。

grid does as its name implies, it organizes widgets in a grid. 顾名思义, grid确实可以将小部件组织在一个网格中。 Every widget is given a row and a column, and there are additional options for spanning rows and column, padding, etc. 每个小部件都具有一行和一列,并且还有其他选项可用于跨越行和列,填充等。

place is the least used geometry manager. place是最少使用的几何图形管理器。 Its main advantage is it allows you to do absolute positioning. 它的主要优点是允许您进行绝对定位。 It also allows relative positioning (eg: you can place one widget in the exact middle of another). 它还允许相对定位(例如:您可以将一个小部件放置在另一个小部件的正中间)。 The downsize to using place is that you have to do a lot of the math yourself, and it's much harder to get widgets to behave properly when fonts or resolutions change, or the user changes the size of the window. 缩小使用place是您必须自己做很多数学运算,并且当字体或分辨率更改或用户更改窗口大小时,使小部件正常运行变得困难得多。

The advantage that grid and pack have over place is that they will automatically grow or shrink the window to fit its contents. 其优点在于gridpack有过place是,他们会自动扩大或缩小的窗口,以适应其内容。 This means that you don't much have to worry about the size of your GUI, it will become the size it needs to be. 这意味着您不必担心GUI的大小,它会变成所需的大小。 pack and grid are the best choice if you want your UI to be responsive -- to react properly when run on different systems with different resolutions or fonts, and for when the user resizes the window. 如果您希望UI能够响应,则packgrid是最佳选择-当在具有不同分辨率或字体的不同系统上运行以及用户调整窗口大小时,它可以正确响应。

The tkinter book at effbot.org is one place that has some good basic documentation: effbot.org上的tkinter书是拥有一些良好基础文档的地方:

It's important to note that you can use all three within the same application -- they all have strengths and weaknesses. 重要的是要注意,您可以在同一应用程序中使用所有这三种方法-它们都有优点和缺点。 The only caveat is that you can't use both grid and pack on different widgets that share the same parent (or master, depending on the terminology you prefer). 唯一的警告是,您不能在共享同一父(或母,取决于您喜欢的术语)的不同小部件上同时使用gridpack

只需使用textbox.pack() ,如果您希望将其放置在左侧,请使用textbox.pack(side=LEFT) ,如果要放置在右侧,请使用textbox.pack(side=RIGHT)

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

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