...

View Full Version : Tkinter image problem



idalatob
10-18-2008, 04:51 PM
Hey guys,

First of all im a relative newb when it comes to working with python, so go easy on me:thumbsup:

I have a problem loading up images using the Label function, heres how my program works:

1.) Root window opens
2.) Image loads
3.) User selects their character.
4.) Root window is hidden & new window is opened(using Toplevel)
5.) Image of character is loaded.

The problem is the image loads fine the first time, but in the second window it refuses saying: TclError: image "empress.gif" doesn't exist.

I havent used Photoimage to load the image. I just used a direct filename.

Could someone help me out with this one?

shyam
10-18-2008, 05:06 PM
where is the code thats doing all this?

idalatob
11-06-2008, 11:37 AM
from Tkinter import *
from char_settings import *
class Application(Frame):
global battlewindow
def __init__(self, master):
Frame.__init__(self, master, height=2, bd=2, bg="white")
self.pack()
self.grid_configure(columnspan=4)
self.char_image = Label(self,image=nochar_iconImage)
self.char_image.grid(row=5,column=0,columnspan=1)
self.resetImage()
self.writeup = Text(self, width=50, height=9, wrap=WORD)
self.writeup.insert(0.0,"Please select a character to find out more.")
self.writeup.grid(row=5,column=1,columnspan=3 ,sticky=W)
self.cc()
Label(self,image=logo_iconImage).grid(row=0,column=0,columnspan=4)
def resetImage(self):
self.char_image["image"] = nochar_iconImage
def cc(self):
output = "Welcome to The African Quest, please create your character."
self.lbl = Label(self, text = str(output), bg="white", fg="blue",font="6")
self.lbl.grid(row = 1, column=0, columnspan=4)
self.name_lbl = Label(self, text = "Your Name:", bg="white")
self.name_lbl.grid(row = 2, column=0, sticky=W)
self.user_name = Entry(self,bg="white")
self.user_name.grid(row=2, column=1, columnspan=2, sticky=W)
self.gender_lbl = Label(self, text = "Gender:", bg="white")
self.gender_lbl.grid(row = 3, column=0, columnspan=2, sticky=W)
self.gender = StringVar()
Radiobutton(self, text="Male",variable= self.gender, value="Male",command= self.load_chars, bg="white").grid(row = 3, column=1, sticky=W)
Radiobutton(self, text="Female",variable= self.gender, value="Female",command= self.load_chars, bg="white").grid(row = 3, column=2, sticky=W)
def load_chars(self):
self.resetImage()
self.char_type_lbl = Label(self, text = "Character Type:", bg="white")
self.char_type_lbl.grid(row = 4, column=0, columnspan=2, sticky=W)
self.char_type = StringVar()
Radiobutton(self, text="Empress/Emperor",variable= self.char_type, value="1",command= self.char_details, bg="white").grid(row = 4, column=1, sticky=W)
Radiobutton(self, text="Thug",variable= self.char_type, value="2",command= self.char_details, bg="white").grid(row = 4, column=2, sticky=W)
Radiobutton(self, text="Thief",variable= self.char_type, value="3",command= self.char_details, bg="white").grid(row = 4, column=3, sticky=W)
def char_details(self):
if (self.char_type.get() == "1"):
if (self.gender.get() == "Male"):
self.new_char_image = self.gen_image(characters[0][3])
self.char_image["image"] = self.new_char_image
self.writeup.delete(0.0, END)
self.writeup.insert(0.0,"These are the details about the emporer")
extra = "Brute Force->" + characters[0][0] + "\n"
extra += "Intuition->" + characters[0][1] + "\n"
extra += "Athleticism->" + characters[0][2] + "\n"
self.writeup.insert(0.0,str(extra))
self.select_button()
else:
self.new_char_image = self.gen_image(characters[0][3])
self.char_image["image"] = self.new_char_image
self.writeup.delete(0.0, END)
self.writeup.insert(0.0,"These are the details about the empress")
extra = "Brute Force->" + characters[0][0] + "\n"
extra += "Intuition->" + characters[0][1] + "\n"
extra += "Athleticism->" + characters[0][2] + "\n"
self.writeup.insert(0.0,str(extra))
self.select_button()
elif (self.char_type.get() == "2"):
self.new_char_image = self.gen_image(characters[1][3])
self.char_image["image"] = self.new_char_image
self.writeup.delete(0.0, END)
self.writeup.insert(0.0,"These are the details about the thug")
extra = "Brute Force->" + characters[1][0] + "\n"
extra += "Intuition->" + characters[1][1] + "\n"
extra += "Athleticism->" + characters[1][2] + "\n"
self.writeup.insert(0.0,str(extra))
self.select_button()
else:
self.new_char_image = self.gen_image(characters[2][3])
self.char_image["image"] = self.new_char_image
self.writeup.delete(0.0, END)
self.writeup.insert(0.0,"These are the details about the thief.")
extra = "Brute Force->" + characters[2][0] + "\n"
extra += "Intuition->" + characters[2][1] + "\n"
extra += "Athleticism->" + characters[2][2] + "\n"
self.writeup.insert(0.0,str(extra))
self.select_button()
def gen_image(self,img_name):
self.image = PhotoImage(master=root, file=img_name)
return self.image
def select_button(self):
self.select_char_button = Button(text="Choose this Character", fg="brown",bg="white", command=self.select_char)
if (self.char_type.get() == "1") or (self.char_type.get() == "2") or (self.char_type.get() == "3") and (self.gender.get() == "Male") or (self.gender.get() == "Female"):
self.select_char_button.grid(row="6",column="0",columnspan="4")
else:
self.select_char_button.destroy()
def select_char(self):
self.chosen_char = [self.user_name.get(),self.gender.get(),self.char_type.get()]
root.withdraw()
battlewindow = Toplevel()
battlewindow.title("On the road")
bw = battlemain(battlewindow,self.chosen_char)
battlewindow.mainloop()
class battlemain(Frame):
global chosen_char
def __init__(self,master,chosen_char):
self.chosen_char = chosen_char
Frame.__init__(self, master,width="650", height="500", bd=2, bg="white")
self.pack()
self.psn()
#load the picture, stats & name
def psn(self):
char_key = int(self.chosen_char[2]) - 1
self.char_picture = characters[char_key][3]
imagey = PhotoImage(master=root,file = self.char_picture)
self.main_user_image = Label(self,image=self.char_picture)
self.main_user_image.grid(row=0,column=0,columnspan=1, sticky=W)
# set the characters stats.
# in the form: bruteforce, intuition, athleticism
root = Tk()
logo_iconImage = PhotoImage(master=root, file="logo.gif")
nochar_iconImage = PhotoImage(master=root, file="no_char.gif")
char_det = Char_details()
characters = char_det.return_char_details()
root.title("The African Quest")
app = Application(root)
root.mainloop()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum