我有这个代码:
from tkinter import *
import urllib
import urllib.request
from bs4 import BeautifulSoup
import Pydeck
import sys
from collections import defaultdict
root = Tk()
name=""
def buttonclicked():
name()
picture()
def name():
all_lists=[] #all lists
text = inputfield.get()
Pydeck.loadDatabase('DATABASE PATH')
cardName = Pydeck.getCardsFromName(text)
if not cardName == "":
c = Pydeck.Card(cardName)
tex.insert(END, c.name)
level="\nLevel %s" % c.level + " " + c.attribute + " " + c.typestring
tex.insert(END, level)
atk="\nAtk: %s" % c.attack
tex.insert(END, atk)
defe="\nDef: %s" % c.defense
tex.insert(END, defe)
typestring='\n%s' %c.typestring
desc='\n%s' %c.description
seperator='\n--------------------\n'
tex.insert(END, typestring)
tex.insert(END, desc)
tex.insert(END,seperator)
#--
tex.see(END) # Scroll if necessary
return c.cardID
def picture():
text = inputfield.get()
gifdir = "PICTURE FOLDERS PATH"
Pydeck.loadDatabase('DATABASE PATH')
cardName = Pydeck.getCardsFromName(text)
if not cardName == "":
c=Pydeck.Card(cardName)
filename='{}.gif' .format(c.cardID)
img = PhotoImage(file=gifdir+filename)
can = Canvas(root)
can.pack(fill=BOTH,side='top')
can.config(width=img.width(), height=img.height())
can.create_image(2, 2, image=img, anchor=NW)
tex=Text(root)
tex.pack(side='right')
inputfield = Entry(root)
inputfield.pack(side='bottom')
but = Button(root,text="Enter Name", command = buttonclicked) #Calls name function
but.pack(side='bottom')
text = inputfield.get()
root.mainloop()
好的,简而言之,该程序位于 Python 3.3 的 Tkinter 中。它要求用户输入怪物的名字,然后在数据库中搜索怪物并返回怪物的属性,这是在name
函数中完成的。我希望它显示我遇到麻烦的怪物的图片。因此,该picture
函数采用属性之一,特别是cardID
属性并将其用作 gif 文件的名称。但是它不显示图片,我不知道为什么。有谁知道为什么?