如何使图像布局与其他屏幕分辨率相匹配?

问题描述 投票:0回答:1

如何使其他不同分辨率的计算机上的程序中的图像布局与我的计算机上的相同。例如:我在我的计算机上运行一个程序,图像一切正常,但是当我在另一台计算机上运行它时,图像布局与我的不一样,也就是说,它不适合屏幕并延伸到屏幕之外。如何才能让其他电脑上的图片和我的一样呢? 将 tkinter 导入为 tk

import tkinter as tk

from PIL import Image, ImageTk


def open_second_window():
    second_window = tk.Toplevel(root)
    second_window.title("window")
    second_window.state('zoomed')


    image_path = "1.jpg"
    image = Image.open(image_path)
    photo = ImageTk.PhotoImage(image)

    label = tk.Label(second_window, image=photo)
    label.image = photo 
    label.pack(fill=tk.BOTH, expand=True)
    label.place(relx=0.5, rely=0)

    button = tk.Button(second_window,height=4, width=12, font='Times 31', text="ONE", command=lambda: open_four_window(second_window))

    button.place(relx=0.22, rely=0.13, relwidth=0.2, relheight=0.3, anchor='ne')
    button1 = tk.Button(second_window,height=4, width=12, font='Times 31', wraplength=289, text="TWO", command=open_five_window)
    button1.place(relx=0.48, rely=0.13, relwidth=0.2, relheight=0.3, anchor='ne')
    button = tk.Button(second_window,height=4, width=12, font='Times 31', text="THREE", command=open_threee_window)
    button.place(relx=0.22, rely=0.48, relwidth=0.2, relheight=0.3, anchor='ne')
    button1 = tk.Button(second_window,height=4, width=12, font='Times 31', text="FOUR", command=open_threetenfive_window)
    button1.place(relx=0.48, rely=0.48, relwidth=0.2, relheight=0.3, anchor='ne')


    tk.Label(second_window, font='Times 30', text="TEXT:", fg="red").place(relx=0.07, rely=0.05)


    third_window.grab_set()
def open_four_window(second_window):
    second_window.destroy()

    four_window = tk.Toplevel(root)
    four_window.title("win")
    four_window.state('zoomed')

    image_path = "2.png"
    image = Image.open(image_path)
    photo = ImageTk.PhotoImage(image)

    label = tk.Label(four_window, image=photo)
    label.image = photo  
    label.pack(fill=tk.BOTH, expand=True)
    label.place(relx=0, rely=0)


    button = tk.Button(four_window,height=1, width=33, font='Times 31', text="START", command=lambda: open_second_window())
    button.place(relx=0.5, rely=0.9, relwidth=0.5, relheight=0.1, anchor='ne')
    button1 = tk.Button(four_window,height=1, width=33, font='Times 31', text="EXIT", command=Close)
    button1.place(relx=1.0, rely=0.9, relwidth=0.50, relheight=0.1, anchor='ne')

    third_window.grab_set()

...

root = tk.Tk()
root.title("menu")
root.state('zoomed')


image_path = "40.jpg"
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)
image.thumbnail((1, 1))

label = tk.Label(root, image=photo)
label.pack(fill=tk.BOTH, expand=True)
label.place(relx=0, rely=0)

def Close(): 
    root.destroy() 

button = tk.Button(root, font='Times 31', text="START", command=lambda: open_second_window())
button.place(relx=0.5, rely=0.9, relwidth=0.5, relheight=0.1, anchor='ne')
button1 = tk.Button(root, font='Times 31', text="EXIT", command=Close)
button1.place(relx=1.0, rely=0.9, relwidth=0.50, relheight=0.1, anchor='ne')


tk.Label(root, font='Times 30', text="red", fg="red text").place(relx=0.1, rely=0.05)
tk.Label(root, font='Times 30 italic', text="descriptions").place(relx=0.35, rely=0.11)

root.mainloop()

附注我上一个问题的答案并没有完全帮助,界面打开正常,但除了图像

python python-3.x windows debugging tkinter
1个回答
0
投票

您已经在部分代码中使用了相对定位(

relx
rely
),但并不统一。为了使您的布局灵活并适应不同的屏幕尺寸,请确保所有小部件(包括图像和按钮)都尽可能使用相对单位放置和调整大小。

我重写了你的代码,添加了动态布局。

import tkinter as tk
from PIL import Image, ImageTk

def resize_image(event, label, image_path):
    new_width = event.width
    new_height = event.height
    image = Image.open(image_path)
    image = image.resize((new_width, new_height), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image)
    label.config(image=photo)
    label.image = photo

def open_second_window():
    second_window = tk.Toplevel(root)
    second_window.title("window")
    second_window.state('zoomed')

    image_path = "1.jpg"
    image = Image.open(image_path)
    photo = ImageTk.PhotoImage(image)

    label = tk.Label(second_window, image=photo)
    label.image = photo  # Keep reference to avoid garbage collection
    label.pack(fill=tk.BOTH, expand=True)

    # Bind the window resize event to dynamically resize the image
    second_window.bind('<Configure>', lambda event: resize_image(event, label, image_path))

    button = tk.Button(second_window, height=4, width=12, font='Times 31', text="ONE")
    button.place(relx=0.22, rely=0.13, relwidth=0.2, relheight=0.3, anchor='ne')

    button1 = tk.Button(second_window, height=4, width=12, font='Times 31', text="TWO")
    button1.place(relx=0.48, rely=0.13, relwidth=0.2, relheight=0.3, anchor='ne')

...

root = tk.Tk()
root.title("menu")
root.state('zoomed')

image_path = "40.jpg"
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)

label = tk.Label(root, image=photo)
label.image = photo  # Keep reference to avoid garbage collection
label.pack(fill=tk.BOTH, expand=True)

# Bind the window resize event to dynamically resize the image
root.bind('<Configure>', lambda event: resize_image(event, label, image_path))

button = tk.Button(root, font='Times 31', text="START", command=lambda: open_second_window())
button.place(relx=0.5, rely=0.9, relwidth=0.5, relheight=0.1, anchor='ne')

button1 = tk.Button(root, font='Times 31', text="EXIT", command=root.quit)
button1.place(relx=1.0, rely=0.9, relwidth=0.5, relheight=0.1, anchor='ne')

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.