Rémi
Rémi

Reputation: 3744

How can I make a window fit its contents in tkinter?

Below is some sample code. The window does not resize (python3.2, on a Mac, from macports). Is there a way I can modify it so that it resizes the window? Thanks.

import tkinter
import time

root = tkinter.Tk()
can = tkinter.Canvas(root, width = 10, height = 10)
can.pack()

for i in range(10):
    can.configure(width = i * 50, height = i * 50)
    root.update()
    print(i)
    time.sleep(0.5)

Upvotes: 1

Views: 2292

Answers (1)

Patrick T Nelson
Patrick T Nelson

Reputation: 1254

Perhaps try this:

import tkinter
import time

root = tkinter.Tk()
can = tkinter.Canvas(root, width = 10, height = 10)
can.rowconfigure(0, weight=1)
can.columnconfigure(0, weight=1)
can.grid()

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

for i in range(10):
    can.configure(width = i * 50, height = i * 50)
    root.update()
    print(i)
    time.sleep(0.5)

Upvotes: 1

Related Questions