一些基本的tkinter问题

时间:2017-09-06 08:36:20

标签: python tkinter

我有几个关于几何管理器的问题:

(a)grid几何管理器如何确定行和列?我的意思是,网格管理器如何将主窗口小部件编入行和列?

我对此有很多困惑 - 当您输入a.grid(row=100,column=100)a.grid(row=0, column=0)时,结果完全一样!

此外,您是否可以使用grid将小部件放在最右上方,将小部件放在最左边,并将middlespace放空?

(b)fill几何管理器中expandpack之间的区别是什么?

此外,当我创建a = Text(root, width=100, height=100),然后输入a.pack(side=TOP)a.pack(side=LEFT)a.pack(side=RIGHT)时,为什么文本框始终位于底部页面?

3 个答案:

答案 0 :(得分:1)

a)grid几何管理器仅为实际具有某些内容的行和列分配空间。因此,如果您只有一个窗口小部件,那么您选择的行和列无关紧要,网格将只有一个单元格(即,除非您使用grid_rowconfiguregrid_columnconfigure指定其他选项,否则更多关于this tutorialhere)中的内容。

b)使用pack几何管理器,fill选项表示它必须扩展自己的大小以填充几何管理器分配的空间的小部件。如果您没有指定它,您可能在窗口小部件周围有一些空白空间,例如,如果您连续放置几个小部件而某些小部件比其他小部件更高。另一方面,expand选项会影响几何管理器分配给窗口小部件的空间量;如果您没有指定它,它将获得尽可能少的空间,否则任何剩余空间将分配给expand设置的所有小部件(更多信息here)。所以它们是独立的东西,一个影响小部件本身的大小,另一个影响它接收的窗口空间量。

至于你的上一个问题,也许你可以提供minimal example来表明你的问题。

答案 1 :(得分:0)

基本上,使用.grid()方法,如果您在位置(0, 0)创建小部件而下一个小部件位于(100, 100)位置,那么系统会将第二个小部件放在显示的位置是(1, 1)。这是因为顺序地第二小部件处于“下一个位置”。您可以认为(1, 1)(99, 99)的位置仍然存在,但是非常小,所以(100, 100)看起来就像(0, 0)旁边,请参阅下面的示例:

from tkinter import *

root = Tk()

label1 = Label(root, text="Col0, Row0")
label2 = Label(root, text="Col100, Row100")

label1.grid(column=0, row=0)
label2.grid(column=100, row=100)

root.mainloop()

关于能够使用.grid()将对象放置在对角中,这是可能的,并且可以使用rowconfigurecolumnconfigure的{​​{1}}属性的组合来完成,以及在窗口小部件上使用weight时的sticky属性。见下文:

.grid()

如果您正在寻找有关from tkinter import * root = Tk() label1 = Label(root, text="1") label2 = Label(root, text="2") label1.grid(column=0, row=0, sticky="NW") label2.grid(column=1, row=1, sticky="SE") root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) #Giving the rows and columns a weight that is not equal to 0 #means that the rows and columns expand to fill the space in the window. #Then we assign a sticky value to the labels, #to position them where we need them in the cell. root.columnconfigure(1, weight=1) root.rowconfigure(1, weight=1) root.mainloop() 的不同属性如何工作的更多信息,我前段时间写了这个作为另一个问题的答案,它应该有助于使用.pack()的基础知识:

.pack()

将它扔进你选择的编辑器中并随意使用它。

我还想指出Stack Overflow并不是真正适合这样的问题的地方,我建议在使用SO之前找一些你想知道的文档。

例如,最好的tkinter文档(在我看来)是: http://effbot.org/tkinterbook/

答案 2 :(得分:0)

我将尽我所能来解释正在发生的事情,这会导致您对几何管理器产生混淆。

  

(a)网格几何管理器如何确定行和列?我的意思是,网格管理器如何将主窗口小部件编入行和列?

行和列的确定与您期望的一样。 a.grid(row=100,column=100)a.grid(row=0, column=0)出现问题的问题是因为行和列的性质。如果行或列中没有任何小部件,那么该行或列将只会折叠为空。直到这个时间放在里面。

因此,当您定义一个网格以使某个窗口小部件位于row=1, column=1时,您会期望得到这样的内容。

*--------*--------*
|        |        |
|        |        |
|        |        |
*--------*--------*
|        | row=1  |
|        | column |
|        |  =1    |
*--------*--------*

但是因为第0行和第0列中没有小部件,所以它们会明显崩溃到没有任何东西,你得到这个:

*--------*
| row=1  |
| column |
|  =1    |
*--------*

其他3个细胞仍然存在,它们只是没有空间被填充,所以它们尽可能小,结果明显缺失细胞。

  

你可以使用网格将一个小部件放在最右上方,一个小部件放在最左边,中间空间是空的吗?

是的,你可以。同样的事情将发生在上面,并且看起来中间单元格已经消失,但是您可以向网格中添加更多参数以允许它仍然显示为3乘3.例如,请参见下面的代码:

我们可以更改行或列的权重来控制网格行为。

from tkinter import *

class Application():
    def __init__(self, master):

        self.master = master
        self.master.geometry("400x400")
        # the below column and row weights allow us to control the behavior of the grid.
        # setting the weight to 0 means the row or column will not resize at all
        # setting the number to 1 or higher will allow the row or column to resize with the window
        # what this does is allow us to keep widgets where we want them by manipulating the weights.
        self.master.columnconfigure(0, weight = 0)
        self.master.columnconfigure(1, weight = 1)
        self.master.columnconfigure(2, weight = 0)
        self.master.rowconfigure(0, weight = 0)
        self.master.rowconfigure(1, weight = 1)
        self.master.rowconfigure(2, weight = 0)

        top_left_label = Label(self.master, text="I am at the top left")
        top_left_label.grid(row=0, column=0)

        bottom_right_label = Label(self.master, text="I am at the bottom right")
        bottom_right_label.grid(row=2, column=2)

root = Tk()
Application(root)
root.mainloop()
  

包几何管理器中填充和展开之间有什么区别?

这已被问过几次,这里只是关于该主题的Question/Answer的链接。

以下是与该主题相关的简短回答:

基本上fill选项告诉tkinter你要填充小部件可用空间的x轴或y轴或两个方向。这意味着如果一个框架具有一个设置的大小,并且您使用fill = BOTH选项在该框架中放置一个按钮,那么该按钮将填充该框架的大小,如果该框架被调整大小,那么该按钮也将如此。

expand选项告诉管理员填充给定框架/窗口中所有窗口小部件之间的所有可用空间。因此,如果其他窗口小部件也分配了扩展选项,它们将均匀地共享空间。请记住,这是一个简单的解释,您应该阅读文档以了解全貌。

  

当我创建一个= Text(root,width = 100,height = 100),然后键入a.pack(side = TOP)或a.pack(side = LEFT)或a.pack(side = RIGHT),为什么文本框总是位于页面底部?

简单的答案是它没有。

如果你看到这个,那么它是由其他东西引起的,而不是你打包小部件的方式。

查看下面的示例代码,并调整窗口大小。您将看到每个包选项的工作原理。

from tkinter import *

class Application():
    def __init__(self, master):

        self.master = master
        self.master.geometry("400x400")
        Text(root, width=10, height=4).pack(side=TOP)
        Text(root, width=10, height=4).pack(side=LEFT)
        Text(root, width=10, height=4).pack(side=RIGHT)
        Text(root, width=10, height=4).pack(side=BOTTOM)


root = Tk()
Application(root)
root.mainloop()

结果:

enter image description here

如果您还有其他问题,请与我们联系。

如果有人希望我扩展我的任何答案,请告诉我,我会添加它。