Importing Tkinter:
The method for importing Tkinter varies mostly due to the operation system case sensitivity, but it's mostly this:
Code: Select all
import TkinterCode: Select all
import tkinterCode: Select all
import Tkinter as tkThe main window:
Every GUI has a main window, that is of course the window where all widgets will be placed. In Tkinter this as simple as:
Code: Select all
main_window = Tkinter.Tk()Code: Select all
main_window.mainloop()Code: Select all
main_window = Tkinter.Tk(className="My first Tkinter program")Widgets:
Having a main window is important, but it doesn't serve any purpose to the end user, that is why we need to use widgets depending on what the program needs. There are many widgets available in Tkinter and they use the same or similar ways to be displayed. We will investigate most (if not all) widgets in part II, now you need to learn the basics.
A simple app:
Every programming language starts with a simple app, usually Hello World so that's what I'm going to show here.
For the hello world we need to show text, and there are two methods in Tkinter to show text: the label widget and the text widget.
The label widget, just like it's name implies, is used to name different parts of the GUI in single line, while the text widget is used for multi line text. This difference has become less of a problem since the label widget is capable of multi line text using the \n character or the triple quotation, this and the fact that you can display images with the label widget has made the text widget rather unnecessary. Long story short: we will be using the label widget for the hello world example.
First we need to make the main window:
Code: Select all
gui = Tkinter.Tk(className="Hello GUI")Code: Select all
label = Tkinter.Label(gui, text="Hello World")The next thing we need is to pack the widget:
Code: Select all
label.pack()And of course we create the mainloop:
Code: Select all
gui.mainloop()Code: Select all
#! /usr/bin/env python
import Tkinter
gui = Tkinter.Tk(className="Hello GUI")
label = Tkinter.Label(gui, text="Hello World")
label.pack()
gui.mainloop()Previous: viewtopic.php?f=37&t=11920
Next: viewtopic.php?f=37&t=13059
Advertising