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.
The Tkinter geometry specifies the method by using which, the widgets are represented on display. The python Tkinter provides the following geometry methods.
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.
● expand : If the expand is set to true, the widget expands to fill any space.
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.
● Rowspan : The height of the widget, i.e. the number of the row up to which the widget is expanded.
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.
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
● 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.
● 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().
● 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.
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.
● 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
0 comments:
Post a Comment
Please do not enter any spam link in the comment box