python - 无法在画布(Tkinter)中显示图像
问题描述
我一直在制作一个画图程序,现在我正在添加一个“打开”功能。这是五分钟前的结果:
不注意画图本身...所以我重新启动了这个东西,这次它只是留下一个空白屏幕,没有显示那样的图像...
这是代码:
def Open():
global Directory
Directory = filedialog.askopenfilename(initialdir="/Desktop", title="Open Image", filetypes=(("Portable Network Graphics","*.png"),("Joint Photographic Experts Group","*.jpg"),("all files","*.*")))
ImageOpened = Image.open(Directory)
Largeur, Hauteur = ImageOpened.size
if Largeur >= 1000 or Hauteur >= 1000:
messagebox.showwarning("Can't open image", "The image is too big!")
elif not Largeur >= 1000 or not Hauteur >= 1000:
Can.delete(ALL)
FinalImage = ImageTk.PhotoImage(ImageOpened)
Can.configure(width=Largeur, height=Hauteur)
WidthPosition = Largeur/2
WidthPosition = WidthPosition+2
HeightPosition = Hauteur/2
HeightPosition = HeightPosition+2
print (Largeur, Hauteur, WidthPosition, HeightPosition)
Can.create_image(WidthPosition,HeightPosition, image=FinalImage)
有人可以帮我吗?;-;
解决方案
刚刚解决了,一开始我忘记了global
... ^^'所以代码现在看起来像这样:
def Open():
global Directory, FinalImage
Directory = filedialog.askopenfilename(initialdir="/Desktop", title="Open Image", filetypes=(("Portable Network Graphics","*.png"),("Joint Photographic Experts Group","*.jpg"),("all files","*.*")))
ImageOpened = Image.open(Directory)
Largeur, Hauteur = ImageOpened.size
if Largeur >= 1000 or Hauteur >= 1000:
messagebox.showwarning("Can't open image", "The image is too big!")
elif not Largeur >= 1000 or not Hauteur >= 1000:
Can.delete(ALL)
FinalImage = ImageTk.PhotoImage(ImageOpened)
Can.configure(width=Largeur, height=Hauteur)
WidthPosition = Largeur/2
WidthPosition = WidthPosition+2
HeightPosition = Hauteur/2
HeightPosition = HeightPosition+2
print (Largeur, Hauteur, WidthPosition, HeightPosition)
Can.create_image(WidthPosition,HeightPosition, image=FinalImage)
推荐阅读
- mysql - 在更新插入新数据时,如果较旧,则更新时间戳
- admin - Infinispan 12.1.1.Final 在管理控制台(服务器管理控制台)中缺少缓存配置
- r - 将分组均值添加到具有分组频率和比例的数据框
- python - 调用 pandas describe 函数时排除一个值
- javascript - Google 登录显示错误 400:invalid_request Permission denied to generate login hint for target domain in Javascript
- android - Android 模拟器 - 如何在应用程序处于终止状态时测试通知
- python - 在 Earth Engine/GIS 中提取样带数据
- python - 删除重复项时重新排列字典列表的最惯用方法?
- odbc - 使用 SuiteAnalytics ODBC 从 Sql Server Licked 服务器连接到 NetSuite 时访问被拒绝
- flutter - 将颤动的 MediaQuery 值存储在另一个类中