使用tkinter .pack()无法显示左下角Frame的问题求助
问题原因
你误解了pack(side=tk.LEFT)的布局逻辑:设置side=LEFT的组件会在父容器中水平靠左排列,后续同side的组件会紧贴前一个组件的右侧,而不是垂直向下堆叠。
你的leftFrame(宽400)和rightFrame(宽1460)加上左右padding后,几乎占满了mainFrame的总宽度(1900),导致bottomLeftFrame没有剩余水平空间可以显示,所以被完全挤压隐藏了。
解决方案
要实现左侧区域上下两个Frame的布局,需要先创建一个中间容器Frame,把leftFrame和bottomLeftFrame放到这个容器里,让它们在容器内垂直堆叠,再将这个中间容器整体放到mainFrame的左侧。
修改后的代码如下:
import tkinter as tk import customtkinter window = customtkinter.CTk() window.geometry("1900x980") mainFrame = customtkinter.CTkFrame(window, width=1900, height=980) mainFrame.pack() mainFrame.pack_propagate(0) topFrame = customtkinter.CTkFrame(mainFrame, width=1870, height=110, corner_radius=10) topFrame.pack(side=tk.TOP, padx=10, pady=10) topFrame.pack_propagate(0) # 创建左侧容器,用来放上下两个Frame leftContainer = customtkinter.CTkFrame(mainFrame, width=400, height=850, corner_radius=0) leftContainer.pack(side=tk.LEFT, padx=15, pady=5) leftContainer.pack_propagate(0) # 左侧上方Frame leftFrame = customtkinter.CTkFrame(leftContainer, width=400, height=825, corner_radius=10) leftFrame.pack(side=tk.TOP, pady=(0,5)) leftFrame.pack_propagate(0) # 左侧下方Frame bottomLeftFrame = customtkinter.CTkFrame(leftContainer, width=400, height=20, corner_radius=10, bg_color="red") bottomLeftFrame.pack(side=tk.TOP) bottomLeftFrame.pack_propagate(0) rightFrame = customtkinter.CTkFrame(mainFrame, width=1460, height=825, corner_radius=10) rightFrame.pack(side=tk.RIGHT, padx=15, pady=5) rightFrame.pack_propagate(0) window.mainloop()
关键修改说明
- 新增
leftContainer作为左侧区域的统一容器,设置合适的高度(825+20+5间距=850),确保能容纳内部两个Frame leftFrame和bottomLeftFrame在leftContainer内用side=tk.TOP垂直堆叠,实现上下排列- 将
leftContainer整体设置为side=tk.LEFT,放到mainFrame的左侧区域
内容的提问来源于stack exchange,提问作者Dominic Culyer
相关产品推荐
相关产品推荐

