首页 > 解决方案 > tkinter 中的网格和打包顺序

问题描述

我从 Tkinter 开始,并尝试了解不同的几何包管理器。我发现了一些奇怪的东西,我想要一个解释。

我尝试打印这个窗口:
在此处输入图像描述

我测试了两段代码:

import tkinter as tk
fen = tk.Tk() 
fr1 = tk.Frame(fen).grid(row=0,column=0)
fr2 = tk.Frame(fen).grid(row=0,column=1)

txt1 = tk.Label(fr1, text="txt1").pack(side="top")
tk.Frame(fr1,bg="blue",height=5).pack(side="bottom",fill="x")

txt2 = tk.Label(fr2, text="txt2").pack(side="top")
tk.Frame(fr2,bg="red",height=5).pack(side="bottom",fill="x")

import tkinter as tk
fen = tk.Tk() 
fr1 = tk.Frame(fen)
fr2 = tk.Frame(fen)

txt1 = tk.Label(fr1, text="txt1").pack(side="top")
tk.Frame(fr1,bg="blue",height=5).pack(side="bottom",fill="x")

txt2 = tk.Label(fr2, text="txt2").pack(side="top")
tk.Frame(fr2,bg="red",height=5).pack(side="bottom",fill="x")

fr1.grid(row=0,column=0)
fr2.grid(row=0,column=1)

只是调用网格的位置不同。

第一个给我一个几何错误:

“不能在里面使用几何管理器包。它已经有网格管理的奴隶”

第二个有效。

为什么会有这种行为差异?

标签: pythontkinter

解决方案


推荐阅读