Reputation: 4689
I have this code:
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()
Ok, so in short the the program is in Tkinter for Python 3.3. It asks the user to input a name of a monster, it then searches for the monster in the database and returns the monster's attributes this is done in the name
function. I want it to display a picture of the monster which is where I'm running into trouble. So the picture
function takes one of the attributes specifically the cardID
attribute and uses that as the name of the gif file. However it doesn't display the picture and I don't know why. Does anyone know why?
Upvotes: 0
Views: 2740
Reputation: 123541
In order to get a stripped-down version of your code to display a .gif test image, I had to add aglobal img
statement to thepicture()
function so thePhotoImage
object that variable name refers to would not be destroyed when the function returns.
It seems strange thatCanvas.create_image()
doesn't increment the reference count of theimage
object it's passed...looks like it's a documented bug.
Update: For reasons I don't completely understand, adding acan.img = img
right after thecan = Canvas(root)
in thepicture()
function accomplishes the same thing in a better way than creating a global namedimg
in my opinion.
Display from working version:
Upvotes: 3