首页 > 解决方案 > 如何使用存储在文本文件中的图像链接显示图像

问题描述

我正在做一个学校项目,使用 tkinter 制作游戏。我正在尝试使用存储在文本文件中的图像链接来显示和图像,但是如果在 def 子例程的末尾出现错误并停止程序,它只会显示图像。我无法理解为什么如果出现错误停止程序时它会显示图像,但如果没有错误则会显示除图像之外的所有其他内容(错误是指代码中的一些随机字母)

我错过了什么吗?

注意:游戏涉及选择国旗所属的国家。

import tkinter as tk  
from PIL import ImageTk,Image
import random
import time


main_window = tk.Tk()
main_window.title("Fun With Flags")
...... #removed the unnecessary part

def play():
   #opens the question text file
   question_file = open('Flags.txt')
   lines = question_file.readlines()
   image_link = lines[1]

   #removes the \n from the end of the variable
   size = len(image_link)
   flag_link = image_link[:size - 1]
   print(flag_link)

   #Finds the image dimensions
   flag = Image.open(flag_link)
   width = flag. size[0]
   height = flag. size[1]
   print(width)
   print(height)

   #Resizes the image
   new_height = 200
   new_width  = int(new_height * width / height)
   flag = flag.resize((new_width, new_height), Image.ANTIALIAS)

   #Displays the flag
   flag = ImageTk.PhotoImage(flag) 
   c.create_image(450, 300, image = flag, anchor = "s")

homepage()

标签: pythontkinter

解决方案


您需要保持对图像的引用,否则图像会被 python 垃圾收集器清除。

所以你需要类似c.image = flagafterflag = flag.resize((new_width, new_height), Image.ANTIALIAS)

这是完整的代码:

import tkinter as tk  
from PIL import ImageTk,Image
import random
import time


main_window = tk.Tk()
main_window.title("Fun With Flags")

c = tk.Canvas(main_window, height = 600, width = 900, bg = "black")
c.pack(side = 'top', fill = 'both', expand = 'yes')

def homepage():

   c.delete("all")

   #Play Button
   c.create_rectangle (350,250, 550, 350, fill = "green", outline = "black", width = 5)
   c.create_text(450, 300, text = "Play", font = "Times 30 bold", fill = "black")

   #Info Button
   c.create_rectangle (350,450, 550, 550, fill = "green", outline = "black", width = 5)
   c.create_text(450, 500, text = "More Information", font = "Times 20 bold", fill ="black")

   def click(event):

    #play button
    if event.x > 350 and event.x < 550 and event.y > 250 and event.y < 350:
        
        #creates the background    
        c.create_rectangle (0, 0, 900, 600, fill = "black")
        c.create_rectangle(200, 375, 400, 450, fill = "green", outline = "red", width = 5)
        c.create_rectangle(500, 375, 700, 450, fill = "green", outline = "red", width = 5)
        c.create_rectangle(200, 475, 400, 550, fill = "green", outline = "red", width = 5)
        c.create_rectangle(500, 475, 700, 550, fill = "green", outline = "red", width = 5)

        play()

    #info button
    elif event.x > 350 and event.x < 550 and event.y > 450 and event.y < 550:
        print("info")
        info()

  c.bind("<Button-1>", click)


def play():

   #opens the question text file
   question_file = open('Flags.txt')
   lines = question_file.readlines()
   image_link = lines[1]

   #removes the \n from the end of the variable
   size = len(image_link)
   flag_link = image_link[:size - 1]
   print(flag_link)

   #Finds the image dimensions
   flag = Image.open(flag_link)
   width = flag. size[0]
   height = flag. size[1]
   print(width)
   print(height)

   #Resizes the image
   new_height = 200
   new_width  = int(new_height * width / height)
   flag = flag.resize((new_width, new_height), Image.ANTIALIAS)

   #Displays the flag
   flag = ImageTk.PhotoImage(flag) 
   c.image = flag #This is the line I added
   c.create_image(450, 300, image = flag, anchor = "s")

def info():
   .....
homepage()

推荐阅读