GUI with Tkinter in Python

In this blog, you are going to learn how to create GUI apps in Python. 

Python provides various options for developing graphical user interfaces (GUIs). Most important are listed below.
·        Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look this option in this chapter.
·        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.
There are many other interfaces available, but we are using Tkinter.
Tkinter :
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. 
Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps −
  • Import the Tkinter module.
  • Create the GUI application main window.
  • Add one or more of the widgets to the GUI application.
  • Enter the main event loop to take action against each event triggered by the user
So here is step wise description:
  • First, you import the key component, i.e., the Tkinter module.
  • As a next step, you initialize the window manager with the tkinter.Tk() method and assign it to a variable. This method creates a blank window with close, maximize, and minimize buttons on the top as a usual GUI should have.
  • Then as an optional step, you will Rename the title of the window as you like with window.title(title_of_the_window).
  • Next, you make use of a widget called Label, which is used to display some text into the window.
  • Then, you make use of Tkinter's geometry management attribute called pack() to display the widget in size it requires.
  • Finally, as the last step, you use the mainloop() method to display the window until you manually close it. It runs an infinite loop in the backend.


import tkinter


window = tkinter.Tk()

# to rename the title of the window
window.title("GUI Using Tkinter")

# pack is used to show the object in the window
label = tkinter.Label(window, text = "Welcome to Tutorial on Tkinter!").pack()

label = tkinter.Label(window, text = "Hello World !!").pack()

window.mainloop()


Comments

Popular posts from this blog