Andrew
Andrew

Reputation: 1025

Positioning Canvas in window - Tkinter/python

Is there a way to position a canvas in a window and have a frame around it?I found only how to position objects inside a canvas.

Upvotes: 3

Views: 29030

Answers (3)

Hamrobe
Hamrobe

Reputation: 41

Like fortyTwo102 mentioned, the place function will allow you to specify exactly where the canvas is. I thought I'd provide some more examples:

# in the center
canvas.place(relx=0.5, rely=0.5, anchor=CENTER)

# in the bottom right corner
canvas.place(relx=1.0, rely=1.0, anchor=SE)

# in the bottom left corner
canvas.place(relx=0.0, rely=1.0, anchor=SW)

# 30 pixels from the left, 50 from the top
canvas.place(x=30, y=50)

Source (and more helpful info): https://www.tutorialspoint.com/python/tk_place.htm

Upvotes: 0

fortyTwo102
fortyTwo102

Reputation: 113

You can use the place() function instead of pack() and do something like:

canvas.place(relx=0.5, rely=0.5, anchor=CENTER)

this would place it at the center.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385970

You can create a frame, then put your widgets in it:

f = tk.Frame(...)
c1 = tk.Canvas(f, ...)
c2 = tk.Canvas(f, ...)
c1.pack(side="left", fill="both", expand=True)
c2.pack(side="right", fill="both", expand=True)

The above will give you two side-by-side canvases inside a single frame. They will grow and shrink as you resize the containing window.

Upvotes: 6

Related Questions