I am creating a simple application using tkinter.ttk. I am creating one Image Viewer App but while creating the app I am getting some problem. Here is my code:
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()
When I run this then it comes like this: In small window
And when I maximize it: In maximize It comes like this. In above picture you can see that the image is not coming properly in center. My pictures must be in exact center in both in small and big window. Please can any one help me to do that by seeing my program.
Thanks in advance