So far our RSS reader has been completely command-line, which is functional but not as nice as we’d really like to have it. So what we are going to do is integrate the GUI that I created in my two Tkinter GUI tutorials into our RSS application.
If you look at our GUI application you can see that at this point it actually has the basic look that we’ll want, what it doesn’t have is the functionality attached to to GUI. So the first thing that we are going to do is let the user add an RSS site when the press the Add button. We are going to pop up a dialog and let them enter the name of the RSS site and the address of the RSS feed.
In order to do that we are going to have to create a simple dialog. Fortunately Tkinter has a nice simple dialog interface that makes creating simple pop-up dialogs quite easy. It’s not as robust as you might like, but if you really wanted to do something very complicated you could simply create another window in the same way that we created our main window. Since we don’t need anything fancy we”ll use the Simple dialog.
To start we must import the simple dialog into our program, so the following will have to be added to the top of the code:
Now we could create create our dialog class in a totally new python file but since it’s going to be pretty simple I decided to keep all together in our one GUI file. The first step in understanding the simple dialog is to read PythonWare’s introduction to the dialog window it will explain all that you need to know to create a simple dialog.
Basically what you need to do is create a class whose parent is the simple dialog:
Then you have to create a body function, this is the function that the tkSimpleDialog assumes will be used to create the body of the dialog. The definition of the body function is as follows:
Pretty simple so far right? There are a few other important functions that you need to know about to create a simple dialog, like when to process the data after the Ok button is pressed:
Continue reading RSS reader – Part Four – Integrating with the GUI
Please remember to read part one and part two.
Classes and Generators
All right, now that we have split our RSS reader up into functions, we’re going to go one step further and put our code into a class.
We’re also going to do something a bit more advanced with our class and create a generator. The reason that we are going to do this is because python has such nice iteration handling and because in the future we’ll probably want to handle each RSS item individually rather then simply dumping it out to the terminal.
Generators are basically a type of iterator, except their syntax is slightly different. In fact anything you can accomplish in a generator you can accomplish in a standard iterator.
Here is an example generator that yields all the factors of the specified number:
def factors (num):
count = 1;
while count < = num/2:
if (num % count == 0):
count = count + 1
Continue reading RSS reader – Part Three – Generator Class
This post is my continuation of my Python based RSS reader that I wrote in part one. As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn’t broken up in to functions properly. So, in this part we’re going to work on breaking the old script up into functions.
Functions are defined in python using the def keyword. So if I wanted to define a function called “count” that counts from 1 to a certain number I would do so like so:
Where count is the name of the function and nNum is a parameter that is being passed into the function. If I wanted to call the function I would do so like this:
Continue reading RSS reader – Part Two (and Functions)
All right so I’ve already figured out how to write an executable script that writes out “Hello World!” to the command line, now I need to figure out how to do something interesting.
As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing seemed interesting enough or easy enough for me to do until I visited one of my favorite websites: slashdot.org.
It was there that I noticed that RSS icon that Firefox always shows me whenever I come across a website with an RSS feed (like this one.)
So I thought to myself, ‘Hmm I wonder how hard it would be to create a simple RSS reader in Python?’ Read on to discover the results. Continue reading RSS reader – Part One