gpt4 book ai didi

python - 在 python 中的 tkinter.ttk 中使图像精确居中

转载 作者:行者123 更新时间:2023-12-04 00:54:53 26 4
gpt4 key购买 nike

我正在使用 tkinter.ttk 创建一个简单的应用程序。我正在创建一个图像查看器应用程序,但在创建该应用程序时遇到了一些问题。这是我的代码:

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

root = Tk()
root.title("Simple Image Viewer")
style = Style()


root.columnconfigure(0, weight=True)
root.rowconfigure(0, weight=True)

img1 = ImageTk.PhotoImage(Image.open("images/img_1.jpg"))
img2 = ImageTk.PhotoImage(Image.open("images/img_2.jpg"))
img3 = ImageTk.PhotoImage(Image.open("images/img_3.jpg"))

images = [img1, img2, img3]
img_idx = 0


def previous_image():
global lbl_img
global images
global img_idx
img_idx = img_idx - 1
if img_idx < 0:
img_idx = len(images) - 1
try:
lbl_img.configure(image=images[img_idx])
except IndexError:
img_idx = -1
lbl_img.configure(image=images[img_idx])
finally:
status.configure(text=f"{img_idx + 1} of {len(images)} images.")


btn_back = Button(text="<", command=previous_image)


def forward_image():
global lbl_img
global images
global img_idx
img_idx = img_idx + 1
try:
lbl_img.configure(image=images[img_idx])
except IndexError:
img_idx = 0
lbl_img.configure(image=images[img_idx])
finally:
status.configure(text=f"{img_idx + 1} of {len(images)} images.")


btn_forward = Button(text=">", command=forward_image)

lbl_img = Label(image=images[img_idx])

status = Label(root, text=f"{img_idx + 1} of {len(images)} images.", borderwidth=1,
relief=SUNKEN, anchor="e")
status.grid(row=2, column=0, columnspan=3, stick=W+E)

btn_exit = Button(text="Exit", command=root.quit)
btn_back.grid(row=0, column=0, stick=W)
lbl_img.grid(row=0, column=1)
btn_forward.grid(row=0, column=2, stick=E)
btn_exit.grid(row=1, column=1)

root.mainloop()

当我运行它时,它是这样的: In small window

当我最大化它时:In maximize它是这样来的。在上图中,您可以看到图像没有正确居中。我的照片必须在小窗口和大窗口中都位于正中央。请任何人通过查看我的程序来帮助我做到这一点。

提前致谢

最佳答案

您可以使用网格来实现这一点,您需要确保左右获得空间,上下也获得空间,但中间没有空间。举个例子:

import tkinter as tk

root = tk.Tk()

up = tk.Frame(root)
up.grid(column=0, row=0,columnspan=3,sticky='n')
s1 = tk.Label(up, text='spacer')
s1.pack()

left = tk.Frame(root)
b1 = tk.Button(left, text='B1')
left.grid(column=0,row=1,sticky='w')
b1.pack()

middle = tk.Frame(root)
middle.grid(column=1, row=1)
s2 = tk.Label(middle, text='spacer')
s2.pack()

down = tk.Frame(root)
qb = tk.Button(down, text='Exit', command= root.destroy)
down.grid(column=0, row=2,columnspan=3,sticky='s')
qb.pack()

right = tk.Frame(root)
right.grid(column=2,row=1,sticky='e')
b2 = tk.Button(right, text='B2')
b2.pack()

root.columnconfigure(0,weight=1) #left get space
root.columnconfigure(2,weight=1) #right get space

root.rowconfigure(0, weight=1) #up get space
root.rowconfigure(2, weight=1) #down get space

root.mainloop()

我们的输出: enter image description here

详细信息what weight does .

关于python - 在 python 中的 tkinter.ttk 中使图像精确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63313779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com