-1

(Skip to hash-tags if in a hurry) This program will only work if it ends on the image showing. I want to use it as a function inside another looping program, but it will not work. It will display the stats of the Pokemon(p.whatever), but the image will not show. The image will show in IDLE Python 3.4, but not the terminal. I've been stuck on this for months.

Here is the program that works(in IDLE Python 3.4, not the terminal):

import pykemon
print('What are you looking for?')
askedpokemon = input()


pokemonInDatabase = False
while pokemonInDatabase == False:
    pokemonInDatabase = True
    try:
        if ('1' in askedpokemon) or ('2' in askedpokemon) or ('3' in askedpokemon) or ('4' in askedpokemon) or ('5' in askedpokemon) or ('6' in askedpokemon) or ('7' in askedpokemon) or ('8' in askedpokemon) or ('9' in askedpokemon):
            p = (pykemon.get(pokemon_id = askedpokemon))

        else:
            askedpokemon = askedpokemon.lower()
            p = (pykemon.get(pokemon = askedpokemon))
            #Turns askedpokemon into number
            askedpokemon = p.resource_uri
            askedpokemon = askedpokemon.replace('/api/v1/pokemon/',' ')
            askedpokemon = askedpokemon.replace('/',' ')
            askedpokemon = askedpokemon.strip()


    except pykemon.exceptions.ResourceNotFoundError:
        print(askedpokemon + " is not a valid Pokemon name or id number.")
        print('Try another')
        askedpokemon = input()
        pokemonInDatabase = False



print (p)

pTypes = (p.types)
for key, value in pTypes.items() :
    pTypes = str(key)
    print ('   Type: ' + pTypes)
print ('     HP: ' + str(p.hp))
print (' Attack: ' + str(p.attack))
print ('Defense: ' + str(p.defense))
print (' Sp Atk: ' + str(p.sp_atk))
print (' Sp Def: ' + str(p.sp_def))
print ('  Speed: ' + str(p.speed))
print ('Exp Yield: ' + str(p.exp))

#######################################################
import time
import urllib
import urllib.request
import tkinter as tk
root = tk.Tk()
url = "http://assets22.pokemon.com/assets/cms2/img/pokedex/full/526.png"

if len(askedpokemon) < 3:
    if len(askedpokemon) == 2:
        askedpokemon = ('0' + askedpokemon)
    if len(askedpokemon) == 1:
        askedpokemon = ('00' + askedpokemon)

url = url.replace('526', askedpokemon)
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)
label = tk.Label(image=image)
label.pack()
##########################################################

Below is the working program with its modules.

https://drive.google.com/file/d/0B3Q4wQpL0nDUYWFFSjV3cUhXVWc/view?usp=sharing

Anthony
  • 13
  • 5
  • 1
    I would bet this is the problem http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm . One of 100,000 places it comes up on SO: http://stackoverflow.com/a/15216402/2329988 . May not be the case... but I bet it is :) – en_Knight Mar 18 '16 at 21:10
  • @en_Knight: The code has the required `image = tk.PhoteImage(...)` as specified in the link. The problem is the lack of `root.mainloop()` in the code or `-i` in the command line. – Terry Jan Reedy Mar 18 '16 at 22:41
  • @TerryJanReedy glad you figured out what the problem was. I meant that he didn't store a link to the image, and that tk only uses weak references so the image can be gc'd when the frame stack frame is dropped. But I guess that isn't happening here, since the OP accepted your answer – en_Knight Mar 20 '16 at 20:37

1 Answers1

0

Here is an mcve that illustrates the problem. Call the file tem.py.

import tkinter as tk
root = tk.Tk()
image = tk.PhotoImage(file='python.png')
label = tk.Label(image=image)
label.pack()

When you run in a terminal, this runs, but the root window closes after label.pack(), before you can see it. Either put root.mainloop() at the end of the code or run with python -i tem.py (as IDLE, in effect, does). The -i says to switch from batch to interactive mode after the end of the program instead of closing. IDLE does this so one can interact with the live program before it is closed.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52