Python GUI – tkinter:

In Python, multiple options for developing GUI is offered which is helpful in doing the data science work. The tkinter method is most commonly there of all GUI methods. It is a standardized Python interface to Tk GUI toolkit shipped in Python. Python, along with tkinter will give the fastest and easiest way of creating GUI applications. It is very easy to create a GUI using tkinter.

Creating a tkinter:
  1. Importing the tkinter module.
  2. Creating the main window(container).
  3. Adding a required number of widgets to main window.
  4. Applying the event trigger on the widgets.

Importing of tkinter is similar to importing other modules. One important thing we should know in order to avoid error is in Python 2.x ‘Tkinter’ is there, and in Python 3.x ‘tkinter’ is there for data science.

Two methods are there for creation of Python application using GUI:
  1. Tk(screenName=None, baseName=None, className=’Tk’, useTk=1): For the creation of main window, tkinter provides a method ‘Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)’. To change the name of the window, we can change the className to desired one. The basic code there for creation of main window of application is:
  2. mainloop(): A method known as name mainloop() is there when we are ready for the application to run. The mainloop() is an infinite loop there for running the application. We should wait for an event to occur and then process event until the window is not closed.
    Python GUI – tkinter for Data Science - PST Analytics

tkinter offers access to geometric configuration of the widgets which organize the widgets in parent windows in python for data science. Mainly three geometry manager classes class are there.

  1. pack() – It is there for organizing widgets in blocks before placement in parent widgets.
  2. grid() – It is there for organizing the widgets in grid(table like structure) before placement in the parent widget.
  3. place() – It is there for organizing the widgets by placing them on specific positions as directed by the programmer.
Many widgets can be put in the tkinter application. Some major widgets are discussed below:
  1. Button – This is there for adding a button to our application. The syntax there is:

The argument master is there for representing the parent window. We have many options which are there for changing the format of the Buttons. Many options can be passed as parameters separated by commas. Below are some options.

  • activebackground: It is there for setting the background color when the button is under the cursor.
  • activeforeground: It is there for setting the foreground color when the button is under the cursor.
  • bg: It can be there for setting normal background color.
  • command: there for calling a function.
  • font: It is there for setting the font on the button label.
  • image:  for setting image on the button.
  • width: for setting width of the button.
  • height: to set height of button.Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. Canvas:

    This is used for drawing pictures and other complex layout like graphics, text and widgets in python for data science. The syntax is as follows:

Many options are there to change the format of the widget. Many options can be passed as parameters separated by commas. Few are listed below.

  • bd – It is there for setting border width in pixels.
  • bg – It is there for setting normal background color.
  • cursor – It is there for setting the cursor used in the canvas.
  • highlightcolor – there for setting color shown in the focus highlight.
  • width – It is there for setting width of the widget.
  • height – It is there for setting height of the widget.

OUTPUT:

  1. CheckButton –

    It is used for selecting any number of options by displaying a number of options to a user as toggle buttons. The syntax used is:

We can pass many options as parameters separated by commas. Some of these are listed below:
  • Title – It sets the title of the widget.
  • activebackground – It is used for setting the background color when the widget is under cursor.
  • activeforeground – It is used for setting the foreground color when the widget is under cursor.
  • bg – Used for setting the normal background Steganography.
  • command – Used for calling a function.
  • font – It is used for setting the font on the button label.
  • image – It is used for setting image on the widget.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

  1. Entry – It can be used to input single line text entry from the user. In order to have multi line text input, the text widget is used. The syntax used is:
The parameter master is used for representing the parent window.

We have many options of changing the format of the widget. Many options can be passed as parameters separated by commas. Some of these are listed below which we can use in python for data science.

  • bd – Used for setting the border width in pixels.
  • bg – Used for setting the normal background color.
  • cursor – Used for setting the cursor used.
  • command – Used for calling a function.
  • highlightcolor – Used for setting the color shown in the focus highlight.
  • width – It is used for setting the width of the button.
  • height – It is used for setting the height of the button.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

  1. Frame – It is used in form of a container to hold the widgets. It comes into application for grouping and organizing the widgets. The syntax will be:

We have many options that are used for changing the format of the widget. Many options can be passed as parameters separated by commas in python for data science. Some are listed below.

  • highlightcolor – It is used for setting color of focus highlight when widget has to be focused.
  • bd – Used for setting the border width in pixels.
  • bg – Used for setting normal background color.
  • cursor – Used for setting the cursor.
  • width – Used for setting width of the widget.
  • height – Used for setting height of widget.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

  1. Label –

    It is used for referring to the display box where we can put any text or image that can be updated any time as per the code. The syntax used is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas in python for data science. Some are listed below:

  • bg – It is used for setting the normal background color.
  • command – It is used for calling a function.
  • font – Used for setting the font on the button label.
  • image – Used for setting image on the button label.
  • width – It is used for setting the width of the button.
  • height – Used for setting the height of the button.

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. Listbox –

    It will offer a list to the user from which he can accept any number of options.

The syntax is:

 Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • highlightcolor – Used for setting the color of focus highlight when the widget has to be focused.
  • bg – Used for setting the normal background color.
  • bd – Used for setting the border width in pixels.
  • font – Used for setting font on the button label.
  • image – Used to set image on the widget.
  • width – Used for setting the width of the widget.
  • height – Used for setting height of the widget.

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. MenuButton – It is a component of the top-down menu that stays on Window all the time. All menubuttons have their own functionality. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • activebackground – Used for setting the background when the mouse is over the widget.
  • activeforeground – Used for setting the foreground when the mouse is over the widget.
  • bg – Used for setting normal background color.
  • bd – Used for setting the border of the indicator.
  • cursor – It is used to make the cursor appear when the mouse is over the menubutton.
  • image – Used for setting the image on the widget.
  • width – Used for setting width of the widget.
  • height – Used for setting height of the widget.
  • highlightcolor – Used for setting the color of the focus highlight when widget has to be focused.

OUTPUT:

  1. Menu –

    It is used for creating all kinds of menus used by the application. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • title – It is used for setting the title of the widget.
  • activebackground – It is used for setting the background color when the widget is under cover.
  • activeforeground – It is used for setting foreground color when the widget is under the cursor.
  • bg – Used for setting normal background color.
  • command – It is used for calling a function.
  • font – It is used for setting the font on the button label.
  • image – It is used for setting image on the widget.

OUTPUT:

  1. Message – It is used for referring to the multi line and non editable text. It works similar to label. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • bd – It is used for setting border around the indicator.
  • bg – It is used for setting the normal background color.
  • font – It is used for setting the font on the button label.
  • image – It is used for setting image on the widget.
  • width – It is used for setting the width of the widget.
  • height – It is used for setting the height of the widget.

OUTPUT:

  1. RadioButton –

    It can be used for offering multi-choice option to user. It will offer several options to user and user has to choose one option. The syntax is:

 

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • activebackground – It is used for setting the background color when widget is under cursor.
  • activeforeground – It is used for setting foreground color when the widget is under the cursor.
  • bg – It is used for setting the normal background color.
  • command – It is used for calling a function.
  • font – It is used for setting the font on the button label.
  • image – Used for setting image on the widget.
  • width – It is used for setting the width of the label in characters.
  • height – It is used for setting the height of the label in characters.

OUTPUT:

  1. Scale – It is used for providing a graphical slider that allows selection of any value from that scale. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • cursor – It is used for changing the cursor pattern when the mouse is over the widget.
  • activebackground – It is used for setting the background of the widget when mouse is over the widget.
  • bg – It is used for setting the normal background color.
  • orient – We can set it to Horizontal or Vertical according to our requirement.
  • from_ – It is used for setting value of one end of the scale range.
  • to – Used for setting value of other end of the scale range.
  • image – Used for setting image on the widget.
  • width – Used for setting width of the widget.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. Scrollbar –

    It is used to refer to the side controller that can be used for implementing listed widgets. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • width – Used for setting width of the widget.
  • activebackground – It is used for setting background when the mouse is over the widget.
  • bg – It is used for setting normal background color.
  • bd – It is used for setting size of the border around the indicator.
  • cursor – It is used to appear the cursor when the mouse is set over the menubutton.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. Text – It is used for editing a multi-line text and formatting the way it has to be displayed. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • highlightcolor – It is used for setting the color of the focus highlight when the widget has to be focused.
  • Insertbackground – It is used for setting the background of the widget.
  • bg – It is used for setting the normal background color.
  • font – It is used for setting the font on the button label.
  • image – It is used for setting image on the widget.
  • width – It is used for setting width of the widget.
  • height – It is used for setting the height of the widget.

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. TopLevel –

    It is a widget which is directly controlled by windows manager. It does not need any parent window to work on. The syntax is:

Python GUI – tkinter for Data Science - PST Analytics

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • bg – It is used for setting normal background color.
  • bd – It is used for setting border around the indicator.
  • cursor – It is used to appear the cursor when the mouse is set over the menubutton.
  • width – Used for setting width of the widget.
  • height – Used to set the height of the widget.

OUTPUT:

  1. SpinBox – It is used as an entry of ‘Entry’ widget. Value is input by selecting a fixed value of numbers. The syntax is:

Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • bg – It is used for setting normal background color.
  • bd – It is used for setting size of the border around the indicator.
  • cursor – It is used for appearing the cursor when the mouse is kept over the menubutton.
  • command – It is used for calling a function.
  • width – It is used for setting width of the widget.
  • activebackground – It is used for setting background when the mouse is over the widget.
  • disabledbackground – It disables the background when the mouse is over the widget.
  • from_ – It is used for setting value of one end of range.
  • to – It is used for setting the value of other end of the range.

OUTPUT:

Python GUI – tkinter for Data Science - PST Analytics

  1. PannedWindow –

    It is a container widget that is used for handling number of panes arranged in it. The syntax is:

Python GUI – tkinter for Data Science - PST Analytics

The parameter master is used for representing the parent window.

  Many options are available to change the format of the widget. A number of options can be passes as parameters separated by commas. Some are listed below:

  • bg – It is used for setting normal background color.
  • bd – It is used for setting size of border around the indicator.
  • cursor – It is used to appear the cursor when the mouse is moved over the menubutton.
  • width – It is used for setting the width of the widget.
  • height – It is used for setting height of the widget.

Python GUI – tkinter for Data Science - PST Analytics

OUTPUT:

To learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *