I was wondering how Python would work with this program I wanted to make. I have very little experience with Python, so bare with me. What I wanted was to make a program that brought up quotes randomly from a .txt file and put them in a XP/Vista message bubble from the icon of the program in the system tray. It wouldn't have a traditional GUI, just the icon in the system tray with the bubble with the quote in it every four hours from start up. I imagine Python could bring up random quotes in a window pretty easily, I just don't know if it will allow me to have the tooltip thing from the system tray.

I had asked this question on another forum, and I had a lot of posts saying that the program was a horribly stupid and useless idea. It's actually my first experiment with coding anything besides html/CSS/PHP/XML, considering I started in web design and never had the opportunity for anything else, because I picked up everything I know through experience. The other reason I wanted to make this program is that my best friend had asked me to, so I am.

I've got the ability to write the code with Eclipse/PyDev and I'm using Windows for the moment, until I get sick of it.

I wouldn't know how to put it into a windows program, but a random quote generator was my first program too!

They way I did it was make a dictionary, the quote was the key, the value was the author.

EDIT: Oh, wait I see what you mean about system tray pop up bubbles. Yep, that's way over my head, but sounds fun.

Anyway, here's essentially what I wrote for my first python experiment, minus the dictionary, adding a class.

import random
quotefile = open('quotes.txt','r')
linelist = quotefile.read().split('\n') #read the file however you'd like, this is just how i sometimes do it
quotefile.close()

class Quote:
[INDENT]def __init__(self,quote,author):
[INDENT]self.quote = quote
self.author = author[/INDENT]
def __str__(self):
[INDENT]s = '"%s" -%s' %(self.quote,self.author)[/INDENT][/INDENT]

quotelist = []
for line in linelist:
[INDENT]thesplit = line.split()
quote = Quote(thesplit[0],thesplit[1])
quotelist.append(quote)[/INDENT]

quotechoice = random.choice(quotelist)
print quotechoice

Note that you'd have to change the line splitting or file reading parts depending on how your txt file is written.

For the "GUI" portion of your program you'll likely want to look into wxPython. There are a number of other toolkits for creating GUIs but wx is by far the most native-looking so it'll get you a very clean looking interface that looks like it was built right into Windows. It'll be impressive for your friend I'm sure.

import random
quotefile = open('quotes.txt','r')
linelist = quotefile.read().split('\n') #read the file however you'd like, this is just how i sometimes do it
quotefile.close()

class Quote:
[INDENT]def __init__(self,quote,author):
[INDENT]self.quote = quote
self.author = author[/INDENT]
def __str__(self):
[INDENT]s = '"%s" -%s' %(self.quote,self.author)[/INDENT][/INDENT]

quotelist = []
for line in linelist:
[INDENT]thesplit = line.split()
-- quote = Quote(thesplit[0],thesplit[1]) --
quotelist.append(quote)[/INDENT]

quotechoice = random.choice(quotelist)
print quotechoice

A couple of questions. One, I would have to put "/n" in my quote.txt at the end, beginning or both of each quote? Two, I get an error when trying to run the .py on line 22. I'm assuming I didn't specify the correct file for the quote.txt, but I don't know. The quote.txt file is in the correct workspace folder.

EDIT: I get the error:

IndexError: List index out of range

I assume that means I didn't index/split up the quotes.txt correctly. I've tried a bunch of different ways of doing it, but I can't get it to work.

Adding these print statements should shed some light on this if I understand what you want to do. Run it this way and if it does not give you an idea of what is happening then post back.

quotelist = []
for line in linelist:
     thesplit = line.split()
     print "\nline"
     print "     split into", thesplit
##     -- quote = Quote(thesplit[0],thesplit[1]) --
##     quotelist.append(quote)

Sorry bout that, a comment might have made that more clear.

A better way to read a file:

def readafile(file):
[INDENT]linelist = []
while 1:
[INDENT]line = file.readline()
if not line:
[INDENT]break[/INDENT]
linelist.append(line)[/INDENT]
return linelist[/INDENT]

The file reading part depends on how your file is formatted. BTW, '\n' (note the slash direction) is a linebreak. So a text file looking like this:

Hi, this is line 1. Moving along.
Line 2, super cool.

So, let's say this is our code:

file = open('quotes.txt')
wholefile = file.read()

wholefile is a string:

'Hi, this is line1. Moving along.\nLine2, super cool'

so

linelist = wholefile.split('\n')

should make linelist a list containing:

Anyway, stick some print statements in there so you can figure out what went wrong, what I like to do is use the IDLE Python GUI where you get those fun little

>>>

and see if each function is returning what it is supposed to.

PS: If you're getting an index error, chances are theres a line of whitespace at the end of your file, you can wrap the code in a try except but it may be easier to just take the whitespace out. ;)

-zach

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.