1
General discussion / Re: Which OS?
« on: April 17, 2012, 07:52:42 AM »
Thanks, Ubuntu it is!
![Cheesy :D](http://ez.feedthetrolls.net/Smileys/default/cheesy.gif)
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
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.
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().
#!/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 am assuming your using Tkinter. To get text from the Entry widget, use it's "get" method.Code: [Select]
entry_widget = Entry(some_parent_frame)
entry_widget.pack(side=RIGHT)
....
my_text = entry_widget.get()
In a nutshell. You will need to know the parent frame, which could be root, or another widget. You might have some other formatting thrown in as well, but you should get the point.
Wait what. You made all that and only NOW you will learn about variables? am I the only one that find it interesting?
Making an IRC client is a nice thing. While it may not be very useful it sure it lots of experience. I remember when I was doing HawtBawt (in delphi) which was an IRC bot. I didn't finish it but it works, somewhat. You can check my code for details if you want.
http://docs.python.org/library/sched.html
http://docs.python.org/library/os.html#os-process