Tkinter

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit[1] and is Python's de facto standard GUI,[2] and is included with the standard Windows and Mac OS X install of Python.

The name Tkinter comes from Tk interface. Tkinter was written by Fredrik Lundh.[3]

As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter. Tkinter calls are translated into Tcl commands which are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a single application.

Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5.[4][5] This allows Tk widgets to be easily themed to look like the native desktop environment in which the application is running, thereby addressing a long-standing criticism of Tk (and hence of Tkinter).

There are several popular GUI library alternatives available, such as wxPython, PyQt (PySide), Pygame and PyGTK.

Tkinter is free software released under a Python license.[6]

Installation[edit]

Tkinter is usually part of Python. However, Tcl/Tk often is not. For information on getting Tcl/Tk/Tkinter installed on your platform, refer to How_to_Install_Tkinter[7]
Several books mention Tkinter.

Some Definitions[edit]

Window[edit]

This term has different meanings in different contexts, but in general it refers to a rectangular area somewhere on your display screen.

Top Level Window[edit]

A window that exists independently on your screen. It will be decorated with the standard frame and controls for your system's desktop manager. You can move it around on your desktop. You can generally resize it, although your application can prevent this.

Widget[edit]

The generic term for any of the building blocks that make up an application in a graphical user interface. Examples of widgets: buttons, radiobuttons, text fields, frames, and text labels.

Frame[edit]

In Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame is a rectangular area that can contain other widgets.

Child and parent[edit]

When any widget is created, a parent-child relationship is created. For example, if you place a text label inside a frame, the frame is the parent of the label.

A Minimal Application[edit]

Here is a sample code:[8]
Lines 1-2:

#!/usr/bin/env python      
import Tkinter as tk

Lines 3-5:

class Application(tk.Frame):   
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()

Lines 6-7:

   def createWidgets(self):
self.quitButton = tk.Button(self, text='Quit',command=self.quit)
self.quitButton.grid()

Lines 8-10:

app = Application()          
app.master.title('Sample application')
app.mainloop()
  • line 1: This line makes the script self-executing, assuming that your system has Python correctly installed.
  • line 2: This line imports the Tkinter module into your program's namespace, but renames it as tk.
  • line 3: Your application class must inherit from Tkinter's Frame class.
  • line 4: Calls the constructor for the parent class, Frame.
  • line 5: Necessary to make the application actually appear on the screen.
  • line 6: Creates a button labeled “Quit”.
  • line 7: Places the button on the application.
  • line 8: The main program starts here by instantiating the Application class.
  • line 9: This method call sets the title of the window to “Sample application”.
  • line 10: Starts the application's main loop, waiting for mouse and keyboard events.

References[edit]

External links[edit]