March 16, 2023

Tkinter GUI Programming

 Tkinter GUI Programming
 


                                  


 (image 1)*

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 walk through Tkinter in this blog.

● 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.

Tkinter Programming.

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 above-mentioned widgets to the GUI application.

● Enter the main event loop to take action against each event triggered by the user.

Example :

 



 



Tkinter widgets.

There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the python GUI applications.

Python Tkinter Geometry.


The Tkinter geometry specifies the method by using which, the widgets are represented on display. The python Tkinter provides the following geometry methods.

 1) The pack() method.

The pack() widget is used to organize widget in the block. The positions widgets added to the python application using the pack() method can be controlled by using the various options specified in the method call.

 However, the controls are less and widgets are generally added in the less organized manner.

 SYNTAX : widget.pack(options)

 A list of possible options that can be passed in pack() is given below. 

● expand : If the expand is set to true, the widget expands to fill any space.

 fill : By default, the fill is set to NONE. However, we can set it to X or Y to determine whether the widget contains any extra space.

 ● size : it represents the side of the parent to which the widget is to be placed on the window.

 Example :

 



Output :

 



2) The grid() method.

The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and columns as the options in the method call. We can also specify the column span (width) or rowspan(height) of a widget.

This is a more organized way to place the widgets to the python application.

SYNTAX : widget.grid(options)

A list of possible options that can be passed inside the grid() method is given below.

  Column : The column number in which the widget is to be placed. The leftmost column is represented by 0.

 ● Columnspan : The width of the widget. It represents the number of columns up to which, the column is expanded.

 Row : The row number in which the widget is to be placed. The topmost row is represented by 0.

● Rowspan : The height of the widget, i.e. the number of the row up to which the widget is expanded.

 Example :


Output :

 



3) The place() method.

The place() geometry manager organizes the widgets to the specific x and y coordinates.

SYNTAX : widget.place(options)

A list of possible options is given below.

 Anchor : It represents the exact position of the widget within the container. The default value (direction) is NW (the upper left corner).

 bordermode : The default value of the border type is INSIDE that refers to ignore the parent's inside the border. The other option is OUTSIDE.

● height, width : It refers to the height and width in pixels.

 x, y : It refers to the horizontal and vertical offset in the pixels.

 Example :

 



 Output :

 



Now, Let’s Build a Text Editor !

 

Building a Text Editor

In this section, you’ll build a text editor application that can create, open ,edit, and save text files. There are three essential elements in the application:

1. A Button widget called btn_open for opening a file for editing

2. A Button widget called btn_save for saving a file

3. A TextBox widget called txt_edit for creating and editing the text file

The layout contains a single row and two columns:

1. A narrow column on the left for the buttons

2. A wider column on the right for the text box

 Example:

 



● Line 1 imports tkinter.

● Lines 3 and 4 create a new window with the title "Simple Text Editor".

 Lines 6 and 7 set the row and column configurations.

● Lines 9 to 12 create the four widgets you’ll need for the text box, the frame, and the open and save buttons.

 These two lines 12 and 13 create a grid with two rows and one column in the frm_buttons frame since both btn_open and btn_save have their master attribute set to frm_buttons.

 Output :



Example :

 Lines 5 to 7 use the askopenfilename() dialog from the tkinter.filedialog module to display a file open dialog and store the selected file path to filepath.

 Lines 8 and 9 check to see if the user closes the dialog box or clicks the Cancel button. If so, then filepath will be None, and the function will return without executing any of the code to read the file and set the text of txt_edit.

 Line 10 clears the current contents of txt_edit using .delete().

 Lines 11 and 12 open the selected file and .read() its contents before storing the text as a string.

 Line 13 assigns the string text to txt_edit using .insert().

 Line 14 sets the title of the window so that it contains the path of the open file.

 Lines 19 to 22 use the asksaveasfilename() dialog box to get the desired save location from the user. The selected file path is stored in the filepath variable.

 Lines 23 and 24 check to see if the user closes the dialog box or clicks the Cancel button. If so, then filepath will be None, and the function will return without executing any of the code to save the text to a file.

 Line 25 creates a new file at the selected file path.

 Line 26 extracts the text from txt_edit with .get() method and assigns it to the variable text.

● Line 27 writes text to the output file.

 Line 28 updates the title of the window so that the new file path is displayed in the window title.

 Output :



Conclusion :

From this blog, you learned how to get started with Python GUI programming. Tkinter is a compelling choice for a Python GUI framework because it’s built into the Python standard library, and it’s relatively painless to make applications with this framework.

 Throughout this tutorial, you’ve learned several important Tkinter concepts:

 How to work with widgets

  How to control your application layout with geometry managers

 How to make your applications interactive

 How to use five basic Tkinter widgets: Label, Button, Entry, Text, and Frame



KRISHNA THAKOR ( 21CE142)

GRISHA VEKARIYA ( 21CE153)

( Guest authors ) 

NOTE: THE WORK IS ORIGINAL AND AUTHOR DOES NOT CLAIM OVER *IMAGE 1

Education for free

Author & Editor

This blog was made by Dhruv Panjrolia and Drashti Panjrolia. Major contributions in the process were made by Mousmi Shrivastava and Kalindi Chokshi

0 comments:

Post a Comment

Please do not enter any spam link in the comment box