A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

A Simple Tkinter Example (Python)

What is Tkinter?

Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.

Here is a simple Tkinter example.

The code below should generate a very simple GUI with a label, a textbox, a button that will allow you to browse a file and a Submit/Execute button.

Another thing is that when you “browse” for a file, the full path of the browsed file will be displayed in the filelocation textbox.

from Tkinter import *
import tkFileDialog, tkMessagebox
class App:
    def __init__(self, master):
        self.master = master
        #call start to initialize to create the UI elemets
    def start(self):
        self.master.title("This is the title of the 'Window'")
        self.now = datetime.datetime.now()
        #create a variable with text
        label01 = "This is some text"
        #put "label01" in "self.master" which is the window/frame
        #then, put in the first row (row=0) and in the 2nd column (column=1), align it to "West"/"W"
        Label(self.master, text=label01).grid(row=0, column=0, sticky=W)
        self.filelocation = Entry(self.master)
        self.filelocation["width"] = 60
        self.open_file = Button(self.master, text="Browse...", command=self.browse_file) #see: def browse_file(self)
        self.open_file.grid(row=1, column=1) #put it beside the filelocation textbox
        RADIO_BUTTON = [
            ("This will display A", "A"),
            ("This will display B","B")
        #initialize a variable to store the selected value of the radio buttons
        #set it to A by default
        self.radio_var = StringVar()
        #create a loop to display the RADIO_BUTTON
        for text, item in RADIO_BUTTON:
            #setup each radio button. variable is set to the self.radio_var
            #and the value is set to the "item" in the for loop
            self.radio = Radiobutton(self.master, text=text, variable=radio_var, value=item)
            self.radio.grid(row=2, column=i)
            i += 1
        #now for a button
        self.submit = Button(self.master, text="Execute!", command=self.start_processing, fg="red")
        self.submit.grid(row=3, column=0)
    def start_processing(self):
        #more code here
    def browse_file(self):
        #put the result in self.filename
        self.filename = tkFileDialog.askopenfilename(title="Open a file...")
        #this will set the text of the self.filelocation
root = Tk()
app = App(root)

Save the code as “gui.py” and then use your command line to execute it.

$ python gui.py

A Window should open up!

FYI, This is just a snippet from my first working Python script. You can view the rest of the source code in my Github: http://github.com/wenbert/tide_converter

Comments, suggestions or recommendations are of course welcomed!

You can find more resources here.

This entry was posted in General and tagged , . Bookmark the permalink.

2 Responses to A Simple Tkinter Example (Python)

  1. Pacek says:

    I’m glad you are writing about Python and I’m looking forward to some more articles about Django. I used to develope under Zend framework but I switched to Django about 6 months ago. It’s just so great and awesome, I no longer like Zend framework :D (and I used to love it).

  2. Wenbert says:

    Thank you Pacek. I dived into Django a few month ago but I had a hard time because I do not know Python that well yet. That is why I decided to practice a bit more with simple apps. Back to basics and I am having so much fun! :D

    For Zend Framework, I have not dealt with it in a while. I have heard of a version 2 in the works but I have no idea on what it will be like. I still follow a lot of ZF guys on Twitter though ;)

    Also, I learned a lot from using ZF. I think realizing some of it’s flaws (both ZF and PHP I mean) is what’s making me like Python and Django.

    The downside though is that I cannot afford yet to pay for a Linode to run my Python apps :-/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>