我的目标是创建一个随机国家生成器,并显示选择的国家的国旗。但是,如果图像文件大于标签的预定大小,则只会显示部分图像。是否有一种方法可以调整图像大小以适合标签?(我看到的所有类似问题的答案都提到了PIL或Image模块。我测试过它们两个,结果都出现了这个错误:
Traceback (most recent call last): File "C:\python\country.py", line 6, in import PIL ImportError: No module named 'PIL'
以下是我的代码,如果有帮助的话:
import tkinter
from tkinter import *
import random
flags = ['England','Wales','Scotland','Northern Ireland','Republic of Ireland']
def newcountry():
country = random.choice(flags)
flagLabel.config(text=country)
if country == "England":
flagpicture.config(image=England)
elif country == "Wales":
flagpicture.config(image=Wales)
elif country == "Scotland":
flagpicture.config(image=Scotland)
elif country == "Northern Ireland":
flagpicture.config(image=NorthernIreland)
else:
flagpicture.config(image=Ireland)
root = tkinter.Tk()
root.title("Country Generator")
England = tkinter.PhotoImage(file="england.gif")
Wales = tkinter.PhotoImage(file="wales.gif")
Scotland = tkinter.PhotoImage(file="scotland.gif")
NorthernIreland = tkinter.PhotoImage(file="northern ireland.gif")
Ireland = tkinter.PhotoImage(file="republic of ireland.gif")
blackscreen = tkinter.PhotoImage(file="black screen.gif")
flagLabel = tkinter.Label(root, text="",font=('Helvetica',40))
flagLabel.pack()
flagpicture = tkinter.Label(root,image=blackscreen,height=150,width=150)
flagpicture.pack()
newflagButton = tkinter.Button(text="Next Country",command=newcountry)
newflagButton.pack()
这段代码除了只显示图像的一部分之外,运行得非常好。是否有一种方法可以在代码本身内调整图像的大小?(我使用的是Python 3.5.1)