Simple Pycodes

Simple Pycodes

Gui programming with


Python by default is a text base programming language, for this reason if we wanna have graphical user interface with it, we need to use extra framework "module".

There are many choices for Gui programming in python, for example:

WxPython, PyGui, Pyside, Tkinter, Pyqt and ...

Tkinter have some pros and cons:

pros: Its part of the standard library so you dont need to download any extra module, have large community, and its simple, and some programmers use it even for writing some basic games . and its cross platform you can use it on windows, linux and mac and it worth to mention that written by Fredrik Lundh, so thanks Fredrik.


Cons: its ugly :D, thats what some people say a bout it but its a mater of personal taste. it doesnt have pythonic look! and of course its not naturally part of the python, its just a wrapper around the TK and Tci module which is interpreted embedded in python interpreter. Tkinter can be used by other languages as well.

in Tkinter we are dealing with Widgets and events,  same as Pygame, Turtle . because every program that have constant interaction with user is Event Driven Program. 

as i mentioned before Tkinter is part of python standard library, so the only thing we need to do is to import it.

In first example we are going to create a main window. same as what we do when working with Pygame or turtle to create the screen.

so we create a main window and call the main loop to keep it up and running.

We can do Gui programming just by use of functions, but it can be a good idea to use Classes as our programs grows, we will deal with more complex codes, so static programming is a good idea here.

so lets change our code into the class.

Lets improve the gui by changing the title of main window and create a Label to display a text.

first we create a object from tkinter.Label, and specify the position of the label whic is on the main window then, we just pack it on the window by using pack()

By creating main_window we have only one frame or window to work with, but imagine we can change the main window by invisible line in to the two separate fram. by using Frame() method in tkinter we can create as many as frames we want on main window, and as other widgets we have to place them on the main window or root or master screen.

Button and command


With button widget we can connect function to the gui, and make the button do something for us, it can be simple print, exit or anything depending on a program.

to create a button we call tkinter.Button(position, text, command) method.

Position = where the button going to be, for example on main_window or top frame

Text = text on the button which can be anything we want like ok, Exit or...

Command = a function or method that by clicking the button will be called.


lets create a gui class with one button and one method for it.


now lets change the function the way that instead of printing text on command line show the response on messagebox, so first we need to import messagebox from tkinter. and call it from click method.

we can use tkinter.messagebox by calling it in function or method, the first argument title bar text and second will be the actual message.


Entry widget

Entry widget allow us to get data from user, so take it as input().

now lets create another simple gui and get some input from user by entry widget.

as you see we create an entry widget by tkinter.Entry(master, width =n)

and to get input in entry window as Data we need to use entry.get() method like entry_data = Entry.get().


messagebox is not the only method to display information in tkinter, we can even do it by changing labels text while program is running. lets create simple convertor which convert kilmeter to miles, and instead of displaying information with messagebox do it with label.

maybe sometimes we dont need to have extra window openning over and over to display some message.


Display mesaage on Label with StringVar

In below example we create very simple converter program which get the kilometer and return miles.

StringVar can be changed on run time, so we create a label and instead of text we set the textvariable to value which is already set to StringVar, and in calculate method change it to miles, so by this way we can show the result of calculation on label.

And when we run the program Label will display the results of calculation.





Another very useful tool that comes with tkinter is radiobuttons, which can be used as choice between different options, the name of radiobutton cames from old radios which had buttons to choose between different channels, so each time you push one button the previous one would popped out by this way there would be only one choice between many options.


tkinter.InitVar() and tracing .


Tracing is a method that you can update a widget by changing its value in runtime. as we saw in previous example with tkinter.StringVar().

in Tkinter there are 4 types of variable Classes:






with radiobuttons we can use set()  method to change  the IntVariable in to different values during the run time.

in next example we are making another gui program which have two options with two radiobuttons and two button widget one for showing the choice we make and another to quit the program. 

and this time we can use another method to exit the program using destroy() method which can be directly interact with button as a command.


Basic Gui programming with Tk

Part 1

Basic Gui programming with Tk

Part 2

Basic Gui programming with Tk

Part 3

Basic Gui programming with Tk

Last part