EvilZone
Programming and Scripting => Scripting Languages => Topic started by: cat_vs_mouse on April 06, 2012, 01:18:23 PM
-
Hey. Can anyone help me with this Python code. I have created it as a sort of test using Python GUIs but I can't get all the images to display. When I run the script, only one of the images appears. If I get rid of that image then another one appears. There are three images that I want to display at the same time. How can I do this?
#!/usr/bin/python
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
#You need to create logo.gif,
photo = PhotoImage(file="logo.gif")
w = Label(frame, image=photo)
w.photo = photo
w.pack()
#quitbut.gif and
xphoto = PhotoImage(file="quitbut.gif")
w.photo = xphoto
w.pack()
self.button = Button(frame, image=xphoto, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
#hellobut.gif
yphoto = PhotoImage(file="hellobut.gif")
w.photo = yphoto
w.pack()
self.hi_there = Button(frame, image=yphoto, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "Hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
-
I use Wxpython but I think...and Im probably wrong... but it looks as if w.pack just keeps on running, it seems as if your code doesnt get past w.pack().
-
I use Wxpython but I think...and Im probably wrong... but it looks as if w.pack just keeps on running, it seems as if your code doesnt get past w.pack().
Thanks. This doesn't seem to be the problem ( :( ) 'cause I removed the w.pack()s and it still is happening :'( . +1 anyway; thanks for trying.
-
You should learn about classes and methods.
The problem you where having is, you kept referencing the Label class to keep track of all images.
Each image should be kept with it's respected widget. The Label doesn't care about the buttons in the root frame, and can't have children inheritances.
In the working code below, really study how I have it set up. The code is a bit different, with proper variable names and structure. Remember to keep things together as much as you can, and load anything intensive at the beginning of the code, like images/video/large files/etc...
from Tkinter import *
class app:
def __init__(self, master):
#create the root window
frame = Frame(master)
frame.pack()
#load images into memory
logo = PhotoImage(file="logo.gif")
quit = PhotoImage(file="quitbut.gif")
hello = PhotoImage(file="hellobut.gif")
#set up label
logoLabel = Label(frame, image=logo)
logoLabel.image = logo #****
logoLabel.pack()
#set up buttons
exitButton = Button(frame, image=quit, fg="red", command=frame.quit)
exitButton.image = quit #****
exitButton.pack(side=LEFT)
helloButton = Button(frame, image=hello, command=self.say_high)
helloButton.image = hello #****
helloButton.pack(side=LEFT)
def say_high(self):
print "Hello world!"
root = Tk()
myApp = app(root)
root.mainloop()
The commented lines with #****, in Tk you need a reference from the class that's using it, so it can store it in cache so the garbage collector doesn't erase what's in memory.
And this is only one way to skin the cat.
-
You should learn about classes and methods.
The problem you where having is, you kept referencing the Label class to keep track of all images.
Each image should be kept with it's respected widget. The Label doesn't care about the buttons in the root frame, and can't have children inheritances.
In the working code below, really study how I have it set up. The code is a bit different, with proper variable names and structure. Remember to keep things together as much as you can, and load anything intensive at the beginning of the code, like images/video/large files/etc...
from Tkinter import *
class app:
def __init__(self, master):
#create the root window
frame = Frame(master)
frame.pack()
#load images into memory
logo = PhotoImage(file="logo.gif")
quit = PhotoImage(file="quitbut.gif")
hello = PhotoImage(file="hellobut.gif")
#set up label
logoLabel = Label(frame, image=logo)
logoLabel.image = logo #****
logoLabel.pack()
#set up buttons
exitButton = Button(frame, image=quit, fg="red", command=frame.quit)
exitButton.image = quit #****
exitButton.pack(side=LEFT)
helloButton = Button(frame, image=hello, command=self.say_high)
helloButton.image = hello #****
helloButton.pack(side=LEFT)
def say_high(self):
print "Hello world!"
root = Tk()
myApp = app(root)
root.mainloop()
The commented lines with #****, in Tk you need a reference from the class that's using it, so it can store it in cache so the garbage collector doesn't erase what's in memory.
And this is only one way to skin the cat.
Thanks; it's working great now! (+1)