Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 打包tkinter小部件会破坏框架_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 打包tkinter小部件会破坏框架

Python 3.x 打包tkinter小部件会破坏框架,python-3.x,tkinter,Python 3.x,Tkinter,因此,每当我初始化Gui类,然后将标签和按钮小部件打包成漂亮的框架样式时,我都会使用它们以中断形式存在的框架进行配置。为什么会这样?如果你注释掉按钮和标签小部件包,它正是我想要的样子 class Gui(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.parent = parent

因此,每当我初始化Gui类,然后将标签和按钮小部件打包成漂亮的框架样式时,我都会使用它们以中断形式存在的框架进行配置。为什么会这样?如果你注释掉按钮和标签小部件包,它正是我想要的样子

class Gui(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.parent.wm_title("Conflict found")
        self.TV = 'Hello'
        # Creation
        self.statusbar = statusbar(self, bg="black", height=100, width=300)
        self.main = Main(self, bg="grey", height=50, width=300)

        # Packing
        self.statusbar.pack(side="top", expand=True)
        self.statusbar.label.pack()
        self.main.pack(side="bottom", expand=True)
        self.main.button.pack()

    def quit(self):
        self.parent.destroy()


class Main(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.button = tk.Button(self, text="Exit", command=self.quit)


class statusbar(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.label = tk.Label(self, text=self.parent.TV, fg='white')

帧的
高度=
宽度=
通常仅在帧为空时适用。一旦您向它添加任何子窗口小部件,它的大小就会被重新计算为容纳所有子窗口小部件所需的最小值。为了避免这种情况,您可以在框架上调用
.pack\u propagate(0)
(或者如果您在子框架上使用
.grid()
),框架的
高度=
宽度=
通常仅在框架为空时才适用。一旦您向它添加任何子窗口小部件,它的大小就会被重新计算为容纳所有子窗口小部件所需的最小值。为了避免这种情况,您可以在框架上调用
.pack\u propagate(0)
(或者如果您在子对象上使用
.grid()
,则调用
.grid\u propagate(0)
)。

简短的回答是tkinter就是这样设计的。使用
pack
grid
时,tkinter将使父窗口增大或缩小以适合其子窗口

这是您99.999%的时间所需要的,因为它允许您根据字符大小(或像素,当它重要时,例如图像)关注小部件的大小,并且GUI的大小正好合适

当您为框架和窗口选择特定大小时,如果用户具有不同的字体、不同的操作系统、不同分辨率的显示器,或者用户试图手动增大或缩小窗口,则程序的外观将不正确

有很多方法可以避免这种行为,但您几乎不想关闭这种行为。最好的解决方案是专注于内部部件,让tkinter担心窗口大小


不要认为“我需要一个100像素高、300像素宽的状态栏”,而应该认为“我需要一个至少可以容纳20个字符的状态栏,并水平填充窗口”。然后,无论您使用何种字体、分辨率或操作系统,Tkinter都会做正确的事情。

简而言之,Tkinter就是这样设计的。使用
pack
grid
时,tkinter将使父窗口增大或缩小以适合其子窗口

这是您99.999%的时间所需要的,因为它允许您根据字符大小(或像素,当它重要时,例如图像)关注小部件的大小,并且GUI的大小正好合适

当您为框架和窗口选择特定大小时,如果用户具有不同的字体、不同的操作系统、不同分辨率的显示器,或者用户试图手动增大或缩小窗口,则程序的外观将不正确

有很多方法可以避免这种行为,但您几乎不想关闭这种行为。最好的解决方案是专注于内部部件,让tkinter担心窗口大小

不要认为“我需要一个100像素高、300像素宽的状态栏”,而应该认为“我需要一个至少可以容纳20个字符的状态栏,并水平填充窗口”。Tkinter会做正确的事情,不管你使用什么字体、分辨率或操作系统