开发者

python - A strange problem when I'm trying to display an image in the Toplevel widget

I'm using Python(2.5) to make a "web album viewer".

I use Tkinter to write GUI, urllib2 to fetch the image from web, PIL to handle the image

User would click a button, and then it would create a Toplevel widget, download the picture, and display it in the Toplevel widget.

The button is bound to the "look" method.

So the code is like this:

class App:

  #some codes are omitted here

  def look(self, pic_url):
    top = Toplevel()
    more = More(top, pic_url)

class More:
  def __init__(self, master, pic_url):
    self.frame = Frame(master)
    self.frame.pack()
    response = urllib2.urlopen(pic_url)
    open("inner_temp.jpg", "wb+").write(response.read())
    self.picture = ImageTk.PhotoImage(file = "inner_temp.jpg")    
    self.photo_label = Label(self.frame, image = self.picture)
    self.photo_label.pack()

The Toplevel widget showed, but there's nothing inside. I found "inner_temp.jpg" was downloaded in the folder correctly, but it just didn't show.

But the strangest thing is if I type one whatever character in the end of the code, the console window would show error message, but the picture showed in the Toplevel widget!

  def look(self, pic_url):
    top = Toplevel()
    more = More(top, pic_url)

class More:
  def __init__(self, master, pic_url):
    s开发者_开发问答elf.frame = Frame(master)
    self.frame.pack()
    response = urllib2.urlopen(pic_url)
    open("inner_temp.jpg", "wb+").write(response.read())
    self.picture = ImageTk.PhotoImage(file = "inner_temp.jpg")    
    self.photo_label = Label(self.frame, image = self.picture)
    self.photo_label.pack()
    x

NameError: global name 'x' is not defined

How could this happen!? I really can't figure it out!

Can someone help me?

Thanks, and I'm sorry for my poor English.


Without seeing more code, my guess is that you are neglecting to start the event loop, which is necessary for widgets to refresh themselves. When you add the code that throws an error, that error triggers the event loop which causes the window to refresh.


My guess would be garbage collection. Try something like this:

class More:
  def __init__(self, master, pic_url):
    self.frame = Frame(master)
    self.frame.pack()
    response = urllib2.urlopen(pic_url)
    open("inner_temp.jpg", "wb+").write(response.read())
    self.frame.picture = ImageTk.PhotoImage(file = "inner_temp.jpg")    
    self.photo_label = Label(self.frame, image = self.frame.picture)
    self.photo_label.pack()


Thank you all very much! Your answers and comments help me solve the problem. I finally found the problem! The problem is here: this code

    class App:

  #some codes are omitted here

  def look(self, pic_url):
    top = Toplevel()
    more = More(top, pic_url)

should be

    class App:

  #some codes are omitted here

  def look(self, pic_url):
    top = Toplevel()
    self.more = More(top, pic_url)

Although I'm not familiar with the garbage collection mechanism of Python, I think the reason why the picture only shows if the error happens is because the error stops the garbage collection from clearing some part of the local variable "more". And replacing the local variable with instance variable "self.more" solves this problem!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜