Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Python GUI Programming Part I

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Python GUI Programming Part I

Post by Acid_Snake » Sun Aug 12, 2012 2:38 pm

There are many GUI toolkits available for python: pygtk, pyqt, wxpython, etc. But the default toolkit used is Tkinter. Tkinter is a wrapper for the Tcl toolkit, it doesn't have much widgets and features but it's simplicity makes it a perfect candidate for a quick and simple work.


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 Tkinter
or this:

Code: Select all

import tkinter
Most people also prefer to use the variable tk:

Code: Select all

import Tkinter as tk

The 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()
and then we start the window's mainloop:

Code: Select all

main_window.mainloop()
You can also name your main window, this name will be displayed as the app name:

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")
Now we crate the label widget with the text we want to show:

Code: Select all

label = Tkinter.Label(gui, text="Hello World")
As you can see the first argument to pass at any widget is where they will be placed, normally is a window, but it can also be a frame (more on that later).
The next thing we need is to pack the widget:

Code: Select all

label.pack()
Don't forget to pack your widgets, otherwise the won't show up.
And of course we create the mainloop:

Code: Select all

gui.mainloop()
The entire code would be:

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()
And the result? this:
Hello_Gui.png
Hello_Gui.png (4.88 KiB) Viewed 1479 times
More widgets later...
Previous: viewtopic.php?f=37&t=11920
Next: viewtopic.php?f=37&t=13059
Advertising
Last edited by Acid_Snake on Sun Aug 12, 2012 11:08 pm, edited 1 time in total.

waratte
Posts: 1320
Joined: Wed Oct 20, 2010 12:03 am

Re: [Tutorial] Python GUI Programming Part I

Post by waratte » Sun Aug 12, 2012 3:52 pm

Wow that's simple. :o

/me might consider trying to learn python again
Advertising

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Python GUI Programming Part I

Post by qwikrazor87 » Sun Aug 12, 2012 9:06 pm

Back to school. :) I feel like a noob again.
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

Post Reply

Return to “Programming and Security”