You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter/TTKbootstrap窗口布局问题:双标签各占半屏失败

问题分析与解决方案

你的代码无法实现两个标签并排各占窗口一半宽度,核心问题在于没有正确配置Grid布局的权重与控件扩展属性,同时存在一些细节错误,以下是具体修正方案:

关键问题点

  • 根窗口未设置列权重,Grid布局无法自动为列分配空间
  • 承载Label的Frame未设置sticky属性和填充规则,无法占据分配的空间
  • Label在Frame内未设置扩展属性,导致Frame仅包裹Label的最小尺寸
  • 存在拼写错误:bootstyle="sucess" 应改为 bootstyle="success"
  • 背景Label使用place后,需确保其他控件显示在其上方

修正后的代码

from textwrap import fill
import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from PIL import Image, ImageTk


class APP:

    def __init__(self):
        # Criando a janela principal
        self.root = tk.Tk()
        self.root.title("FMD")
        self.root.geometry("1080x600")  # Define o tamanho da janela
        # Impede o redimensionamento da janela
        self.root.resizable(False, False)

        # 配置根窗口的列权重:两列各占50%宽度
        self.root.grid_columnconfigure(0, weight=1)
        self.root.grid_columnconfigure(1, weight=1)
        # 配置行权重(可选,若后续需要垂直扩展)
        self.root.grid_rowconfigure(0, weight=1)

        # Carregando a imagem existente
        background_image = Image.open("background.png")

        # Convertendo a imagem para o formato Tkinter PhotoImage
        self.background_photo = ImageTk.PhotoImage(background_image)

        # Criando um rótulo com a imagem como plano de fundo
        self.background_label = tk.Label(
            self.root, image=self.background_photo)
        self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
        # 将背景标签置于底层,确保其他控件可见
        self.background_label.lower()

        # Criando o frame do Dataset
        self.dataset_frame = ttk.Frame(self.root, bootstyle="dark")
        # 设置Frame在网格中填充整个单元格,四边扩展
        self.dataset_frame.grid(row=0, column=0, sticky="nsew", padx=5, pady=5)
        # 配置Frame的列权重,让Label能占满Frame宽度
        self.dataset_frame.grid_columnconfigure(0, weight=1)

        self.dataset_label = ttk.Label(
            self.dataset_frame, text="Dataset:", bootstyle="inverse-dark")
        self.dataset_label.configure(font=("Arial", 16, "bold"))
        # 设置Label填充整个Frame,水平居中显示文本
        self.dataset_label.pack(fill="both", expand=True)

        # Criando o frame do preview
        self.preview_frame = ttk.Frame(self.root, bootstyle="dark")
        # 同样设置Frame填充单元格
        self.preview_frame.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
        self.preview_frame.grid_columnconfigure(0, weight=1)

        self.preview_label = ttk.Label(
            self.preview_frame, text="Preview:", bootstyle="success")  # 修正拼写错误
        self.preview_label.configure(font=("Arial", 16, "bold"))
        self.preview_label.pack(fill="both", expand=True)

        # Inicia o loop principal da aplicação
        self.root.mainloop()


# Inicializa a aplicação
app = APP()

核心修改说明

  1. 根窗口列权重配置:grid_columnconfigure 设置两列权重均为1,确保窗口宽度被平均分配给两列
  2. Frame的sticky属性:sticky="nsew" 让Frame在上下左右四个方向填充所在的网格单元格
  3. Frame内部列权重:为每个Frame配置列权重,让Label能扩展填充整个Frame
  4. Label的pack参数:fill="both", expand=True 让Label填充整个Frame空间,同时文本保持居中
  5. 背景层级调整:background_label.lower() 确保背景图片不会遮挡其他控件

内容的提问来源于stack exchange,提问作者ItzChadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:24:57