Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一些基本的tkinter问题_Python_Tkinter - Fatal编程技术网

Python 一些基本的tkinter问题

Python 一些基本的tkinter问题,python,tkinter,Python,Tkinter,关于几何体管理器,我有几个问题: (a) 网格几何体管理器如何确定行和列?我的意思是,grid manager如何将主小部件索引到行和列中 关于这一点,我有很多困惑-当您键入a.grid(row=100,column=100)和a.grid(row=0,column=0)时,结果完全相同 另外,您可以使用网格将小部件放在最右上方,将小部件放在最左下方,将中间空间清空吗 (b) 在packgeometry manager中fill和expand之间有什么区别 另外,当我创建a=Text(root,

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

(a)
网格
几何体管理器如何确定行和列?我的意思是,grid manager如何将主小部件索引到行和列中

关于这一点,我有很多困惑-当您键入
a.grid(row=100,column=100)
a.grid(row=0,column=0)
时,结果完全相同

另外,您可以使用
网格
将小部件放在最右上方,将小部件放在最左下方,将中间空间清空吗

(b) 在
pack
geometry manager中
fill
expand
之间有什么区别


另外,当我创建
a=Text(root,width=100,height=100)
,然后键入
a.pack(side=TOP)
a.pack(side=LEFT)
a.pack(side=RIGHT)
,为什么文本框总是位于页面的底部
方法如果您在位置
(0,0)
创建一个小部件,并且下一个小部件位于位置
(100,100)
,那么系统将把第二个小部件放在看起来是
(1,1)
的位置。这是因为第二个小部件依次处于“下一个位置”。你可以认为
(1,1)
(99,99)
的位置仍然存在,但是非常小,所以
(100100)
看起来就在
(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()
将对象放置在相对的角落,这是可能的,并且可以使用
行配置
列配置
权重
属性以及在小部件上使用
.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()
的基础知识:

把它扔进你选择的编辑器中,然后玩转它


我还想指出,堆栈溢出并不是解决此类问题的好地方,我建议您在使用SO之前,查找您想知道的文档

例如,我认为最好的tkinter文档是:

a)网格
几何图形管理器仅为实际包含某些内容的行和列分配空间。因此,如果您只有一个小部件,那么无论您选择哪一行和哪一列,网格实际上只有一个单元格(也就是说,除非您使用
grid\u rowconfigure
grid\u columnconconfigure
指定其他选项,更多信息请参见和)

b) 使用
pack
geometry manager时,
fill
选项指示小部件必须扩展其自身大小以填充由geometry manager分配的空间。如果您没有指定它,您可能会在小部件周围有一些空白,例如,如果您将几个小部件放在一行中,其中一些小部件比另一个高。另一方面,
展开
选项会影响几何体管理器分配给小部件的空间量;如果您不指定它,它将接收尽可能少的空间量,否则任何剩余空间将分布在所有设置了
expand
set(更多信息)的小部件中。所以它们是真正独立的东西,一个影响小部件本身的大小,另一个影响它接收的窗口空间量


至于你的最后一个问题,也许你可以提供一个演示你的问题的例子。

我将尽我所能解释是什么导致你对几何体管理器产生这种困惑

(a) 栅格几何图形管理器如何确定行和列?我的意思是,grid manager如何将主小部件索引到行和列中

行和列的确定与您预期的一样。您对
a.grid(row=100,column=100)
a.grid(row=0,column=0)
显示相同的问题是因为行和列的性质。如果一行或一列中没有任何小部件,那么该行或列将崩溃为零。直到有东西被放在里面

因此,当您定义一个网格,使其在say
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()
在包几何体管理器中填充和展开有什么区别

这已经被问了好几次了,这里有一个关于这个主题的链接

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

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

expand
选项告诉管理员填充给定fra中所有小部件之间的所有可用空间
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()
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()