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.

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. Commented Feb 27, 2022 at 16:45
  • is add_textbox_with_label part of the definition of the class DialogForm? Commented Feb 27, 2022 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. Commented Feb 27, 2022 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. Commented Feb 27, 2022 at 17:32
  • Like on my last screenshot - columnconfigure not working as well. Commented Mar 13, 2022 at 13:40

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I agree, pack works best for containers in general. But since OP was asking for grid I'd suggested grid configure.
@SpectrumCodeFrom: that link takes me to a sign-up page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.