eHow launches Android app: Get the best of eHow on the go.

How To

How to Use the Tk Toolkit With Python

Contributor
By Amanda Morin
eHow Contributing Writer
(1 Ratings)

The Tk toolkit is a cross-platform library for building graphical user interfaces (GUIs) for programs written in a variety of programming languages. Since there's no universal code or easy way to translate among them, each of the languages supported by the Tk toolkit must use a library of "wrapper" code so that the interface can be created using its own code. In Python, the library used to interface with Tk is called Tkinter.

Difficulty: Moderate
Instructions
  1. Step 1

    Import the Tkinter module. Importing Tkinter gives your program access to the classes, methods and functions needed to create the graphical user interface components. You can do this by using either the "import" or the "from" keyword to load the Tkinter module into your script:import Tkinterorfrom Tkinter import

  2. Step 2

    Create the root widget. All components of a GUI are called widgets, and in Tk there must be a root widget to contain the rest of the widgets. Use the Tk() function to create an instance of the root widget. A program can have only one root, and it must be the first widget created in the program:root = Tk()

  3. Step 3

    Label the root widget. While this is optional, it is desirable to add a meaningful label to the main window of the program. The label widget will display in the title bar of the main window. Use the pack method to automatically size the label to the widget on which it will display.t = Label(root, text="A simple Tk application")t.pack()

  4. Step 4

    Add any other widgets and program statements. All other widgets created must be done between the statement that imports Tkinter and the beginning of the main event loop. For instance, to create two buttons enclosed in a frame, a frame must be instantiated and packed into the root widget. The buttons are created and packed into the frame:buttonframe = Frame(root)buttonframe.pack()messagebutton = Button(buttonframe, text="click me")cancelbutton = Button(buttonframe, text="cancel")messagebutton.pack(side=LEFT)cancelbutton.pack(side=LEFT)

  5. Step 5

    Start the main event loop for the root widget using the mainloop() method. The main event loop must be started after all the other statements in the program. The event loop handles user events, like keyboard entry from the user and mouse clicks. It also monitors for updates from the windowing system and from Tk:root.mainloop()

Tips & Warnings
  • The main event loop takes care of common events such as updates to the display and resizing widgets.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics