Naf Rose
Naf Rose

Reputation: 3

position of line when resizing Tkinter window

The following code:

from tkinter import *
root=Tk()
for x in range(10):
    for y in range(10):
        canvas=Canvas(root, width='15',height='15',highlightthickness=0,bg='red')                      
        canvas.create_line(canvas.winfo_x(),canvas.winfo_y(),canvas.winfo_x()+15,canvas.winfo_y()+15,width=2,fill='black')
        canvas.grid(row=y,column=x,sticky='NESW')
for x in range(10):
    for y in range(10):
        root.columnconfigure(x,weight=1)
        root.rowconfigure(y,weight=1)
root.mainloop()

produces this, which is a 10 by 10 grid filled with canvases; there is a line extending from the top left to the bottom right corner of each canvas.

When I resize the window, the canvas widgets resize correctly, but the lines retain their shape like this. The lines need to adjust according to the window/widget size.

The core of the problem is that the lines are made using the coordinates of the top left corner of the widget, and are extended 15 pixels in each direction. Is there a way of getting the coordinates of the bottom right corner of the widget, so that the lines can change their shape dynamically, or some other way of keeping the lines shape, relative to the widget?

Upvotes: 0

Views: 1287

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386352

You can get the current width and height of any widget with the winfo_width and winfo_height methods. If you are binding to the <Configure> method to track when the canvas changes size, the event object has a width and height attribute.

For example:

from tkinter import *

def redraw_line(event):
    width = event.width
    height = event.height
    canvas = event.widget
    canvas.coords("diagonal", 0, 0, width, height)

root=Tk()
for x in range(10):
    for y in range(10):
        canvas=Canvas(root, width='15',height='15',highlightthickness=0,bg='red')                      
        canvas.bind("<Configure>", redraw_line)
        # coordinates are irrelevant; they will change as soon as
        # the widget is mapped to the screen.
        canvas.create_line(0,0,0,0, tags=("diagonal",))
        canvas.grid(row=y,column=x,sticky='NESW')

for x in range(10):
    for y in range(10):
        root.columnconfigure(x,weight=1)
        root.rowconfigure(y,weight=1)


root.mainloop()

Upvotes: 1

Related Questions