首页 > 解决方案 > Tkinter 管理帧

问题描述

我在我的一个程序中遇到了障碍。以下是我的代码。我创建了一个带有多个框架的 tkinter 窗口应用程序。我能够正确地设置它们,框架内有框架。但是,我不知道为什么添加标签小部件会修改框架的大小。建议将不胜感激。

在以下代码中,请查看取消注释这两个标签小部件之间的区别。

import tkinter as tk

def main():
    print ('Beginning')

    app = tk.Tk()

    size_x = app.winfo_screenwidth()
    size_y = app.winfo_screenheight()

    app.title('Color Mixer')
    app.geometry("%dx%d+0+0" % (size_x, size_y))
    # This disables resizing windows.

    #app.configure(background = 'yellow')

    print ('Beginning')

    #Main containers
    top_frame = tk.Frame(app, bg = 'yellow', width=size_x, height=int(size_y/3),  padx = 3, pady=3)
    bottom_frame = tk.Frame(app, bg='green', width=size_x, height=int(2*size_y/3), padx=3, pady=3)
    # layout all of the main containers
    app.grid_rowconfigure(0, weight=1)
    app.grid_columnconfigure(0, weight=1)

    top_frame.grid(row=0, sticky="nsew")
    bottom_frame.grid(row=1, sticky="nsew")

    # create the widgets for the top frame

    model_label = tk.Label(top_frame, font = ('Times', str(int(size_x*0.014)), 'bold'), bg = 'yellow' , text='Pick a Color Model')
    model_label.grid(row=0, column = 0)
    v = tk.IntVar()
    tk.Radiobutton(top_frame,
                   text="RYB Color Space",
                   indicatoron=0,
                   padx=35,
                   font=('Times',str(int(size_x * 0.0075)), 'normal'),
                   variable=v,
                   value=1).grid(row=1, column=0)
    tk.Radiobutton(top_frame,
                   text="CMYK Color Space",
                   indicatoron=0,
                   padx=20,
                   font=('Times', str(int(size_x * 0.0075)), 'normal'),
                   variable=v,
                   value=2).grid(row=2, column=0)


    top_frame.grid_rowconfigure(0, weight=1)
    top_frame.grid_rowconfigure(1, weight=1)
    top_frame.grid_rowconfigure(2, weight=1)
    top_frame.grid_columnconfigure(0, weight = 1)

 # Bottom Frame - Dividing into two frames.
    bottom_left = tk.Frame(bottom_frame, bg='blue', width=int(0.5*size_x), height=int(2/3*size_y), padx = 3, pady =3)
    bottom_right = tk.Frame(bottom_frame, bg='red', width=int(0.5*size_x), height=int(2/3*size_y), padx=3, pady=3)
    bottom_left.grid(row=0, column=0, sticky="nsew")
    bottom_right.grid(row=0, column=1, sticky="nsew")

    # ryb_note = tk.Label(bottom_left, font=('Times', str(int(size_x * 0.005)), 'normal'), bg='red', text='NOTE')
    # ryb_note.grid(row=0, column=0)
    # ryb_red = tk.Label(bottom_left, font=('Times', str(int(size_x * 0.005)), 'normal'), bg='red', text='Red')
    # ryb_red.grid(row=1, column=0)

    bottom_frame.grid_rowconfigure(0, weight = 1)
    bottom_frame.grid_columnconfigure(0, weight = 1)
    bottom_frame.grid_columnconfigure(1, weight = 1)

    app.mainloop()


if __name__ == '__main__':
    main()

标签: pythontkinter

解决方案


您可以通过添加几个方法调用来防止两个框架受到其内容的影响,grid_propagate()如下所示。这是一些文档

    ...
    # Bottom Frame - Dividing into two frames.
    bottom_left = tk.Frame(bottom_frame, bg='blue', width=int(0.5*size_x), height=int(2/3*size_y), padx = 3, pady =3)
    bottom_right = tk.Frame(bottom_frame, bg='red', width=int(0.5*size_x), height=int(2/3*size_y), padx=3, pady=3)
    bottom_left.grid(row=0, column=0, sticky="nsew")
    bottom_right.grid(row=0, column=1, sticky="nsew")
    bottom_left.grid_propagate(0)   # Added
    bottom_right.grid_propagate(0)  # Added
    ...

推荐阅读