Author Topic: Can anyone help me with this code?  (Read 1196 times)

0 Members and 1 Guest are viewing this topic.

Offline cat_vs_mouse

  • /dev/null
  • *
  • Posts: 17
  • Cookies: 0
  • Learning Ruby (and loving it :D)
    • View Profile
Can anyone help me with this code?
« 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?


Code: [Select]
#!/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()
cat_vs_mouse - Thanks for reading!

Quote from: techb
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
All the kings horses and all the kings men said, "Fuck him, he's just an egg."

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: Can anyone help me with this code?
« Reply #1 on: April 06, 2012, 05:59:21 pm »
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().

Offline cat_vs_mouse

  • /dev/null
  • *
  • Posts: 17
  • Cookies: 0
  • Learning Ruby (and loving it :D)
    • View Profile
Re: Can anyone help me with this code?
« Reply #2 on: April 06, 2012, 06:57:33 pm »
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.
« Last Edit: April 06, 2012, 06:58:37 pm by cat_vs_mouse »
cat_vs_mouse - Thanks for reading!

Quote from: techb
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
All the kings horses and all the kings men said, "Fuck him, he's just an egg."

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Can anyone help me with this code?
« Reply #3 on: April 06, 2012, 09:10:13 pm »
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...


Code: [Select]

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.
« Last Edit: April 06, 2012, 09:13:12 pm by techb »
>>>import this
-----------------------------

Offline cat_vs_mouse

  • /dev/null
  • *
  • Posts: 17
  • Cookies: 0
  • Learning Ruby (and loving it :D)
    • View Profile
Re: Can anyone help me with this code?
« Reply #4 on: April 06, 2012, 10:46:16 pm »
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...


Code: [Select]

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)
cat_vs_mouse - Thanks for reading!

Quote from: techb
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
All the kings horses and all the kings men said, "Fuck him, he's just an egg."