Python 调整大小时将图像居中

Python 调整大小时将图像居中,python,tkinter,Python,Tkinter,使用Tkinter时,我需要将实体居中。当尝试将标签居中时,它将仅在第一行内居中,而不会在窗口内居中 我希望它在整个窗口的中心。i、 e.中间。到目前为止,它只是顶部的中间部分。这可能吗 谢谢 from tkinter import * from tkinter import ttk from PIL import ImageTk, Image root = Tk() # New window, but text appears in the center of the center (th

使用Tkinter时,我需要将实体居中。当尝试将标签居中时,它将仅在第一行内居中,而不会在窗口内居中

我希望它在整个窗口的中心。i、 e.中间。到目前为止,它只是顶部的中间部分。这可能吗

谢谢

from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image

root = Tk()

# New window, but text appears in the center of the center (the absolute center).
def whatsup():
    popup = Tk()
    popup.title("Cadillac")
    frame = Frame(popup)
    frame.pack()

    label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
    label.pack()

root.title("I Love You")

# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.pack(side=LEFT)

# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.pack()

root.mainloop()

您可以使用
grid
而不是
pack
,使用
rowconfigure
columnconfigure
这样的方法:

from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image

root = Tk()
root.title("I Love You")

# New window, but text appears in the center of the center (the absolute center).
def whatsup():
    popup = Tk()
    popup.title("Cadillac")
    frame = Frame(popup)
    frame.pack()

    label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
    label.pack()

# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.grid(row=0, column=0, sticky='w')

# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.grid(row=1, column=1)
root.rowconfigure([0,1,2], weight=1)
root.columnconfigure([0,1,2], weight=1)

root.mainloop()
回复评论

这也可以,但它的居中方式不同:

from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image

root = Tk()
root.title("I Love You")

# New window, but text appears in the center of the center (the absolute center).
def whatsup():
    popup = Tk()
    popup.title("Cadillac")
    frame = Frame(popup)
    frame.pack()

    label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
    label.pack()

# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.grid(row=0, column=0, sticky='w')

# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.grid(row=0, column=1, sticky='')

root.rowconfigure(0, weight=1)
root.columnconfigure([0,1], weight=1)

root.mainloop()
在进行了一些修补(没有双关语),我在
whatsup()
函数中添加了
expand=YES
frame.pack()

def whatsup():
    popup = Tk()
    popup.title("Cadillac")
    frame = Frame(popup)
    frame.pack(expand=YES) # This was the changed line!

    label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
    label.pack()

这允许弹出文本居中。

尝试使用
canvas.place(relx=0.5,rely=0.5,anchor='c')
。为什么要将3行和3列进行utalize?只需配置第一行和第一列,然后执行canvas.grid(row=0,column=0,sticky='')^^你确定吗?我认为您需要有3行才能设置权重,这样即使在调整大小时它也会保持居中。不,您不需要。基本上,第一行/列会占用所有可用空间,通过设置
sticky='
可以告诉网格管理器不要将小部件放在任何角落,也不要以任何方式拉伸它。所以剩下要做的就是把它放在中心。请随时为我试用它,效果非常好。@Nummer_42O:在我的回答中,看到我用一行和
sticky='
尝试的代码,但它不起作用,图像保持在左上方。也许我在什么地方出错了?是的,你完全删除了
rowconfigure
columnconfigure
。我希望您只在第0行/第0列上使用它们,而不是在所有3列上使用它们^^