3

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.

4
  • Just create a Frame and use the frame object as the Canvas's master. What's the problem? Commented Oct 28, 2012 at 18:27
  • I have two canvases and I like to position side by side rather than one above the other. Thanks, I'll try frame method. Commented Oct 28, 2012 at 18:29
  • Are you using grid() or pack() as your geometry manager? Commented Oct 28, 2012 at 18:30
  • Not at a computer right now to test, but try using something like canvas1.pack(side=LEFT) and canvas2.pack(side=RIGHT). I'll be able to give a more detailed answer later. Commented Oct 28, 2012 at 18:33

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

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.