简体   繁体   English

在窗口中定位画布 - Tkinter/python

[英]Positioning Canvas in window - Tkinter/python

有没有办法在窗口中放置画布并在它周围放置一个框架?我只找到了如何在画布内定位对象。

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.当您调整包含窗口的大小时,它们会增大和缩小。

You can use the place() function instead of pack() and do something like:您可以使用 place() 函数代替 pack() 并执行以下操作:

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

this would place it at the center.这将把它放在中心。

Like fortyTwo102 mentioned, the place function will allow you to specify exactly where the canvas is.就像前面提到的 fortyTwo102 一样, place函数将允许您准确指定画布的位置。 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来源(和更多有用的信息): https : //www.tutorialspoint.com/python/tk_place.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM