tkinter frame propagate not behaving?

前端 未结 2 1416
清歌不尽
清歌不尽 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()
    

提交回复
热议问题