Friday, February 10, 2012

A Way to Use Tkinter and Python

There's probably as many ways to learn as there are people.

I'm learning Python, and OOP, and Java and a ton of other things. There seems to be no end to the number of things of which I am ignorant. But I like Python - it fits my procedural style while giving me a transition into OOP.

There are a ton of great sites out there. Effbot has a kick-ass site. Python's documentation is very good. But my questions are often more practical than the intelligent stuff these guys talk about.

For example, this my first post, is all about writing the code for a Tkinter application. But I wanted to put the code for the GUI in one file and all the meat in another. Seems simple enough, and for most of the programmers out there it undoubtedly is. It's probably so easy, they don't feel the need to write about it. I, on the other hand, am baffled by easy stuff like this.

And so, I wrote a really simple application to do - well not much of anything. But it does contain a file which defines the application - two buttons and a text label. The other file contains an instance of the class and a function which reacts to a button press. I hope this helps someone.

file listing for - the gui portion of the project. So the idea is to write the GUI part - and then to more-or-less forget about it.

#! /usr/bin/env python

""" was written as an example of how to split a project
     into multiple files"""

import sys
from Tkinter import *

class Application(Frame):

        def __init__(self,master=None):
                Frame.__init__(self, master)

        def createWidgets(self):

            self.frame0 = Frame(self)
            self.label1 = Label(self.frame0, text="This is a text label.")
            self.label1.grid( row=0, column=0)

            self.button1 = Button(self.frame0, text = "Push Button")

            self.button2 = Button(self.frame0, text = "Quit" , command = self.quit)

And, its companion

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

app.master.title("Hello There")

def process(self):
     print ("process activated")

app.button1.bind("<Button-1>", process)


Next, I want to make the button go to a website. Where I'm going is to get a podcast feed list, and eventually i want to download them and play them. For now, I'll use Tkinter, but I'd prefer to use PyQt. I can't wait to figure out how to do that.

No comments:

Post a Comment