1

In short, I have such a code (it is a fragment, but the important one).

class DF(tk.Toplevel):
    def __init__(self, master,width):
        tk.Toplevel.__init__(self)

        self.root_frame = Frame(self, bg="red")
        self.root_frame.grid(row=0, column=0, padx=20, pady=20, sticky="ne")

        bottom_frame = Frame(self)
        bottom_frame.grid(row=1, column=0, padx=5, pady=5, sticky="ne")


            cancel_button = Button(master=bottom_frame)
            cancel_button.pack(side="right")

For example, this is how I dynamically create consecutive _main_form elements.

def add(self):
    label = tk.Label(master=self._main_frame)
    label.grid(row=self._rows, column=0, columnspan=2, sticky="we")

tk.Entry(master=self._main_frame) textbox.grid(row=self._rows, column=2, columnspan=2, sticky="ne")

However, the window does not adjust to the window's width. What can I do in such a situation? Thanks to bg you can see as if these frames were not at all in their entire width. I also add a screen.

ImLearning
  • 75
  • 6
  • Search for `.grid_columnconfigure` and `.grid_rowconfigure`. Also note that it is usually not a great idea to overwrite builtin-variables like `type` and that you can find a [Beginners guide on SO](https://stackoverflow.com/a/63536506/13629335). – Thingamabobs Feb 27 '22 at 16:45
  • is `add_textbox_with_label` part of the definition of the class `DialogForm`? – Bryan Oakley Feb 27 '22 at 17:29
  • yes, that's one of her methods. I create this object in another object and then I can use object.add_textbox_with_label etc. – ImLearning Feb 27 '22 at 17:31
  • 1
    That description is unclear. What is this other object? Could you please provide a complete example as a single block of code? Though, a cursory glance makes me thing @Thingamabobs is correct - you need to use `columnconfigure` to make sure all extra space goes to the frame. – Bryan Oakley Feb 27 '22 at 17:32
  • Like on my last screenshot - columnconfigure not working as well. – ImLearning Mar 13 '22 at 13:40
  • Your updated code has inconsistent variable name. Should `self._main_frame` be `self.root_frame` instead? – acw1668 Mar 14 '22 at 04:20

1 Answers1

2

The root of the problem is that you're using grid in the Toplevel, but you haven't told grid what to do with extra space. As a rule of thumb, you should always call rowconfigure and columnconfigure to give a positive weight to at least one row and one column.

self.grid_columnconfigure(0, weight=1)

In this case, however, since you only have two full-width frames inside the Toplevel, it's simpler to use pack for both the top and bottom frames.

bottom_frame.pack(side="bottom", fill="x")
self._main_frame.pack(side="top", fill="both", expand=True)

Either of those solutions solve the problem of the bottom and top frame not taking the full width of the window. You need to be equally diligent to add weight to columns and rows inside of self._main_frame.

For a deeper description of how the weight option works, along with some visual aids, see What does 'weight' do in tkinter?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685