[英]How to edit a background image in tkinter in python?
我嘗試編輯tkinter代碼的背景圖片,但是即使我將不同的圖片重命名為“ landscape.png”,我也無法做到,無論其他圖片變大還是變小。 我不知道我能做些什么來改善這個問題,就像這樣的代碼僅適用於一種類型的圖片,我不知道為什么。 我現在正嘗試處理此問題3天,這是我的最后幫助手段
我已經嘗試了上面
import tkinter as tk
import requests
HEIGHT = 500
WIDTH = 600
def test_function(entry):
print("This is the entry:", entry)
# api.openweathermap.org/data/2.5/forecast?q={city name},{country code}
# a4aa5e3d83ffefaba8c00284de6ef7c3
def format_response(weather):
try:
name = weather['name']
desc = weather['weather'][0]['description']
temp = weather['main']['temp']
final_str = 'City: %s \nConditions: %s \nTemperature (°F): %s' % (name, desc, temp)
except:
final_str = 'There was a problem retrieving that information'
return final_str
def get_weather(city):
weather_key = 'a4aa5e3d83ffefaba8c00284de6ef7c3'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': weather_key, 'q': city, 'units': 'imperial'}
response = requests.get(url, params=params)
weather = response.json()
label['text'] = format_response(weather)
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file='landscape.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
frame = tk.Frame(root, bg='#80c1ff', bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
entry = tk.Entry(frame, font=40)
entry.place(relwidth=0.65, relheight=1)
button = tk.Button(frame, text="Get Weather", font=40, command=lambda: get_weather(entry.get()))
button.place(relx=0.7, relheight=1, relwidth=0.3)
lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
label = tk.Label(lower_frame)
label.place(relwidth=1, relheight=1)
root.mainloop()
我希望顯示另一張圖像,但出現此錯誤:
Traceback (most recent call last):
File "/Users/michal/Desktop/GUI-master/WeatherApp.py", line 41, in <module>
background_image = tk.PhotoImage(file='landscape.png')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3501, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "landscape.png"
>>>
PhotoImage類可以從文件讀取
GIF
和PGM/PPM
圖像:
photo = PhotoImage(file="image.gif")
photo = PhotoImage(file="image.pgm")
如果您需要使用其他文件格式,則
Python Imaging
Library (PIL)
包含一些類,這些類使您可以加載30多種格式的圖像,並將其轉換為Tkinter兼容的圖像對象:
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
您可以在Tkinter接受圖像對象的任何地方使用PhotoImage實例。
一個例子:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
在您的代碼中:
from PIL import Image, ImageTk
background_image = Image.open(image_path)
background_photo = ImageTk.PhotoImage(background_image)
background_label = tk.Label(root, image = background_photo)
background_label.image = background_photo
background_label.place(relwidth=1, relheight=1)
注意:如果您尚未安裝PIL,只需從cmd安裝它:
pip install Pillow
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.