Python GUI

Python provides different options for developing, Graphical User Interfaces (GUIs). The most important features are listed below.

Tkinter:

Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. Tk is fully portable to the Mac OS X, Windows, and Unix platforms. We would look this option in this site.

wxWidgets:

wxwidgets is a free, portable GUI class library written in C++ that provides a native look and feel on a number of platforms, with Windows, Mac OS X.

PyQt:

This is also a Python interface for a popular cross-platform Qt GUI library. PyQt is currently more mature, but you must buy a PyQt license from Riverbank Computing if you want to write proprietary applications.


Tkinter Programming

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides an easy way to create GUI applications. To create a GUI application using Tkinter we need to perform the following steps −

1. Import the Tkinter module.

2. Create the GUI application's main window.

3. Add one or more widgets to the GUI application.

4. Enter the main event loop to continue


Example 1: 

Lets make a blank window at first. The Tkinter module, containing the Tk toolkit, that needs to be imported. In our example, we import everything from Tkinter by writing from tkinter import  * . from tkinter import  * imports every exposed object in Tkinter into our current namespace.


from tkinter import *

root = Tk()

root.mainloop()

To initialize Tkinter, we have to create a Tk root widget, which is a window with a window manager.
The root widget has to be created before any other widgets.

To make visible our window, we need a continuous loop. We do this, by writing, root.mainloop(). Our script will remain in the event loop until we close the window.

If we run the code, we will see the following output. In our case it's just a window.






Example 2:


In this example, we will customize the window a little bit. We have named our window as "test window".

Here we have added a widget in our window. The first parameter of the Label is the name of the window, in our case "root". So our label widget is a child of the root widget. The keyword parameter "text" specifies the text to be shown.


from tkinter import *

root = Tk( )
root.geometry( "200x200" )
root.title( "test window" )

theLabel = Label( root, text = "Hello Tkinter!" )
theLabel.pack( )

root.mainloop( )

If we run the code, we will see the following output. In our case its a window with window name and a text label.




Example 3:


In this example, we have created two different frames and a few buttons. We have added three buttons on the top frame and one button in the bottom frame.



from tkinter import *

root = Tk( )
root.geometry( "300x100" )
root.title( "test window" )


topFrame = Frame( root ) 
topFrame.pack( )

bottomFrame = Frame( root )
bottomFrame.pack( side = BOTTOM )

button1 = Button( topFrame, text= "Button 1", fg="red" )
button2 = Button( topFrame, text= "Button 2", fg="blue" )
button3 = Button( topFrame, text= "Button 3", fg="green" )
button4 = Button( bottomFrame, text= "Button 4", fg="purple" )

button1.pack( )
button2.pack( )
button3.pack( )
button4.pack( )

root.mainloop( )

If we run the code, we will see a following output.



Example 4:

In this example, we will create a Login form using the grid method of packing.  Here we do not make any frame. Let's take a look.


from tkinter import *

root = Tk( )
root.geometry( "200x100" )
root.title( "test window" )

label_1 = Label( root, text = "Name:" )
label_2 = Label( root, text = "Password:" )

entry_1 = Entry( root )
entry_2 = Entry( root )

label_1.grid( row = 0,  column = 0,  sticky = E )  # this keeps the label, in East
label_2.grid( row = 1,  column = 0,  sticky = E )

entry_1.grid( row = 0,  column = 1 )
entry_2.grid( row = 1,  column = 1 )

check = Checkbutton( root, text = "remember me")
check.grid( columnspan = 2 )                     # this will combine column zero and one

btn = Button( root, text = "Login", bg = "blue", fg = "white")
btn.grid( columnspan = 2 )


root.mainloop( )


And this will create the following Login form.




Example 5:

In this example, we will create an Open File interface.


from tkinter import *
from tkinter.filedialog import askopenfilename

root = Tk()
root.geometry( "400x300" )
root.title( "File Opener" )
text = Text(root)

def open_file():
    file_name = askopenfilename(initialdir="C:/",
                           title="Choose a  text file.",
                           filetypes =(("Text File", "*.txt"),("All Files","*.*"))

                           )
    print (file_name)
    try:
        with open(file_name,'r') as UseFile:
            print(UseFile.read())
    except:
        print("No file exists")


menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=open_file)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)


root.config(menu=menubar)
root.mainloop()