How to Create a Hello World Application in Python Using Tkinter

Tkinter or “TK Interface” module provides various classes and functions to create cross-platform graphical applications in Python using the “Tk UI” framework. Tkinter is included in the default modules shipped with Python builds, even though it is maintained by ActiveState. It is one of the most popular GUI toolkits available for Python, useful for both creating quick prototypes and for development of full-fledged applications. This article covers a guide about installation of Tkinter in Linux, some code samples and their explanation to create a simple “Hello World” application.

Installing Tkinter

You can install Tkinter for Python 3 in Ubuntu by running the command specified below:

$ sudo apt install python3-tk

Tkinter can be installed in other Linux based distributions from the package manager. You can also install Tkinter packages in Linux by following installation instructions available here.

To verify that Tkinter has been successfully installed on your system, run the the command mentioned below:

$ python3 -m tkinter

If Tkinter has been installed correctly, you should see a GUI window like this:

You can also use a Python interpreter to verify installation of Tkinter. Run the following commands in succession to do so (last two commands will run in the Python interpreter):

$ python3

import tkinter

print (tkinter.TclVersion)

Creating a Hello World Application Using Tkinter

You can create a simple application showing “Hello World !!” string by using the code sample specified below:

from tkinter import *

root = Tk()

root.title("Hello World")

main_string = Label(root, text="Hello World !!")

main_string.pack()

root.mainloop()

The first statement in the code sample above imports necessary functions from the Tkinter module. Instead of importing specific functions, everything is imported at once using “*” (asterisk) character. Next, the main application or root window is defined and a “Hello World” title is set for it. A new label widget showing “Hello World !!” string is created in the next statement. The “pack” method is used to automatically resize and match the window area with the position and area of the widget without cutting off visibility of the widget as no geometry is specified. Lastly, the main event loop is run that listens for user events like keyboard and mouse input actions. Once the main loop is successfully run, you should see an application window like this:


Notice that the application title is not completely displayed in the title bar. The “pack” method without any arguments auto-fits the main application window to the area of visible widgets. Since the application window is too small, you can manually specify its size by using the “geometry” method to prevent auto-fit.

from tkinter import *

root = Tk()

root.title("Hello World")

root.geometry("640x480")

main_string = Label(root, text="Hello World !!")

main_string.pack()

root.mainloop()

You can also add a padding argument to the pack method used for the label widget to increase the area of the main application window by stretching the widget.

from tkinter import *

root = Tk()

root.title("Hello World")

main_string = Label(root, text="Hello World !!")

main_string.pack(padx=50, pady=50)

root.mainloop()

The two arguments, “padx” and “pady” specify horizontal and vertical spacing respectively on both sides of the widget.

Pack method is one of the most important methods you will use while creating user interfaces using Tkinter library. Widgets won’t appear on the main application frame unless you call pack method on each and every widget you have defined in the code. You can use the pack method to define dynamic and fixed geometry and position of the widgets. Widgets can be packed into each other to create nested widgets as well. You can read more about the pack method and a few more examples about it from its usage reference.

Further Reading

To know more about Tkinter API you can use a guide available in the official Python documentation. TkDocs features an excellent tutorial that can help create your first Tkinter app though the guide may be a little complex for absolute beginners. You can find official Tkinter manuals that include API definitions and examples on Tcl Developer Xchange website. Python Wiki’s Tkinter page features numerous links that can help you get started.

Conclusion

This article only covers a simple example for getting you started with Tkinter applications. You can create advanced GUIs using Tkinter, but for applications requiring complex user interface elements, many developers prefer PyQt over Tkinter. PyQt also features more widget built-ins than Tkinter, including a graphical suite for designing applications using drag and drop actions.



from Linux Hint https://ift.tt/2JDnj3H

Post a Comment

0 Comments