[英]Make Image Exact Center in tkinter.ttk in python
我正在使用 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()
當我運行它時,它是這樣的:在小 window
當我最大化它時:在最大化它是這樣的。 在上圖中,您可以看到圖像未正確居中。 我的圖片在大小 window 中都必須位於准確的中心。 請任何人通過查看我的程序來幫助我做到這一點。
提前致謝
您可以使用網格來實現這一點,您需要確保左右獲得空間,上下也一樣,但中間沒有空間。 作為一個例子:
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()
有關重量的詳細信息。
我認為您可以為此采取兩種解決方案。
.place(x=x_coord, y=y_coord, anchor=center)
以確保圖像已設置並始終位於程序的中心。一個技巧是在一開始就設置程序的分辨率,而不是使用網格,您可以使用坐標和錨點來很好地放置您的按鈕和圖像(我認為),我還沒有嘗試使用圖像。
root.resizable(0, 0)
后,在程序開始時。 這將阻止用戶調整 window 的大小。我實現了你所說的,但對我來說按鈕並不完美。 我想把按鈕放在 label 的頂部,我不想把它們放在一邊。這可能嗎?
import tkinter as tk
root = tk.Tk()
image_list = getfiles()
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')
image = Image.open(image_list[current])
photo = ImageTk.PhotoImage(image)
s2['image'] = photo
s2.photo = photo
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()
要將按鈕放在與圖像相同的位置,您需要 place()
label = Tkinter.Label(root, compound=Tkinter.TOP)
label.pack()
A=Tkinter.Button(root, text='Previous picture', command=lambda: move(-1))
A.place(relx=0, x=2, y=20,width=130,height=100)
B=Tkinter.Button(root, text='Next picture', command=lambda: move(+1))
B.place(relx=0, x=135, y=20,width=130,height=100)
C=Tkinter.Button(root, text='PRINT', command=root.quit)
C.place(relx=1, x=-130, y=20,width=130,height=100)
move(0)
root.update()
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.