使用Python中的Tkinter加载图像和文本

2024-10-17 08:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我尝试运行一个代码,以采取一个图像,并调整其大小,然后打开它使用一个tkinter框架与一个文本一起。在

root2 = Tk. Tk()
filename = "pichart.png"
basewidth = 500
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
os.remove(filename) # deletes the original image after you have got the resized image
img.save('pichart.png')
bard = Image.open("pichart.png")
bardejov = ImageTk.PhotoImage(bard)
label1 = Tk.Label(root2,image=bardejov)
label1.image = bardejov
label1.place(x=20, y=20)
samp="sfaagdgaegadgadfasasdasda"
label2 = Label(root2, text=samp)
label2.place(x=600, y=20)
root2.geometry("%dx%d+%d+%d" % (1400, 700, 00, 00))
root2.mainloop()

图像被转换并保存,但在显示时出现错误:

^{pr2}$

虽然单独运行显示片段可以很好地处理directory中的image。在


Tags: 图像imageimgpngopenfloatfilenametk