Page 1 of 1

Fundamentals of Tkinter Part One

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Posted 22 July 2009 - 07:30 PM

Basic Tkinter and Python

Tkinter (“TK INTERface”) is an interface to the Tk modules. Basically, this allows for easy and simple GUI creation.

When there are so many choices available for Python GUI development, why choose Tkinter as the choice?
  • There is very little code that has to be rewritten because Tkinter has a wrapper that takes care of all the background for us.

  • Tkinter handles the layout for us

  • Full support for font, images, window management, and synchronization.

This tutorial will get you started with a simple program that shows the simplicity of Tkinter. Fire up your Python IDE and lets get started.

# hello.py
from Tkinter import *

mainFrame = Tk()

w = Label(mainFrame, text=”Hello World!”)
w.pack()

mainFrame.mainloop()



Now run it and you will see a simple program that has a simple label that says “Hello World!”.

Let me explain what this code does.

from Tkinter import *



This imports the Tkinter module that allows for the GUI to be made.

mainFrame = Tk( )
w = Label( mainFrame, text=”Hello World!” )
w.pack( )



The first line sets up the Tk interface and sets up a blank frame.

The next line assigns a newly created label to the mainFrame with the text “Hello World” written on it.

pack( ) places all of the GUI elements neatly onto it and makes them all visible.

mainFrame.mainloop()



This line simply sends the GUI into a loop that updates the appearance and handles events for us.

Event Handling

Event Handling in Tkinter is extremely easy to set up in comparison to some other GUI tools that exist for Python.

Here is an example that shows how this is done.


from Tkinter import *
import tkMessageBox

class TkApplication():

	def __init__(self, parent):

		self.frame = Frame(parent)
		self.frame.pack()

		self.myButton = Button(self.frame, text="Hello", command=self.sayHi)
		self.myButton.pack(side=LEFT)

		self.quitButton = Button(self.frame, text="Quit", command=self.frame.quit)
		self.quitButton.pack(side=LEFT)
	
	def sayHi(self):

		tkMessageBox.showinfo("Hi", "Hello World")

parent = Tk()
app = TkApplication(parent)
parent.mainloop()



This application shows a dialog that says “Hello World” upon clicking the Hello Button. The __init__ method set up a frame and two buttons. What you may notice is the last argument in the Button constructor. This is called a callback function. It is called automatically when that button is clicked. The sayHi method shows a simple message dialog that tells the user hello. Finally, the pack method that says “(side=LEFT)” lets the buttons line up side by side as opposed to top and bottom.

The remaining parts of the code are repeats of the last program.

Well, thats it for part one of Fundamentals of Tkinter, thanks for reading!


Is This A Good Question/Topic? 3
  • +

Page 1 of 1