Header Ads

Header ADS

Introduction to Tkinter and Creating first GUI window

             Introduction to tkinter module

tkinter is a built-in library of python which is used for creating simple GUI application .
If you are a python programmer and you want to give your python code a nice GUI look then you can choose tkinter as your First choice .

GUI application ? 
 a GUI ( Graphical User Interface ) basically means a platform or software which contains some widgets like buttons , text ,images ,icons, etc. and a user can perform some actions by using it.

Advantages of using tkinter ...

Layered approach

The layered approach used in designing Tkinter gives Tkinter all of the advantages of the TK library. Therefore, at the time of creation, Tkinter inherited from the benefits of a GUI toolkit that had been given time to mature. This makes early versions of Tkinter a lot more stable and reliable than if it had been rewritten from scratch. Moreover, the conversion from Tcl/Tk to Tkinter is really trivial, so that Tk programmers can learn to use Tkinter very easily.

Accessibility

Learning Tkinter is very intuitive, and therefore quick and painless. The Tkinter implementation hides the detailed and complicated calls in simple, intuitive methods. This is a continuation of the Python way of thinking, since the language excels at quickly building prototypes. It is therefore expected that its preferred GUI library be implemented using the same approach. 


Portability

Python scripts that use Tkinter do not require modifications to be ported from one platform to the other. Tkinter is available for any platform that Python is implemented for, namely Microsoft Windows, X Windows, and Macintosh. This gives it a great advantage over most competing libraries, which are often restricted to one or two platforms. Moreover, Tkinter will provide the native look-and-feel of the specific platform it runs on.

Availability

Tkinter is now included in any Python distribution. Therefore, no supplementary modules are required in order to run scripts using Tkinter.



Creating first GUI using tkinter .

import tkinter as tk
root = tk.Tk()
root.mainloop()         


as you can see i only code 3 lines and a GUI is displayed on the screen. this is the reason why i like tkinter most .

Working of code ..
The first line of code is just importing tkinter library in our python file as i earlier said tkinter is a built-in library but for using its attributes , classes and methods we have to import it .

Now in  the second line we code root = tk.Tk( ) . Tk is the class of tkinter library which is responsible for creating GUI but for using Tk class attributes and methods we have to create a instance/Object of Tk( ) class , so we defined root as the Tk( ) class object and by using root we can call any method  of Tk class and access its attributes also.

After initializing Tk class we use its Object root to call a method named mainloop( ) . the mainloop method is responsible for holding the GUI on the screen or you can say it makes the GUI visible to user. if you run the above code without using root.mainloop() then you cannot seen any GUI displayed on the screen . the program runs and terminate without displaying any GUI on the screen .




This blog is contributed by Yogesh singh .
if you are interested in tkinter and its project then you can check my other post .
I hope you like this Blog .
have a Good Day ... 




No comments

Powered by Blogger.