我正在使用tkinter和PIL在GUI上显示一些图像当我在__init__
函数中打开图像时,在self
外部使用它可以工作,但是当我在函数内部打开图像并在同一函数中使用时尽管图像在文件夹中,但基本上不会出现任何错误的图像。
这是代码
import datetime
import time
import os
from tkinter import *
import tkinter
from PIL import Image, ImageTk
from json_helper import JSONHelper
from babel.dates import format_date, format_datetime, format_time
class GUI():
def __init__(self, configs, settings):
self.configs=configs
self.settings=settings
self.root = Tk()
#self.root.attributes('-fullscreen', True)
self.canvas = Canvas(self.root, width=480, height=800)
self.canvas.pack()
def _days(self, x, y, anc, visibility):
day_list= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for day_item in day_list:
response_days = self.settings.Weather.Days
#These are from config file. So you can print 7 of them.
day_name = getattr(self.configs.Gui.Weather.Days, day_item) # pzt,sali,cars
self.canvas.create_text(x, y, fill= "white", font = "Corbel 16", anchor = anc, state= NORMAL, text = day_name)
for response_day in response_days:
if response_day.Day == day_item:
weather_image = getattr(self.configs.Gui.Weather.Images, response_day.Text)
opened_image = Image.open(self.configs.Folders.Gui + str(weather_image.Small))
image1 = ImageTk.PhotoImage(opened_image)
print(self.configs.Folders.Gui + str(weather_image.Small))
self.border(self.canvas.create_image(x, y+30, image=self.image_sunny_small, anchor=anc ,state=NORMAL))
self.canvas.create_text(x, y+60, fill= "white", font = "Corbel 16", anchor = anc, state= NORMAL, text = response_day.Degree)
x+=62
这是文件夹
~/Desktop/hodor/src/images/gui $ ls
cloudy_medium logo main_page_layer rainy_medium snowy_medium sunny_medium today
cloudy_small main_page profile_page rainy_small snowy_small sunny_small