Python GUI — tkinter

Python offers several options for developing a graphical user interface. Of all the GUI methods, tkinter is the most commonly used method. It is the standard Python interface for the Tk GUI toolkit that ships with Python. Python with tkinter outputs the fastest and easiest way to build GUI applications. Creating a GUI with tkinter — simple task.
To create a tkinter:

  1. Import module — tkinter
  2. Create main window (container)
  3. Add any number of widgets to the main window
  4. Apply an event trigger to widgets.

Importing tkinter is similar to importing any other module in Python code. Note that the module name in Python 2.x — “Tkinter”, and in Python 3.x — “Tkinter”.

 import tkinter 

There are two main methods a user should remember when building a Python GUI application.

  1. Tk (screenName = None, baseName = None, className = & # 39; Tk & # 39 ;, useTk = 1). To create the main window, tkinter offers the Tk (screenName = None, baseName = None, className = & # 39; Tk & # 39 ;, useTk = 1) “. To change the name of the window, you can change the className to what you want. The main code used to create the main application window:
     m = tkinter.Tk () where m is the name of the main window object 
  2. mainloop (): has a method known as mainloop () that is used when you are ready to run your application. mainloop () — it is an endless loop used to start the application, wait for an event, and process it until the window is closed.
     m.mainloop () 

    import tkinter

    m = tkinter.Tk ()

    "" "
    widgets added here
    "" "
    m.mainloop ()

tkinter also offers access to geometric widget configuration, which can organize widgets into parent windows. There are basically three classes of class geometry managers.

  1. The pack () method: organizes widgets into blocks before placing them in the parent widget.
  2. Grid () method: organizes widgets in a grid (table view) before placing them in the parent widget.
  3. place () method: organizes widgets by placing them on specific positions specified by the programmer.

There are a number of widgets that you can put in your tkinter application. Some of the main widgets are described below:

  1. Button : This widget is used to add a button to your application.
    General syntax:
     w = Button (master, option = value) 

    master — this is the parameter used to represent the parent window.
    There are a number of options that are used to change the format of the buttons. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • activebackground : Set the background color when the button is under the cursor.
    • activeforeground : Set the foreground color when the button is under the cursor.
    • bg : Set the normal background color.
    • Command : For function call.
    • Font : set the font on the button label.
    • image : set the image on the button.
    • width : set the width of the button.
    • height : set the height of the button.

    import tkinter as tk

    r = tk.Tk ()

    r.title ( `Counting Seconds` )

    bu tton = tk.Button (r, text = `Stop` , width = 25 , command = r.destroy)

    button.pack ()
    r.mainloop ()

    Output:

  2. Canvas: is used for drawing pictures and other complex layouts such as graphics, text and widgets.
    General syntax:
     w = Canvas (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • bd : Set the border width in pixels.
    • BG : Set the normal color background.
    • cursor : Set the cursor used on the canvas.
    • HighlightColor : Set the color displayed in the focus highlight.
    • width : set the width of the widget.
    • height : set the height of the widget.

    from tkinter import *

    master = Tk ()

    w = Canvas (master, width = 40 , height = 60 )

    w.pack ()

    canvas_height = 20

    canvas_width = 200

    y = int (canvas_height / 2 )

    w.create_line ( 0 , y, canvas_width, y)

    mainloop ()

    Output:

  3. CheckButto n: to select any number of options by displaying a number of options to the user as toggle buttons. General syntax:
     w = CheckButton (master, option = value) 

    There are a number of options that are used to change the format of this widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • Title : Set the title of the widget.
    • activebackground : Set the background color when the widget is under the cursor.
    • activeforeground : set the foreground color when the widget is under the cursor.
    • bg : set normal steganography background

      Change

      Secret code:

      Attach file: nd color.

    • Command : for function call.
    • Font : Set the font on the button label.
    • image : Set the image on the widget.

    from tkinter import *

    master = Tk ()

    var1 = IntVar ()

    Checkbutton (master, text = `male` , variable = var1) .grid (row = 0 , sticky = W)

    var2 = IntVar ()

    Checkbutton (master, text = `female` , variable = var2) .grid (row = 1 , sticky = W)

    mainloop ()

    Output:

  4. Write: is used to input a one-line text entry from the user. For multi-line text input, the Text widget is used.
    General syntax:
     w = Entry (master, option = value) 

    master — this is the parameter used to represent the parent window.
    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • bd : Set the border width in pixels.
    • bg : Set the normal color background.
    • cursor : Set the cursor to use.
    • Command : To call a function.
    • HighlightColor : set the color displayed in the focus highlight.
    • width : set the width of the button.
    • height : Set the height of the button.

    from tkinter import *

    master = Tk ()

    Label (master, text = ` First Name` ). grid (row = 0 )

    Label (master, text = `Last Name` ). grid (row = 1 )

    e1 = Entry (master)

    e2 = Entry (master)

    e1.grid (row = 0 , column = 1 )

    e2.grid (row = 1 , column = 1 )

    mainloop ()

    Output:

  5. Frame: acts as a container for storing widgets. It is used to group and organize widgets. General syntax:
     w = Frame (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • highlightcolor : Set the highlight color when the widget should be focused.
    • bd : Set the width of the border in pixels.
    • BG : Set the normal background color.
    • Cursor : Set the cursor to use.
    • width : set the width of the widget.
    • height : set the height of the widget.

    from tkinter import *

     

    root = Tk ()

    frame = Frame (root)

    frame.pack ()

    bottomframe = Frame (root)

    bottomframe .pack (side = BOTTOM)

    redbutton = Button (frame, text = `Red` , fg = ` red ` )

    redbutton.pack (side = LEFT)

    greenbutton = Button (frame, text = `Brown` , fg = `brown` )

    greenbutton.pack ( side = LEFT)

    bluebutton = Button (frame, text = ` Blue` , fg = `blue` )

    bluebutton.pack (side = LEFT)

    blackbutton = Button (bottomframe, text = `Black` , fg = ` black` )

    blackbutton.pack (side = BOTTOM)

    root.mainloop ()

    Output:

  6. Label : refers to the display window where you can place any text or image that can be updated at any time to match with code.
    General syntax:
     w = Label (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • bg : set the background color to normal.
    • BG, to set the normal color background.
    • Command : to call a function.
    • Font : set the font on the button label.
    • image : set the image to the button.
    • width : set the width of the button.
    • height "To set the height of the button.

    from tkinter import *

    root = Tk ()

    w = Label (root, text = `GeeksForGeeks.org!` )

    w.pack ()
    root.mainloop ()

    Output:

  7. Listbox: It offers a user list from which the user can accept any number of options.
    General syntax:
     w = Listbox (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • highlightcolor : Set the highlight color when the widget should be focused.
    • bg : Set the normal background color.
    • bd : Set the border width in pixels.
    • Font : Set the font on the button label .
    • image : set the image to the widget.
    • width : set the width of the widget.
    • height : Set the height of the widget.

    from tkinter import *

     

    top = Tk ()

    Lb = Listbox (top)

    Lb.insert ( 1 , `Python` )

    Lb.insert ( 2 , `Java` )

    Lb.insert ( 3 , `C ++` )

    Lb.insert ( 4 , ` Any other` )

    Lb.pack ()
    top.mainloop ()

    Output:

  8. MenuButton : This is part of the pull-down menu that stays in the window at all times. Each menu has its own functionality. General syntax:
     w = MenuButton (master,option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • activebackground : Set the background when the mouse is over the widget.
    • activeforeground : set the foreground when the mouse is over the widget.
    • bg : set the normal background color.
    • bd : set the size of the border around the indicator.
    • cursor : to display the cursor when hovering over the menu.
    • image : to set an image on the widget.
    • width : set the width of the widget.
    • height : set the height of the widget.
    • highlightcolor : Set the focus highlight color when the widget should be focused.

    from tkinter import *

     

    top = Tk ()

    mb =   Menubutton (top, text = & amp; quot; GfG & amp; quot;)

    mb.grid ()

    mb.menu  =   Menu (mb, tearoff = 0 )

    mb [& amp; quot; menu & amp; quot;]  =   mb.menu

    cVar  = IntVar ()

    aVar = IntVar ()

    mb.menu.add_checkbutton (label = `Conta ct` , variable = cVar)

    mb.menu.add_checkbutton (label = `About` , variable = aVar)

    mb.pack ()
    top.mainloop ()

    Output:

  9. Menu : Used to create all kinds of menus used by the application.
    General syntax:
     w = Menu (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of this widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • title : set the title of the widget
    • activebackground : set the background color when the widget is under the cursor.
    • activeforeground : Set the foreground color when the widget is under the cursor.
    • bg : Set the normal color background.
    • Command : to call a function.
    • Font : set the font on the button label.
    • image : Set the image to the widget.

    from tkinter import *

                       

    root = Tk ()

    menu = Menu (root)

    root.config (menu = menu)

    filemenu = Menu (menu)

    menu.add_cascade (label = `File` , menu = filemenu)

    filemenu.add_command (label = `New` )

    filemenu.add_command (label = `Open ...` )

    filemenu.a dd_separator ()

    filemenu.add_command (label = ` Exit` , command = root.quit)

    helpmenu = Menu (menu)

    menu.add_cascade (label = `Help` , menu = helpmenu)

    helpmenu.add_command (label = `About` )

    mainloop ()

    Output:

  10. Message : refers to multi-line and non-editable text. This works the same as Label. 
    General syntax:
     w = Message (master, option = value) master is the parameter used to represent the parent window. 

    There are a number of options that are used to change the format of the widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • bd : set a border around the indicator.
    • bg : set a normal background color .
    • Font : set the font on the button label.
    • image : set the image on the widget.
    • width : set the width of the widget.
    • height : set the height of the widget.

    from tkinter import *

    main = Tk ()

    ourMessage = `This is our Message`

    messageVar = Message (main, text = < code class = "plain"> ourMessage)

    messageVar.config (bg = `lightgreen` )

    messageVar.pack ()
    main.mainloop ()

    Output:

  11. RadioButton: is used for giving the user a choice of multiple options. It offers several options for the user and the user has to choose one option. 
    General syntax:
     w = RadioButton (master, option = value) 

    There are a number of options that are used to change the format of this widget. The number of parameters can be passed as parameters separated by commas. Some of them are listed below.

    • activebackground : Set the background color when the widget is under the cursor.
    • activeforeground : set the foreground color when the widget is under the cursor.
    • bg : set the normal background color.
    • Command : for function call.
    • Font : set the font on the button label.
    • image : set the image on the widget.
    • width : set the width of the label in characters.
    • height : set the height of the label in characters.

    from tkinter import *

    root =