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

Tkinter开发求助:添加StatusBar类及设置Grid列为边框的方法

问题解决方案

1. 解决布局冲突问题

报错原因是同一个父容器(主窗口)不能混合使用pack和grid两种布局管理器:StatusBar通过pack挂载到主窗口,而MainFrame通过grid挂载,两者规则冲突。

修复方法:统一主窗口内组件的布局方式,把MainFrame的布局改为pack,替换MainFrame类末尾的self.grid():

# 替换原self.grid()为:
self.pack(fill=tk.BOTH, expand=True)

这样主窗口里的MainFrame会占满除状态栏外的所有空间,StatusBar固定在底部,布局管理器统一为pack,冲突即可解决。

2. 防止第0列折叠

第0列折叠是因为该列没有放置任何组件,Tkinter会自动收缩无内容的列。给第0列设置**最小宽度(minsize)**即可让它保持边框效果:

修改MainFrame类里的列配置:

# 原配置
# self.columnconfigure(0, weight=4)
# 修改为:
self.columnconfigure(0, weight=4, minsize=100)
# 同理给第5列也设置minsize,保持左右边框对称
self.columnconfigure(5, weight=4, minsize=100)

minsize参数强制列保持指定的最小宽度,即使没有内容也不会折叠;weight参数保证窗口缩放时列按比例调整。


修改后的完整代码

#!/usr/bin/python3

import getpass
import subprocess
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk,Image  
from datetime import datetime
from tkinter import filedialog


class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        
        # 配置网格 - 修复第0、5列折叠问题
        self.columnconfigure(0, weight=4, minsize=100)
        self.columnconfigure(1, weight=2)
        self.columnconfigure(2, weight=2)
        self.columnconfigure(3, weight=2)
        self.columnconfigure(4, weight=2)
        self.columnconfigure(5, weight=4, minsize=100)

        self.createwidgets()
    
    def createwidgets(self):

        ### 文件路径输入框
        self.entry1_var = tk.StringVar()
        self.entry1=tk.Entry(self, textvariable=self.entry1_var, justify='center')
        self.entry1.grid(row=0, column=1, columnspan=4, sticky=tk.EW, padx=0, pady=40, ipady=10 )

        ##--------------------------------------------------##

        ### 选择文件按钮
        self.button1=tk.Button(self, text='FILE', bg="#873600", bd=4, relief="raised")
        #self.button1['command'] = self.openfile
        self.button1.grid(column=1, row=1, padx=0, pady=0, ipadx=30)
        
        ### 查看信息按钮
        self.button2=tk.Button(self, text='INFO', bg="#873600", bd=4, relief="raised")
        #self.button2['command'] = self.exif_info
        self.button2.grid(column=2, row=1, padx=0, pady=0, ipadx=30)
        
        ### 查看EXIF按钮
        self.button3=tk.Button(self, text='EXIF', bg="#873600", bd=4, relief="raised")
        #self.button3['command'] = self.exif_clean
        self.button3.grid(column=3, row=1, padx=0, pady=0, ipadx=30)

        ### 退出按钮
        self.button4=tk.Button(self, text='EXIT', bg="#873600", bd=4, relief="raised")
        #self.button4['command'] = container.exit
        self.button4.grid(column=4, row=1, padx=0, pady=0, ipadx=30)

        ##--------------------------------------------------##

        ### EXIF信息显示框
        self.text1=tk.Text(self, height=12)
        self.text1.grid(row=3, column=1, columnspan=4, padx=0, pady=40, ipady=10)
        
        # 修复布局冲突:替换grid为pack
        self.pack(fill=tk.BOTH, expand=True)
    
class App(tk.Tk):
    def __init__(self):
        super().__init__() 

        bg_colour = "#4C4E52" 

        self.geometry("1400x870")
        #self.configure(bg=bg_colour)
        self.resizable(False, False)
        self.title('EXIF CLEANER v1')

    def exit(self):
        self.destroy()

    
class StatusBar(tk.Frame):   
    def __init__(self, container):
        super().__init__(container)

        time_date = datetime.now().strftime('%I:%M:%S %p on %A, %B the %dth, %Y')
        
        self.variable=tk.StringVar()
        self.variable.set(time_date)     
        
        self.statusbar=tk.Label(self, bd=0, relief="solid", height="2", width="100", 
            textvariable=self.variable, foreground="red", background='#2f2f2f', font=('helvetica',8))                       
        
        self.statusbar.pack(side=tk.BOTTOM, fill=tk.X)
        self.pack(fill="x", side="bottom", ipady=0, padx=0, pady=0)


if __name__ == "__main__":
    app = App()
    StatusBar(app)  # 取消注释后不再报错
    MainFrame(app)
    app.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 15:43:10