Using Menus in Tkinter

This is a quick post on how to use menu’s in Tkinter, if you are unfamiliar with Tkinter you might want to check out some of my other Tkinter related posts.

We are going to start of with a basic python Tkinter app:

#! /usr/bin/env python
from Tkinter import *

class App(Frame):

	def __init__(self, master):
		"""Initialise the base class"""
        	"""Set the Window Title"""
        	self.master.title("TkInter Menus")
        	"""Display the main window
        	with a little bit of padding"""
        	self.grid(padx=15, pady=15,sticky=N+S+E+W)

if __name__ == "__main__":
	root = Tk()
	app = App(root)

Python Tkinter WindowWhat this gives us is basically a plain old blank Tkinter window, not too exciting. The next thing we are going to do is add create our root menu, before going any further you might want to read the excellent information about menu widgets in PythonWare’s Tkinter introduction. You don’t have to but it’s an excellent source of information.

Continue reading Using Menus in Tkinter

RSS reader – Part Four – Integrating with the GUI

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:

import tkSimpleDialog

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:

class RSSItemDialog(tkSimpleDialog.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:

def body(self, master):

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:

def apply(self):

Continue reading RSS reader – Part Four – Integrating with the GUI

Creating a GUI in Python using Tkinter – Part 2

This post is going to build off of Creating a GUI in Python using Tkinter which introduces the basic elements needed to create a GUI using TKinter. This post will take that introduction a bit further and build a more complex GUI application and use some more widgets.

The GUI that we are going to start creating is the GUI that we will eventually use for the RSS reader that I am creating.

Not much having to do with the RSS reader will happen in this post since we will just be creating the GUI shell, but it will help to explain some of the variable names.

GuiLayoutWe are going to start off with a simple shell that will hopefully resemble the following layout. We will have RSS sites on the left, then the selected sites RSS story titles on the right, and finally the selected stories text on the bottom.

To create this GUI we will be using the following widgets:

Continue reading Creating a GUI in Python using Tkinter – Part 2

Creating a GUI in Python using Tkinter

Up until now we’ve focused on python applications that only run in the command line. For a lot of tasks this is great, but in order to be really impressive we’re going to want to create a GUI for some of our python applications.

There are a lot of python GUI toolkits out there, but for our first application we are going to use Tkinter, which is considered somewhat of a standard right now and is installed when pyhon is installed. Another nice thing about the Tkinter GUI is that it is cross platform and offers a native look and feel on *nix, Windows, and OS X.

Tkinter Example One

So lets start off with a simply “Hello World” GUI app:

#! /usr/bin/env python
from Tkinter import *

class GUIFramework(Frame):
    """This is the GUI"""
    def __init__(self,master=None):
        """Initialize the base class"""
        """Display the main window"""
        """Create the Text"""
        self.HelloLabel = Label(master, text="Hello World!")
if __name__ == "__main__":
    guiFrame = GUIFramework()

Running this on OS X results in the following:

hello world python gui

And running it in Windows Xp gives you:

hello world python gui
Continue reading Creating a GUI in Python using Tkinter