Best post- LabelFrame, Toplevel, PanedWindow widgets

LabelFrame, Toplevel, PanedWindow arewidgets of tkinter in Python

Tkinter provides many widgets for GUI in Python.

LabelFrame Widget

The LabelFrame widget is used to draw a border around its child widgets.
This widget is a variant of the Frame widget which has all the features of a frame.
It can display a label.
Its primary purpose is to act as a container for complex window layouts.

Syntax
Here is the simple syntax to create this widget −

w = LabelFrame(master, option=value, ... )

Parameters

master − This represents the parent window.

options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Various options of LabelFrame widget:

  • bg : The normal background color displayed on window
  • bd : The size of the border around the window. Default is 2 pixels.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
  • font : If you are displaying text in this widget(with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : It is used to give foreground colour.
  • width: Width of the widget in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
  • height : It represents the height of the window.
  • highlightbackground : The highlightcolor when the widget doesn’t has the focus.
  • highlightthickness : The thickness of the focus highlight. The default value is 1.
  • highlighcolor : The color of the focus highlight when the widget has the focus.
  • labelAnchor : It specifies where to place the label.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :7th April 2020

Description : Program to show use of LabelFrame widget

"""
from tkinter import *

root = Tk()

labelframe = LabelFrame(root, text="This is a LabelFrame 1 of ITVoygers",bg="orange",fg="black",font="Verdana",relief=RAISED)
labelframe.pack(fill="both", expand="yes")
 
lab = Label(labelframe, text="I am label1 Inside the LabelFrame 1",bg="green",fg="black",font="Verdana")
lab.pack()
 
lab = Label(labelframe, text="I am label2 Inside the LabelFrame 1",bg="light green",fg="black",font="Verdana")
lab.pack()

labelframe = LabelFrame(root, text="This is a LabelFrame 2 of ITVoygers",bg="pink",fg="purple",font="Verdana",relief=RAISED)
labelframe.pack(fill="both", expand="yes")
 
lab = Label(labelframe, text="I am label1 Inside the LabelFrame 2",bg="purple",fg="black",font="Verdana")
lab.pack()

lab = Label(labelframe, text="I am label2 Inside the LabelFrame 2",bg="sky blue",fg="black",font="Verdana")
lab.pack()


OUTPUT

Output of LabelFrame widget (itvoyagers.in)

Toplevel Widget

A container widget displayed as a separate, top-level window.
The Toplevel widget work pretty much like Frame, but it is displayed in a separate, top-level window. Such windows usually have title bars, borders, and other “window decorations”.
They do not necessarily have a parent widget on top of them.
The toplevel widget may or may not have the parent window on the top of them.
The toplevel windows have the title bars, borders, and other window decorations.

Syntax
Here is the simple syntax to create this widget −

w = Toplevel( option=value, ... )

Parameters

options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Various options of Toplevel widget:

  • bg : The normal background color displayed on window
  • bd : The size of the border around the window. Default is 2 pixels.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
  • class_ : The text selected in the text widget is exported to be selected to the window manager. We can set this to 0 to make this behavior false.
  • font : If you are displaying text in this widget(with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : It is used to give foreground colour.
  • width: Width of the widget in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
  • height : It represents the height of the window.

Various methods of Toplevel widget:

  • deiconify() : This method is used to display the window.
  • frame() : It is used to show a system dependent window identifier.
  • group(window) : It is used to add this window to the specified window group.
  • iconify() : Turns the window into an icon, without destroying it.
  • protocol(name, function) : This method is used to mention a function which will be called for the specific protocol.
  • state() : Returns the current state of the window. Possible values are normal, iconic, withdrawn and icon.
  • transient([master]) : It is used to convert this window to a transient window (temporary).
  • withdraw() : It is used to delete the window but doesn’t destroy it.
  • maxsize(width, height) : Defines the maximum size for this window
  • minsize(width, height) : It is used to declare the minimum size for the window.
  • positionfrom(who) : Used to define the position controller.
  • resizable(width, height) : It is used to control whether the window can be resizable or not.
  • sizefrom(who) : Used to define the size controller.
  • title(string) : Used to define the title for the window.
    • Example

      """
      Author : ITVoyagers (itvoyagers.in)
      
      Date :7th April 2020
      
      Description : Program to show use of Toplevel widget
      
      """
      from tkinter import *  
        
      root = Tk()   
        
      def open_toplevel():  
          top = Toplevel(root)  
          top.mainloop()  
        
      itv = Button(root, text = "open toplevel window", command = open_toplevel)  
        
      itv.pack() 
        
      
      
      

      OUTPUT

      Output of Toplevel Widget (itvoyagers.in)

      PanedWindow Widget

      The PanedWindow contains multiple panes.
      The PanedWindow widget acts like a Container widget
      It can display a label.
      It can be arranged horizontally or vertically.
      The child panes can be resized by the user, by moving the separator lines known as sashes by using the mouse

      Syntax
      Here is the simple syntax to create this widget −

      w = PanedWindow(master, option=value, ... )
      

      Parameters

      master − This represents the parent window.

      options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

      Various options of PanedWindow widget:

      • bg : The normal background color displayed on window
      • bd : The size of the border around the window. Default is 2 pixels.
      • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
      • orient : Default is HORIZONTAL else it can be set to VERTICAL.
      • fg : It is used to give foreground colour.
      • width: Width of the widget in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
      • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
      • height : It represents the height of the window.
      • handlepad : This option represents the distance between the handle and the end of the sash. For the horizontal orientation, it is the distance between the top of the sash and the handle. The default is 8 pixels.
      • handlesize : It represents the size of the handle. The default size is 8 pixels. However, the handle will always be a square.
      • sashpad : Represents the padding to be done around each sash. The default is 0.
      • sashrelief : Represents the type of the border around each of the sash. The default is FLAT.
      • sashwidth : Represents the width of the sash. The default is 2 pixels.
      • showhandle : It is set to True to display the handles. The default value is false.

      Various methods of PanedWindow widget:

Leave a Comment