Python 3.3 TKinter映像不存在

编程入门 行业动态 更新时间:2024-10-23 08:37:10
本文介绍了Python 3.3 TKinter映像不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用来自PIL的ImageTk.PhotoImage打开图像,但是我一直遇到相同的错误.我有图片,可以看到它在下载时就打开了,然后打开了,但是然后我得到一个错误,它不在那里.

I am trying to open an image with ImageTk.PhotoImage from PIL but I keep hitting the same error. I have the image and can see that it is there when it downloads it, it opens it, but then I get the error that it is not there.

这是错误

Exception in Tkinter callback Traceback (most recent call last): File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ return self.func(*args) File "S:/Projects/xmlParser.py", line 128, in updateSelected self.threadLabelImage.config(text = self.threadImage) File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure return self._configure('configure', cnf, kw) File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: image "pyimage1" doesn't exist

相关代码

if self.threadLinkList[self.y].find('imgur') != -1: url = self.threadLinkList[self.y]+'.gif' imageName=self.threadLinkList[self.y][-11:-4]+'.gif' print(imageName) urllib.request.urlretrieve(self.threadLinkList[self.y],imageName) imgfile = Image.open(imageName) imgfile = imgfile.resize((150,150),Image.ANTIALIAS) # img = Image.open(file) print(imgfile) self.threadImage = ImageTk.PhotoImage(imgfile) self.threadLabelImage.config(text = self.threadImage) self.threadImage.image = imgfile

要运行的整个程序.

import xml.etree.ElementTree as ET import webbrowser,time,urllib.request,re,os import tkinter as tk import urllib from PIL import Image,ImageTk main = tk.Tk() os.getcwd() class Application(tk.Frame): def __init__(self, master=None): self.threadTitle = tk.StringVar() self.threadAuth = tk.StringVar() self.threadPub = tk.StringVar() self.threadArtLink = tk.StringVar() self.threadLink = tk.StringVar() self.threadImg = tk.StringVar() self.threadArtLink.set('Click something to display thread info') photo = Image.open("temp.png") photo = photo.resize((150,150), Image.ANTIALIAS) self.threadImage = ImageTk.PhotoImage(photo) # Intializes tkinter gui framework tk.Frame.__init__(self, master) # Packs widgets needed self.grid() # Creates the widgets functions self.createWidgets() # Intializes the man rss.xml self.initial() # self.threadLabelArtLink = None # self.threadLabelTitle = None # self.threadLabelThreadLink = None # self.threadLabelArtLink = None # self.threadImgLink = None def createWidgets(self): # Create entrybox and align to grid self.send_entry = tk.Entry(self) self.send_entry.grid(row=0,column=0) # Create button,allign to grid, get xml self.change_sub = tk.Button(self,text='Change Subreddit',padx=5, pady=5, command=lambda :self.getXML(self.send_entry.get())) self.change_sub.grid(row=0 , column=3) # Create scrollbar on Y-Axis self.lb_scrollY = tk.Scrollbar(self,orient=tk.VERTICAL) # On grid next to Listbox(sticky means fill whole row self.lb_scrollY.grid(row=1,column=4,sticky=tk.NS,rowspan=6) # Create Listbox and get Y from scrollbar self.thread_lb = tk.Listbox(self,yscrollcommand=self.lb_scrollY.set,height=20) # Calls function whenever a new item is selected & open thread if double click 1 self.thread_lb.bind('<<ListboxSelect>>',self.updateSelected) self.thread_lb.bind('<Double-Button-1>',self.openPage) # scrolly will change the view of listbox self.lb_scrollY['command']=self.thread_lb.yview self.thread_lb.grid(row=1,column=0,sticky=tk.NS+tk.EW,columnspan=4) self.threadFrame = tk.LabelFrame(main,text='Reddit',width=450,height=350,labelanchor='n') self.threadLabelTitle = tk.Label(self.threadFrame,textvariable=self.threadTitle,wraplength=400,padx=20, pady=5) self.threadLabelTitle.grid(row=1,column=10,sticky= tk.EW) self.threadLabelAuth = tk.Label(self.threadFrame, textvariable=self.threadAuth,wraplength=400,padx=20, pady=5) self.threadLabelAuth.grid(row=2,column=10,sticky = tk.EW) self.threadLabelPub = tk.Label(self.threadFrame, textvariable=self.threadPub,wraplength=400,padx=20, pady=5) self.threadLabelPub.grid(row=3,column=10,sticky = tk.EW) self.threadLabelArtLink = tk.Label(self.threadFrame, textvariable=self.threadArtLink,wraplength=400,padx=20, pady=5) self.threadLabelArtLink.grid(row=4,column=10,sticky = tk.EW) self.threadLabelThreadLink = tk.Label(self.threadFrame, textvariable=self.threadLink,wraplength=400,padx=20, pady=5) self.threadLabelThreadLink.grid(row=5,column=10,sticky = tk.EW) self.threadImgLink = tk.Label(self.threadFrame, textvariable=self.threadImg,wraplength=400,padx=20, pady=5) self.threadImgLink.grid(row=6,column=10,sticky = tk.EW) self.threadLabelImage = tk.Label(self.threadFrame,image=self.threadImage,wraplength=400,padx=20,pady=5) self.threadLabelImage.grid(row=7,column=10,sticky = tk.EW) self.threadFrame.grid(row=0,column=10,sticky=tk.EW,rowspan=8) self.QUIT = tk.Button(self, text="QUIT", fg="red", command=main.destroy,padx=5, pady=5) self.QUIT.grid(row=7) self.threadFrame.grid_propagate(0) def updateSelected(self, event): # getting selected listbox item i=self.thread_lb.curselection() # Returns tuple that must be split x,self.y,z = re.split("\D+",str(i)) self.y=int(self.y) print(self.threadTitleList[self.y]) print(self.threadPubDateList[self.y]) print(self.threadLinkList[self.y]) print(self.threadDescList[self.y]) self.threadTitle.set(self.threadTitleList[self.y]) self.threadAuth.set(self.threadAuthList[self.y]) self.threadPub.set(self.threadPubDateList[self.y]) self.threadArtLink.set(self.threadLinkList[self.y]) self.threadLink.set(self.threadDescList[self.y]) # self.threadImg.set('Will put image here') if self.threadLinkList[self.y].find('imgur') != -1: url = self.threadLinkList[self.y]+'.gif' imageName=self.threadLinkList[self.y][-11:-4]+'.gif' print(imageName) urllib.request.urlretrieve(self.threadLinkList[self.y],imageName) imgfile = Image.open(imageName) imgfile = imgfile.resize((150,150),Image.ANTIALIAS) # img = Image.open(file) print(imgfile) self.threadImage = ImageTk.PhotoImage(imgfile) self.threadLabelImage.config(text = self.threadImage) self.threadImage.image = imgfile # # threadTitle = self.threadTitleList[y] # print(self.threadLabelTitle["text"]) # # self.threadLabelTitle['text']=threadTitle # self.threadLabelAutPub['text']=self.threadPubDateList[y] # self.threadImgLink['text']=self.threadLinkList[y] # self.threadLabelThreadLink['text']=self.threadDescList[y] # main.update() def openPage(self,event): webbrowser.get('windows-default').open_new(self.threadLinkList[self.y]) def descStripper(self,desc): # Intialize values l1,l2,l2Start = 0,0,0 t1,t2,t2start = 0,0,0 link = "" thread = "" # Where to start looking for each in description element l1=int(desc.find('<br/> <a href="')) t1=int(desc.find('</a> <a href="')) a1=int(desc.find('"> ')) # If both of the tags are found then continue if l1 != -1 and t1 != -1 and a1 != 1: # Start looking for end of quotes 16 characters from beginning of tag l2Start = l1+16 l2=int(desc.find('"',l2Start)) # Link is created from what is in the quotes link = desc[l1+15:l2] # Same as above but to find thread link t2start = t1+15 t2=int(desc.find('"',t2start)) thread = desc[t1+14:t2] a2start = a1+4 a2 = int(desc.find(' <',a2start)) author = desc[a1+3:a2] return link,thread,author else: # If it can't find one it will return an error link = "Couldn't find the stuff :(" thread = "Couldn't find the thread link :(" return link, thread def lbPopulator(self,title,pub,link): # Delete old entries from listbox self.thread_lb.delete(0,tk.END) # Iterate through all the items and append them to the listbox for item in title: self.thread_lb.insert(tk.END,item) def getXmlData(self): # Intialize lists self.threadPubDateList = [] self.threadTitleList = [] self.threadLinkList = [] self.threadDescList = [] self.threadThumbNailList = [] self.threadAuthList = [] # Use the downloaded rss.xml for XML parsing tree=ET.parse('rss.xml') # define root as the base of the XML parsing tree root=tree.getroot() for channel in root: # Iterate through all the channels for SubChannel in channel: # Iterate through all the items in the channel if SubChannel.tag == 'item': # If the SubChannel is called item then search for the items below for threadInfo in SubChannel: # iterate through all the items in the 'item' if threadInfo.tag == 'title': # append the tag from the title to the list self.threadTitleList.append(threadInfo.text) if threadInfo.tag == 'pubDate': # Append the pubdate info to the list but remove excess characters self.threadPubDateList.append(threadInfo.text[:-6]) if threadInfo.tag == 'description': # Pass all the information from the description to the stripper to get the useful # information and links link,thread,author = self.descStripper(threadInfo.text) self.threadLinkList.append(link) self.threadDescList.append(thread) self.threadAuthList.append(author) # if threadInfo.tag == '' # Populate the listbox with the newly generated lists self.lbPopulator(self.threadTitleList,self.threadPubDateList,self.threadLinkList) def getXML(self,subreddit): try: # Try to download the xml file using the user input subreddit url = 'www.reddit'+subreddit+'.rss' source = urllib.request.urlretrieve(url,'rss.xml') self.getXmlData() except urllib.error.HTTPError as err: # Error caused by reddit API limiting connections print('Too many requests-Try again') def initial(self): try: # Same as above but downloads the front page source = urllib.request.urlretrieve('www.reddit/.rss','rss.xml') self.getXmlData() except urllib.error.HTTPError as err: print('Too many requests-Trying again 3') # If error occurs program waits 3 seconds and then restarts time.sleep(3) self.__init__() # main.geometry("350x400") app = Application(master=main) # Begins the applications GUI loop app.mainloop()

推荐答案

我试图将图像应用于图像标签的text属性.这是固定代码.

I was attempting to apply the image to the text attribute of the label instaed of the image. Here is the fixed code.

url = self.threadLinkList[self.y]+'.gif' imageName=self.threadLinkList[self.y][-11:-4]+'.gif' print(imageName) urllib.request.urlretrieve(self.threadLinkList[self.y],imageName) imgfile = Image.open(imageName) imgfile = imgfile.resize((150,150),Image.ANTIALIAS) # img = Image.open(file) print(imgfile) self.threadImage = ImageTk.PhotoImage(imgfile) self.threadLabelImage.config(***image*** = self.threadImage) self.threadImage.image = imgfile

更多推荐

Python 3.3 TKinter映像不存在

本文发布于:2023-10-07 14:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   映像   Python   TKinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!