如何让 tkinter canvas 到可用 space 的中心
How to get a tkinter canvas to the center of the available space
我试图让 canvas 位于可用 space 的中心,但它一直停留在屏幕的北部...这是一张图片
这是代码
def newsetup(filelocation):
global width, height
global stage, img_id, imgtk
for widgets in root.winfo_children():
widgets.destroy()
root.config(bg = '#454545')
iconsframewidth = int(screen_width / 20)
iconsframe = Frame(root, width = iconsframewidth, bg = '#2a2a2a')
iconsframe.pack(side = 'left', expand = False, fill = 'y')
iconsframe.pack_propagate(0)
sep1frame = Frame(root, bg = '#1a1a1a', width = 10, relief = 'sunken')
sep1frame.pack(side = 'left', expand = False, fill = 'y')
optionsframe = Frame(root, bg = '#2a2a2a', height = 100)
optionsframe.pack(side = 'top', expand = False, fill = 'x')
optionsframe.pack_propagate(0)
sep2frame = Frame(root, bg = '#1a1a1a', height = 10, relief = 'sunken')
sep2frame.pack(side = 'top', expand = False, fill = 'x')
propertyframe = Frame(root, bg = '#2a2a2a', width = 150)
propertyframe.pack(side = 'right', expand = False, fill = 'y')
propertyframe.pack_propagate(0)
sep3frame = Frame(root, bg = '#1a1a1a', width = 10, relief = 'sunken')
sep3frame.pack(side = 'right', expand = False, fill = 'y')
stageframe = Frame(root, bg = '#454545')
stageframe.pack(side = 'top', expand = True, fill = 'both')
stageframe.pack_propagate(0)
stage = Canvas(stageframe, width = width, height = height)
stage.pack(anchor = CENTER)
root.update()
pencilbutton = Button(iconsframe, image = pencilimg, borderwidth = 0, bg = '#2a2a2a', fg = '#2a2a2a', relief = 'flat')
pencilbutton.pack(anchor = W)
imgtk = ImageTk.PhotoImage(Image.open(filelocation))
img_id = stage.create_image(stage.winfo_width() / 2, stage.winfo_height() / 2, image = imgtk)
stage.image = imgtk
stage.bind('<Configure>', PhotoEditing.stageresize)
我试过使用锚点但没有用...我有一个想法将 canvas 放在框架中但框架没有填满整个空白区域
TL;DR 在打包 canvas.
时添加 expand=True
打包器的工作方式是将小部件放置在可用空 space 的一侧,并将其居中在分配的 space 中。默认情况下,分配的 space 将尽可能小以适合小部件。
当沿着顶部或底部打包东西时,如果你想让它垂直居中,你需要告诉打包器扩展分配给所有剩余的 space 的 space .然后小部件将在剩余的 space.
中水平和垂直居中
我还建议始终明确设置边,以便更清楚地表明您的意图。
stage.pack(side="top", anchor = CENTER, expand=True)
我试图让 canvas 位于可用 space 的中心,但它一直停留在屏幕的北部...这是一张图片
这是代码
def newsetup(filelocation):
global width, height
global stage, img_id, imgtk
for widgets in root.winfo_children():
widgets.destroy()
root.config(bg = '#454545')
iconsframewidth = int(screen_width / 20)
iconsframe = Frame(root, width = iconsframewidth, bg = '#2a2a2a')
iconsframe.pack(side = 'left', expand = False, fill = 'y')
iconsframe.pack_propagate(0)
sep1frame = Frame(root, bg = '#1a1a1a', width = 10, relief = 'sunken')
sep1frame.pack(side = 'left', expand = False, fill = 'y')
optionsframe = Frame(root, bg = '#2a2a2a', height = 100)
optionsframe.pack(side = 'top', expand = False, fill = 'x')
optionsframe.pack_propagate(0)
sep2frame = Frame(root, bg = '#1a1a1a', height = 10, relief = 'sunken')
sep2frame.pack(side = 'top', expand = False, fill = 'x')
propertyframe = Frame(root, bg = '#2a2a2a', width = 150)
propertyframe.pack(side = 'right', expand = False, fill = 'y')
propertyframe.pack_propagate(0)
sep3frame = Frame(root, bg = '#1a1a1a', width = 10, relief = 'sunken')
sep3frame.pack(side = 'right', expand = False, fill = 'y')
stageframe = Frame(root, bg = '#454545')
stageframe.pack(side = 'top', expand = True, fill = 'both')
stageframe.pack_propagate(0)
stage = Canvas(stageframe, width = width, height = height)
stage.pack(anchor = CENTER)
root.update()
pencilbutton = Button(iconsframe, image = pencilimg, borderwidth = 0, bg = '#2a2a2a', fg = '#2a2a2a', relief = 'flat')
pencilbutton.pack(anchor = W)
imgtk = ImageTk.PhotoImage(Image.open(filelocation))
img_id = stage.create_image(stage.winfo_width() / 2, stage.winfo_height() / 2, image = imgtk)
stage.image = imgtk
stage.bind('<Configure>', PhotoEditing.stageresize)
我试过使用锚点但没有用...我有一个想法将 canvas 放在框架中但框架没有填满整个空白区域
TL;DR 在打包 canvas.
时添加expand=True
打包器的工作方式是将小部件放置在可用空 space 的一侧,并将其居中在分配的 space 中。默认情况下,分配的 space 将尽可能小以适合小部件。
当沿着顶部或底部打包东西时,如果你想让它垂直居中,你需要告诉打包器扩展分配给所有剩余的 space 的 space .然后小部件将在剩余的 space.
中水平和垂直居中我还建议始终明确设置边,以便更清楚地表明您的意图。
stage.pack(side="top", anchor = CENTER, expand=True)