1

I can’t get any information on exactly how window and canvas sizes work in Tkinter.

If I create a window, I can set its size using either the .configure() method or the .geometry() method. If I set both, the .geometry() method appears to override the .configure() method, and I know I can also set the position using the .geometry() method.

I can also create a Canvas object and pack it inside the window. Regardless of the size I set for the canvas, either too large or too small, it still fits neatly inside the window.

Here is a code snippet to illustrate my experiments:

root = tkinter.Tk()
root.configure(background='#ECECEC', padx=20, pady=20)
root.geometry('800x600+200+200')
root.configure(width=1200, height=700)

canvas = tkinter.Canvas(root, width=600, height=1400, background='white')
canvas.pack(fill="both", expand=True)

root.mainloop()

What’s the relationship between the window .configure() and .geometry() methods, and how should this affect the included canvas?

2
  • 1
    You might find this interesting. Commented May 11, 2024 at 13:38
  • 1
    Every widget has a natural size; sometime's its obvious (such as a Button or Label being just big enough to hold its text), sometimes you have to explicitly set it (such as a Canvas or Text, which are expected to have changing contents). But the geometry manager you use to make the widget visible can override that - here, the fill parameter to .pack() says to ignore the natural size, and instead make the widget fit the window. Commented May 12, 2024 at 18:33

0

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.