Python Widgets or Tkinter Widgets

Kailash Chandra Behera | Monday, August 03, 2020

Introduction

In my previous article(Python GUI Programming using Tkinter) we demonstrated how to display the tkinter windows in Python application, in this article we will discuss what are the various widgets provided by Python tkinter and some code example of commonly used widgets

Getting Started

Widgets are standard graphical user interface (GUI) elements and nothing but controls like button, canvas, grid etc. which we are using in Windows application in .Net Technology. Python tkinter provides various widgets like other technology for windows application. Here in this article we have discussed about some main widgets which are frequently used in Python GUI application.

The Python tkinter proides various widgets whose purpose is to contain other widgets. For example Tkinter module provides Frame(ttk frame) and TopLevel widgets to contain other widgets like Button, Text and Level. Here below are the most common used container widgets are listed downs

Frame

It acts as a container to hold the widgets. It is used for grouping and organizing the widgets. Frame represents a rectangular area of the screen contained in other frames or top-level windows.
Syntax:-
 fram = Frame (master, option=value)   

In the above syntax master is the main window or parent container widget. The options parameter is used to format the frame, there are a various number of options provided by the Tkinter module of Python. Options are passed by separating them using commas, the most common option are listed below.

  1. highlightcolor:-Set the color of the focus highlight when widget has to be focused.
  2. bd:-Set the border width in pixels.
  3. Bg:-Set the normal background color.
  4. cursor:-Set the cursor used.
  5. width:-Set the width of the widget.
  6. height:-Set the height of the widget.
Example: -
 from tkinter import *  
 from tkinter import messagebox  
 r = Tk();  
 r.title('Frame Widget Demo')   
 frame = Frame(r);  
 frame.pack();   
 def callBack(mss):  
  messagebox.showinfo("Frame Widget Demo",mss);  
 button = Button(frame, text='Click Me',fg ='red',command=lambda:callBack('I am Red'))  
 button.pack(side=LEFT);   
 button = Button(frame, text='Click Me',fg ='blue',command=lambda:callBack('I am Blue'))  
 button.pack(side=LEFT);   
 button = Button(frame, text='Click Me',fg ='yellow',command=lambda:callBack('I am Yellow'))  
 button.pack(side=LEFT);   
 r.mainloop();   

In the above example, the Frame widget contains three buttons Red, Blue, and Yellow which call a function called ‘callBack’ which displays a message. Buttons are arranged from left to right in the Python Frame.

Tkinter Widgets

Python Widgets

Top-level

Top-level represents a rectangular area of the screen that is a top-level window. This widget is directly controlled by the window manager and It doesn’t need any parent window to work on. Each instance of Toplevel can interact with the window manager and can contain other widgets. Every program using Tkinter has at least one top-level window, known as the root window.

If you want to have more than one top-level window, first instantiate the main one with root=Tkinter.Tk( ). Later in your program, you can instantiate other top-level windows as needed, with calls such as another_toplevel=Tkinter.Toplevel( ).

Syntax:-1 For Main window
 root=Tkinter.Tk().  
Syntax:-1 For Top-level window
 toplevel=Tkinter.Toplevel( ).  
Example:-
 from tkinter import *  
 root = Tk()  
 root.title('Main Window')   
 top = Toplevel()  
 top.title('Top Level')   
 root.mainloop()  
Python Widgets

Python Widgets

PanedWindow

Paned Window is a container widget which is used to handle number of panes arranged in it. It can contain any number of panes arranged horizontally or vertically but each pane can contain only one widgets.

Syntax:-
 paneWindow = PanedWindow( master, option, ... )  

master parameter is representing to the container of the paned window. The option parameter represents to the values which is used to customize or format paned window. The most common used options is listed below.

  1. bg:-The color of the slider and arrowheads when the mouse is not over them.
  2. Borderwidth:-Set border widh of paned window the default is 2.
  3. Cursor:-The cursor that appears when the mouse is over the window.
  4. Height:-Sets the height of paned window.
  5. Width:-Sets the width of paned window
  6. Orient:-Set the orientation of paned window , default is horizontal.
Functions
  1. add:-Adds a child widgets to the paned window.
  2. get:-This method returns a specific character or a range of text.
Example:-
 from tkinter import *  
 m1 = PanedWindow()  
 m1.pack(fill=BOTH,expand=1)  
 lebel1 = Label(m1,bg="Red", text="lebel1")  
 m1.add(lebel1)  
 lebel2 = Label(m1,bg="Yellow", text="lebel2")  
 m1.add(lebel2)  
 m2 = PanedWindow(m1, orient=VERTICAL)  
 m1.add(m2)  
 lebel3 = Label(m2,bg="Blue", text="lebel3")  
 m2.add(lebel3)  
 lebel4 = Label(m2,bg="Wheat", text="lebel4")  
 m2.add(lebel4)  
 mainloop()  

In the above example, the demonstrations demonstrated with two paned window with label widgets having orientation example horizonal and vertical.

Tkinter Widgets

Tkinter Widgets

Tkinter Widgets

  1. Button: The tkinter button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.
  2. Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
  3. Check Button: To select any number of options by displaying a number of options to a user as toggle buttons.
  4. Tkinter Entry: It is used to accept inputs text from user the single line text entry from the user.
  5. Tkinter Text: It is similar like Entry, but it accepts multiple lines.
  6. Label : A Label widget shows text to the user. You can update the widget programmatically too.
  7. Frame :It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.
  8. List box: The tkinter offers a list to the user from which the user can accept any number of options.
  9. Message: The widget can be used to display short text messages. The message widget is similar in its functionality to the Label widget, but it is more flexible in displaying text.
  10. Tkinter RadioButton: It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option.
  11. Scale: It is nothing but slider that allows to select any value from that scale.
  12. Scrollbar: It refers to the slide controller which will be used to implement listed widgets.
  13. Panned Window: It is a container widget which is used to handle number of panes arranged in it.
  14. Spin Box: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.

Summary

This article demonstrated how to work with Python container widgets and various type of tkinter widgets. I hope you have enjoyed it a lot.

Thanks