Using Tkinter in Python GUI programming


There are many available packages that you can use in creating of windows/dialogs in Python script. For example, The most important can be found below:

Tkinter: Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.

wxPython: This is an open-source Python interface for wxWindows http://wxpython.org.

JPython: JPython is a Python port for Java, which gives Python scripts seamless access to Java class libraries on the local machine http://www.jython.org.

Since the Tkinter is shipped with the python installation package, it is therefore recommended for basic GUI programming. Let ‘s have a look at a small GUI created by Python. It requires to import the package Tkinter and tkMessageBox (if you fancy showing dialogs).

from Tkinter import *
from tkMessageBox import *

gui = Tk()

def hello():
    showinfo("HELLO!", "WORLD")

btn = Button(gui, text = "Hello", command = hello)
btn.pack()

gui.mainloop()

The above script will show a window with a button created (labelled ‘Hello’). The method hello() will be invoked if you click the button.

pythongui Using Tkinter in Python GUI programming beginner GUI implementation python technical

Using Tkinter.Tk() creates a new GUI object and returns a handle which can be used to add more elements e.g. Buttons, Labels, Text etc.

The mainloop method will let the GUI handler to enter the message looping, which will show up the window. The elements are created and put on the window before the mainloop

Usually, the first parameter is the GUI handler, for instance, the below will create a textarea and put some texts in it.

from Tkinter import *

gui = Tk()

txt = Text(gui)
txt.insert(INSERT, "Hello World")
txt.pack()

gui.mainloop()

pythongui2 Using Tkinter in Python GUI programming beginner GUI implementation python technical

The elements will not be visible until the pack() is invoked, which will pack itself in the window. Compared to C#, C++ or Java, python provides an easier and efficient method to do the GUI programming.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
447 words
Last Post: Notes on Python Syntax
Next Post: Codeforces: A. Funky Numbers

The Permanent URL is: Using Tkinter in Python GUI programming

Leave a Reply