Fitting Picture to Full Screen Window

28 Views Asked by At

I am trying to get a picture to fit within the bounds of a full screen window I have created. With this code:

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)

` I get the result in the photo, about a quarter of the image filling the entirety of the screen. No warnings or errors pop up in the shell. Not sure what I'm doing wrong.

I get the result in the photo, about a quarter of the image filling the entirety of the screen, when I was expecting the image to fit to the window screen.

enter image description here

No warnings or errors pop up in the shell. Not sure what I'm doing wrong.

1

There are 1 best solutions below

0
AshhadDevLab On

Description: The issue is here in your code:

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

You are defining you tkinter window width and height according to your display.

For example if your windows resolution is 1920x1080 then width in this snippet width = window.winfo_screenwidth() will be defined as 1920 and height in this snippet height = window.winfo_screenheight() will be 1080.

Issue: The tkinter window is so big that its expanding out of your screen if you reduce the tkinter windows height and width then it will work for you.

Solution: Considering your resolution is 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)

Considering you resolution is 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)

Conclusion: Reduce the size of the tkinter window and do not keep it as same as your original resolution because windows widgets are also taking up some space of the window as well as the tkinter windows taskbar which makes the window to become bigger then the expected width and height.