图像不会循环显示,为什么不呢?

时间:2016-03-18 21:07:40

标签: image python-3.x tkinter

(如果赶时间,请跳至哈希标签) 该程序仅在图像显示结束时才有效。 我想将它用作另一个循环程序中的函数,但它不起作用。它将显示宠物小精灵的统计数据(p.whatever),但图像不会显示。该图像将显示在IDLE Python 3.4中,但不会显示在终端上。几个月来我一直坚持这个。

这是有效的程序(在IDLE Python 3.4中,而不是终端):

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()
##########################################################

以下是带有模块的工作程序。

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

1 个答案:

答案 0 :(得分:0)

这是一个说明问题的mcve。调用文件tem.py

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

当您在终端中运行时,它会运行,但根窗口会在label.pack()之后关闭,然后才能看到它。将root.mainloop()放在代码的末尾,或者使用python -i tem.py运行(实际上是IDLE)。 -i表示在程序结束后从批处理切换到交互模式而不是关闭。 IDLE执行此操作,因此可以在关闭之前与实时程序进行交互。

相关问题