tkinter frame propagate not behaving?

前端 未结 2 1412
清歌不尽
清歌不尽 2021-01-29 11:02

If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_ti

相关标签:
2条回答
  • 2021-01-29 11:32

    Ok after some digging I realized the problem was not with your options_frame_title but with your frames the Label was being placed in.

    Of you un-comment options_frame_title and comment out preview_frame_title you will see the exact same problem. What is happening is the frame has a set size and the main window is conforming to that frame size. And when you decide to place a label into the frame then the frame will conform to the label size.

    What you want to do to achieve the look you are going for is do something a little different with the .grid_propagate(0) than what you are currently doing.

    We also need to add some weights to the correct frames so the widgets will fill properly.

    Take a look at this code.

    from tkinter import *
    
    blank_app = Tk()
    
    main_frame = Frame(blank_app,width=700, height=300, bg='gray22')
    main_frame.grid(row=0, column=0, sticky=NSEW)
    
    main_frame.grid_propagate(0) #the only place you need to use propagate(0) Thought there are better ways
    
    main_frame.columnconfigure(0, weight = 1) #using weights to manage frames properly helps a lot here
    main_frame.columnconfigure(1, weight = 1)
    main_frame.rowconfigure(0, weight = 0)
    main_frame.rowconfigure(1, weight = 1)
    
    main_title = Label(main_frame, text='App Builder', bg='gray', fg='red', font='Times 12 bold', relief=RIDGE)
    main_title.grid(row=0, column=0, columnspan=2, padx=2, pady=2, sticky=NSEW)
    
    preview_frame = Frame(main_frame, bg='red', highlightcolor='white', highlightthickness=2)
    preview_frame.grid(row=1, column=0, padx=2, pady=2, sticky=NSEW)
    preview_frame.columnconfigure(0, weight = 1)# using weights to manage frames properly helps a lot here
    
    preview_frame_title = Label(preview_frame, text='Preview Window', bg='gray', fg='blue', relief=RIDGE)
    preview_frame_title.grid(row=0, column=0, sticky=NSEW)
    
    options_frame = Frame(main_frame, bg='blue', highlightcolor='white', highlightthickness=2)
    options_frame.grid(row=1, column=1, padx=2, pady=2, sticky=NSEW)
    options_frame.columnconfigure(0, weight = 1) #using weights to manage frames properly helps a lot here
    
    options_frame_title = Label(options_frame, text='Widget Options', bg='gray', fg='blue', anchor=CENTER, relief=RIDGE)
    options_frame_title.grid(row=0, column=0, sticky=NSEW)
    
    blank_app.mainloop()
    
    0 讨论(0)
  • 2021-01-29 11:53

    I don't understand what you mean by "behaving properly". It seems to be behaving as it's designed to behave.

    By default, tkinter frames are designed to shrink (or grow) to fit their child widgets. When you comment out options_frame_title.grid(...), the frame has no visible children so it says the fixed size that you gave it. When you uncomment that line, it causes a label to be placed in the widget which then causes the frame to shrink to fit.

    To further complicate the matters for you, grid will by default give any extra space to rows and columns that have a non-zero weight. Since you haven't given any rows or columns any weight, they don't get any extra space.

    Part of the problem is that you are trying to solve too many problems at once. When first starting out you need to be more methodical. Also, you should consider using pack when you're putting a single widget into another widget. It only takes one line of code to get it to fill its parent rather than three with grid.


    pro-tip: it really helps if you separate widget creation from widget layout. Your code, even though it's only a couple dozen lines long, is really hard to read.


    For example, the first thing you should do is start by creating your top-most frames, and get them to fill and expand/shrink properly before putting any widgets in them.

    Starting from scratch

    Step 0: don't remove the ability to resize the window

    User's don't like having control taken away from them. Remove this line:

    blank_app.resizable(width=False, height=False)
    

    Your users will thank you, and during development it's much easier to play with the window to make sure everything is filling, growing, and shrinking as necessary.

    Step 1: main_frame

    Since it appears this is designed to contain everything, it makes sense to use pack since it is the only widget directly in blank_app.

    main_frame = Frame(blank_app, width=750, height=500, bg='gray22')
    main_frame.pack(fill="both", expand=True)
    

    With just that (plus the first couple of lines where you create the root window, along with the final call to mainloop), notice how the window is the right size, and the main frame fills the window. You can resize the window all you want and the main frame will continue to fill the whole window.

    Step 2: widgets inside main_frame

    As I mentioned earlier, it's best to separate widget creation and widget layout. Also, when using grid a good rule of thumb is to always give at least one row and one column a weight. It appears you want the right frame to be about 3x as wide as the left frame. This is where you can use weights.

    # widgets in the main frame
    main_title = Label(main_frame, text='App Builder', bg='gray', fg='red', font='Times 12 bold', relief=RIDGE)
    preview_frame = Frame(main_frame, width=70, height=465, bg='red', highlightcolor='white', highlightthickness=2)
    options_frame = Frame(main_frame, width=240, height=465, bg='blue', highlightcolor='white', highlightthickness=2)
    
    # laying out the main frame
    main_frame.grid_rowconfigure(1, weight=1)
    main_frame.grid_columnconfigure(0, weight=1)
    main_frame.grid_columnconfigure(1, weight=3)
    
    main_title.grid(row=0, column=0, padx=2, pady=2, sticky="nsew", columnspan=2)
    preview_frame.grid(row=1, column=0, padx=2, pady=2, sticky="nsew")
    options_frame.grid(row=1, column=1, padx=2, pady=2, sticky="nsew")
    

    Once again, run the code and notice that as you resize the main window everything still continues to fill the window, and resize properly, and keep the proper proportions. If you don't like the proportions, just change the weights. They can be any number you want. For example, you could use 70 and 240 if you want.

    Step 3: preview frame

    The preview frame has a label, and I presume you will be putting other stuff under the label. We'll continue to use grid, and just give the row below the label a weight so that it gets all of the extra space. When you add more widgets, you might need to adjust accordingly.

    # widgets in the preview frame
    preview_frame_title = Label(preview_frame, text="Preview Window", bg='gray', fg='blue', relief=RIDGE)
    
    # laying out the preview frame
    preview_frame.grid_rowconfigure(1, weight=1)
    preview_frame.grid_columnconfigure(0, weight=1)
    preview_frame_title.grid(row=0, column=0, sticky="nsew")
    

    Step 4: the options frame

    This is just like the preview frame: a label at the top, and all of the extra space is given to the empty row number 1.

    # widgets in the options frame
    options_frame_title = Label(options_frame, text='Widget Options', bg='gray', fg='blue', anchor=CENTER, relief=RIDGE)
    
    # laying out the options frame
    options_frame.grid_rowconfigure(1, weight=1)
    options_frame.grid_columnconfigure(0, weight=1)
    options_frame_title.grid(row=0, column=0, sticky="new")
    

    Final thoughs

    Notice that you don't need to worry about propagation, which is somewhat of an advanced topic. You also don't have to worry about the size of frames since we're using column weights to give relative sizes, and you don't have to give sizes to their labels.

    We have removed the propagation code, removed the non-resizable behavior, and removed some hard-coded widths, giving us less code but more functionality.

    0 讨论(0)
提交回复
热议问题