#๐Ÿ”’ Canvas widget not resizing properly (tkinter/ttkbootstrap)

16 messages ยท Page 1 of 1 (latest)

winged cloak
#

I'm trying to make this canvas, which has a scrollingtext widget inside, resize according to the contents of these two entry widgets when I click a button.
Whenever I click the button, though, it just glitches a bit and then goes back to its original size.
Any idea why this is happening?

textBoxFrame = ttkb.Frame(right_frame)
textBoxFrame.grid(row=0, column=0, columnspan=2, sticky="NSEW")
textBoxFrame.grid_rowconfigure(0, weight=1)
textBoxFrame.grid_columnconfigure(0, weight=1)
innerCanvas = ttkb.Frame(textBoxFrame)
innerCanvas.pack()
textBox = ttkb.ScrolledText(innerCanvas, wrap=WORD)
textBox.grid(row=0, column=0)

def onResizeClick():
    try:
        boxwidth = int(box_width_setting.get())
        boxheight = int(box_height_setting.get())
        resizeTextbox(innerCanvas, boxwidth, boxheight)
    except ValueError:
        print("Please pass valid integers")

def resizeTextbox(canvas, boxwidth, boxheight):
    canvas.grid_forget()
    canvas.configure(width=boxwidth, height=boxheight)

resizeTextbox(innerCanvas, 80, 45)
box_resize = ttkb.Button(box_setting_frame, text="resize", command=onResizeClick)
box_resize.grid(row=1, column=1)

I'm using the function that's supposed to resize the widget to set its size initially, so I know that it works to some extent.

cold cypressBOT
#

@winged cloak

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ancient jetty
#

if im correct, setting innerCanvas.grid_propagate(False) should resolve it

#

some other notes:

  • textBox is not configured to expand so if your frame becomes larger than the default requested size of that widget, it'll leave empty space around your text box instead
  • canvas.grid_forget() is a no-op because innerCanvas was packed into textBoxFrame, not gridded
    • .grid_forget() and .pack_forget() lets you remove a child widget that's been gridded/packed
winged cloak
#

grid_propagate makes the text box really tiny, for some reason

#

and pack_forget makes it disappear altogether

#

grid_propagate on the canvas and using .pack with expand=true on the textbox do make it appear properly, but it still doesn't resize

#

I tried making the frame also not propagate, but it's still no dice

#

oh, well, I needed to repack the canvas after resizing it

#

it works after doing pack_propagate(False)!

#

Thanks again

ancient jetty
#

ye, you should be mindful of what geometry manager you're using for a widget and which methods apply to that widget's geometry manager vs. the widget's parent geometry manager, otherwise it's really easy to mix them up

#

as in, given a parent and child widget, you might call these methods on the parent to configure the parent's grid:

  • parent.grid_propagate()
  • parent.grid_columnconfigure()
    but you would call these methods on the child:
  • child.grid() to add the child to the parent's grid
  • child.grid_forget() to remove the child from the parent's grid
    if you mix up the widget for these functions, you'd be configuring the wrong widget

finished writing up an example for this textbox resizing if you want to look through it:
https://gist.github.com/thegamecracks/8f887d7438c711c31c8af12fdeecad7d
when reading, try to keep track of which widget's geometry manager is being affected in each method call (this example has 3 total), and it'll make more sense how to use them

cold cypressBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.