使图片适合全屏窗口

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

我正在尝试让图片适合我创建的全屏窗口的范围。使用此代码:

import sys
import random
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))
window.title("War of the Ring")

def CloseProgram():
    window.destroy()
    window.quit()

def PlayMainGame():
    #Destroy widgets and title
    Title.destroy()
    NewGameButton.destroy()
    LoadGameButton.destroy()
    OptionsButton.destroy()
    global ResizedBoardImage
    canvas = Canvas(window, width = width, height = height)
    canvas.pack()
    BoardImage = Image.open("Middle Earth Map.jpg")
    ResizedBoardImage = BoardImage.resize((width, height), )
    ResizedBoardImage = ImageTk.PhotoImage(BoardImage)
    canvas.create_image(10,10, anchor = NW, image = ResizedBoardImage)

` 我在照片中得到了结果,大约四分之一的图像填满了整个屏幕。 shell 中不会弹出任何警告或错误。不知道我做错了什么。

当我期望图像适合窗口屏幕时,我得到了照片中的结果,大约四分之一的图像填满了整个屏幕。

shell 中不会弹出任何警告或错误。不知道我做错了什么。

image-scaling
1个回答
0
投票

描述: 问题出在您的代码中:

width = window.winfo_screenwidth()
height = window.winfo_screenheight()

您正在根据您的显示器定义 tkinter 窗口宽度和高度。

例如,如果您的 Windows 分辨率为

1920x1080
,则此片段中的宽度
width = window.winfo_screenwidth()
将定义为 1920,此片段中的高度
height = window.winfo_screenheight()
将定义为 1080。

问题: tkinter 窗口太大了,如果您减小 tkinter 窗口的高度和宽度,它会扩展到屏幕之外,那么它将适合您。

解决方案: 考虑到你的决心是

1920x1080
:

import sys
import random
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

window = tk.Tk()
width = 1600
height = 900
window.geometry("%dx%d" % (1600, 900))
window.title("War of the Ring")

def CloseProgram():
    window.destroy()
    window.quit()

def PlayMainGame():
    #Destroy widgets and title
    Title.destroy()
    NewGameButton.destroy()
    LoadGameButton.destroy()
    OptionsButton.destroy()
    global ResizedBoardImage
    canvas = Canvas(window, width = width, height = height)
    canvas.pack()
    BoardImage = Image.open("Middle Earth Map.jpg")
    ResizedBoardImage = BoardImage.resize((width, height), )
    ResizedBoardImage = ImageTk.PhotoImage(BoardImage)
    canvas.create_image(10,10, anchor = NW, image = ResizedBoardImage)

考虑到你的决心是

1600x900

import sys
import random
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

window = tk.Tk()
width = 1280
height = 720
window.geometry("%dx%d" % (1600, 900))
window.title("War of the Ring")

def CloseProgram():
    window.destroy()
    window.quit()

def PlayMainGame():
    #Destroy widgets and title
    Title.destroy()
    NewGameButton.destroy()
    LoadGameButton.destroy()
    OptionsButton.destroy()
    global ResizedBoardImage
    canvas = Canvas(window, width = width, height = height)
    canvas.pack()
    BoardImage = Image.open("Middle Earth Map.jpg")
    ResizedBoardImage = BoardImage.resize((width, height), )
    ResizedBoardImage = ImageTk.PhotoImage(BoardImage)
    canvas.create_image(10,10, anchor = NW, image = ResizedBoardImage)

结论: 减小 tkinter 窗口的大小,不要保持与原始分辨率相同,因为窗口小部件也占用了窗口以及 tkinter 窗口任务栏的一些空间,这使得窗口变得比预期的宽度和高度更大.

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.