所以我正在使用 Tkinter 和 Breezypythongui 的组合来上课,但我找不到让它显示图像的方法。下面是我试图在其中显示图像的框架的代码。我使用 EasyFrame,添加面板,然后添加画布,但它不会绘制图像。
class Detail(EasyFrame):
'''Offers the backbone of the program GUI'''
global shipList
global hangarList
global wishList
global desiredShip
def __init__(self):
'''Sets up the window and the label.'''
self.detailsWindow = EasyFrame.__init__(self, title=desiredShip, width = 1200, height = 700, background="cornsilk4")
self.shipDetails()
def shipDetails(self):
'''Finds the ship's info and displays it along with the ship's image. Also defines the loaners for the desired ship.'''
global loaners
import os
import json
global programDirectory
os.chdir(programDirectory)
os.chdir("Ship Store/")
os.chdir(desiredShip)
file = open("shipInfo.txt", 'r')
infoJson = file.read()
file.close()
os.chdir(programDirectory)
shipInfo = json.loads(infoJson)
row = 0
column = -1
for element in shipInfo:
column += 1
relevantInfo = element + ": " + shipInfo[element]
self.textLabel = self.addLabel(text = relevantInfo, row = row, column = column, sticky = "NEWS")
if column == 2:
row += 1
column = -1
loaners = shipInfo["Loaners"]
loaners = loaners.split(", ")
os.chdir("Ship Store/"+desiredShip)
panel = self.addPanel(row = 3, column = 0, columnspan = 3)
canvas = panel.addCanvas(row = 3, column = 0, width = 500, height = 500)
img = Image.open(desiredShip+".jpg")
img = img.resize((1280,720), Image.LANCZOS)
img = ImageTk.PhotoImage(img)
canvas.drawImage(x = 20, y = 20, anchor=N+W, image=img)
os.chdir(programDirectory)
我曾尝试将其更改为完全 Tkinter,但它反馈“_tkinter.TclError:无法在内部使用几何管理器包。其中已经有由网格管理的从属”,我什至尝试删除调整大小以查看是否可能弄乱了某些东西起来,但没有效果。每次都给我留下一张空白的画布。