0

When I configure a tkinter window's rows and columns with weight=1 and then place a canvas over it spanning all the rows and the columns ,the canvas becomes size responsive:

win=Tk()
win.geometry("1200x675")
rows=0
while rows<200:
    win.rowconfigure(rows,weight=1)
    win.columnconfigure(rows,weight=1)
    rows+=1

canvas=Canvas(win,bg='white',width=1200,height=675)
canvas.grid(sticky='nesw',rowspan=200,columnspan=200)

But the widgets placed inside the canvas at specific coordinate remains of the same size and at the same position,for example a button:

but=Button(text="Calculate")
canvas.create_window(100,100,window=but)

So,please can anyone help as to how to make the widgets also size responsive.

parag goyal
  • 23
  • 1
  • 8

1 Answers1

1

You don't need to make that many grid cells. One cell with weight=1 will expand the canvas with size change.

There is no general method to scale contained widgets, you'll have to write that yourself.

Hook window size change with <Configure> and then loop through widgets on canvas form canvas.find_withtag('all'). See example below:

from tkinter import *

win = Tk()
win.geometry("300x200+800+50")

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

canvas=Canvas(win, bg='white')
canvas.grid(sticky='nesw')

def size(event):
    width, heigth = canvas.winfo_width(), canvas.winfo_height()
    print('Canvas size:', width, 'x', heigth)
    items = canvas.find_withtag('all')  # Find all widgets on canvas
    for item in items:
        pass    # Change widget size here, one widget at a time

win.bind('<Configure>', size)   # Hook window size changes

win.mainloop()
figbeam
  • 7,001
  • 2
  • 12
  • 18