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

Tkinter使用grid_columnconfigure配置不同行列属性及实现指定列扩展

解决方案

你需要调整3处网格配置,即可实现Select Action区域自适应扩展的效果,修改后的核心代码如下:

class RenameFrame():
    def __init__(self):
        pass
    def createRenameFrame(self, win):
        width = ((win.winfo_screenwidth() // 2) - 30)
        height = win.winfo_screenheight() - 105
        R_Frame = Frame(win, width=width, height=height, bg='#222222', highlightcolor='white',relief='solid')
        R_Frame.grid(row = 1, column = 0)
        R_Frame.grid_propagate(0)
        Grid.columnconfigure(R_Frame, 0, weight = 1)
        Grid.rowconfigure(R_Frame, 0, weight = 1)
        win.update_idletasks() 
        R_FrameContent = LabelFrame(R_Frame, text='Bunch Renamer', width=width, height = height, bg='#222222', fg='white',
            font=('Flux Regular', 20, 'bold'))
        R_FrameContent.grid(padx=10, pady=10, ipady=20, ipadx=20)
        R_FrameContent.grid_propagate(0)
        # R_FrameContent 配置修改:新增行1的权重配置
        Grid.columnconfigure(R_FrameContent, 1, weight = 2)
        Grid.rowconfigure(R_FrameContent, 1, weight=1)
        # 第一行代码不变
        lb = Label(R_FrameContent, text='Selected Folder: ',bg='#222222', fg='white', font=('Flux Regular', 10, 'bold'),
            width='15')
        lb.grid(row=0, column=0, pady=5, padx=2)
        pathVal = StringVar()
        pathVal.set("No Folder Selected")
        entry = Entry(R_FrameContent, textvariable=pathVal, state='disabled', bd=0, relief = 'solid')
        entry.grid(row=0, column=1, padx=2, ipadx=5, ipady=2 , sticky="ew")
        browseButton = Button(R_FrameContent, text = 'Browse', activebackground='tomato', activeforeground='white', bd=0, relief='solid',
            bg='tomato',fg='white', highlightbackground='tomato', font=('Flux Regular', 10, 'bold'), width=10 )
        browseButton.grid(row=0, column=2, pady=5, padx=2)
        # 第二行代码修改
        actionSelectionFrame = LabelFrame(R_FrameContent, text = 'Select Action', labelanchor = 'n', bg='#222222', fg='white',
            font=('Flux Regular', 10, 'bold'))
        # grid新增sticky="nsew"参数,让框架填充分配到的单元格
        actionSelectionFrame.grid(row = 1, column = 0, columnspan = 3, padx=15, pady= 5, sticky="nsew")
        # 新增:给actionSelectionFrame内部三列设置相同权重,实现单选按钮均匀分布
        Grid.columnconfigure(actionSelectionFrame, 0, weight=1)
        Grid.columnconfigure(actionSelectionFrame, 1, weight=1)
        Grid.columnconfigure(actionSelectionFrame, 2, weight=1)
        status = IntVar()   
        rb1 = Radiobutton(actionSelectionFrame, text='Default Prefix', variable=status, font=('Flux Regular', 10, 'bold'), value= 1)
        rb1.grid(row=0, column=0, ipady=5, ipadx=5, padx=5, pady=(10, 10))
        rb2 = Radiobutton(actionSelectionFrame, text='Custom Prefix', variable=status, font=('Flux Regular', 10, 'bold'), value= 2)
        rb2.grid(row=0, column=1, ipady=5, ipadx=5, padx=5, pady=(10, 10))
        rb3 = Radiobutton(actionSelectionFrame, text='Custom File Name ', variable=status, font=('Flux Regular', 10, 'bold'), value= 3)
        rb3.grid(row=0, column=2, ipady=5, ipadx=5, padx=5, pady=(10, 10))
        return R_Frame

注:顺带修正了原代码中三个单选按钮value值相同的问题,避免选择逻辑异常

grid_rowconfigure与grid_columnconfigure使用规则

  • 作用对象:两个方法均作用于父容器,用于配置父容器内部网格的行/列空间分配规则,而非直接作用于子组件。
  • weight参数:代表当前行/列可分配到的父容器剩余空间的比例,默认值为0代表不占用剩余空间。例如同个父容器下两列weight分别设为1和2,剩余宽度会按1:2的比例分配给两列。
  • 配合规则:子组件调用grid()方法时需要设置sticky="nsew",才会自动填充分配到的单元格空间,否则仅父容器行/列设置weight不会触发子组件扩展。
  • 跨行列适配:跨列/跨行放置的子组件,可占据的总空间为所跨所有行/列分配到的空间总和。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 10:45:07