Friday, March 13, 2020

tkinter in Python


Tkinter


Tkinter is Python's well known GUI (Graphical User Interface) library. This Tutorial describes the usage and features of this library. Python and Tkinter enables programmers with a fast and easy way to produce GUI based applications. This is the simplest and most popular technique to manufacture GUI-based applications. The name Tkinter is an acronym from the tk interface. The Tkinter is an open-source framework and is available for various platforms like Linux, Unix, Mac, and Windows. 

Widgets

The Widgets are Graphical user interface components, that are added to a GUI Frame. Tk provides the following widgets,
  • button
  • canvas
  • checkbutton
  • combobox
  • entry
  • frame
  • label
  • labelframe
  • listbox
  • menu
  • menubutton
  • message
  • notebook
  • tk_optionMenu
  • panedwindow
  • progressbar
  • radiobutton
  • scale
  • scrollbar
  • separator
  • sizegrip
  • spinbox
  • text
  • treeview

Tkinter also provides these following top-level windows

  • tk_chooseColor - dialog box to choose a color.
  • tk_chooseDirectory - a dialog box to select a directory.
  • tk_dialog - a modal dialog and waits for user response.
  • tk_getOpenFile - a dialog box to choose a file to open.
  • tk_getSaveFile - a dialog box to select a file to save.
  • tk_messageBox - a message window
  • tk_popup - a popup menu.
  • toplevel - manufactures and manipulates top-level widgets.


Geometry Managers


Tkinter has three Geometry managers for organizing geometry: place, pack, and grid.


  1. The "place": used for absolute pixel coordinates.
  2. The "pack": puts widgets into one of four sides. New widgets will be kept next to existing widgets.
  3. The "grid": places widgets into a grid which is equivalent to a dynamically resizing screen.

A Simple GUI for displaying HelloWorld message

import tkinter as tk
class SimpleFrame(tk.Frame):
 
#Creating the constructor of the Class simple frame
 
def __init__(self,parent):
   
# invoke a constructor of the superclass
   
tk.Frame.__init__(self,parent)
   
# Put the frame into main window
   
self.grid()
     
# Create a text box with "Hello World" text
   
hello =tk.Label(self,text="Hello World")
   
# Create a label with text Hello World
   
hello.grid(row=10,column=10)
   
# Spawn window

if __name__ == "__main__":
 
# Create main window object
 
root =tk.Tk()
 
# provide the title to the window
 
root.title("My First Simple Tk Frame")
  simple_frame =SimpleFrame(root)
 
#GUI
 
simple_frame.mainloop()



Output:

tkinter



Place

The argument for the widget.place are x, the absolute x-coordinate of the widget, y, the absolute y-coordinate of the widget, the height, the absolute height of the widget, the width, the absolute width of the widget.

A code example using place:

from tkinter import * #Import tkinter
class SimpleFrame(Frame):
 
def __init__(self,parent):
    Frame.__init__(self,parent)
    self.grid()
    origin_message=Label(
parent,text="text at the origin")
    origin_message.place(x=0,y=0,height=50,width=200)
    label_bottum=Label(
parent,text="Text at position 300,300")
    label_bottum.place(x=300,y=300,height=50,width=200)
   
# Spawn Window

if __name__=="__main__":
  root=Tk()
  myframe=SimpleFrame(root)
  myframe.mainloop()

Output:

place tkinter




Grid

This is the most frequently used for managing the geometry. The arguments of widget.grid are,

row, represents the row of the widget 
rowspan, represents the number of columns a widget spans (its default value is 1) 
column, represents the column of the widget (its default value is 0) 
columnspan, represents the number of columns a widget spans (its default value is 1) 
sticky, represents where to put the widget if the grid cell is larger than it. It can be combination of directions like N,NE,E,SE,S,SW,W,NW.

from tkinter import #Import tkinterclass SimpleFrame(Frame):
  
def __init__(self,parent):
    Frame.__init__(self,parent)
    self.grid()
    origin_message=Label(
parent,text="text at the origin")
    origin_message.place(x=0,y=0,height=50,width=200)
    label_bottum=Label(
parent,text="Text at position 300,300")
    label_bottum.place(x=300,y=300,height=50,width=200)
    
# Spawn Windowif __name__=="__main__":
  root=Tk()
  myframe=SimpleFrame(root)
  myframe.mainloop()

Output


grid tkinter


Pack

widget.pack have following arguments,
expand, specifies whether or not to fill space not occupied by the parent
fill, specifies whether to expand to fill entire space (NONE (default), X, Y, or BOTH)
side, specifies the side to pack against (TOP (default), BOTTOM, LEFT, or RIGHT)

from tkinter import *
class PackFrame(Frame):
 
def __init__(self,parent):
    Frame.
__init__(self,parent)
   
self.pack()
    top_label=Label(
parent,text="This is top label")
    top_label.pack()
    bottom_label=Label(
parent,text="This is at bottom label")
    bottom_label.pack()

# Spawn Window

if __name__=="__main__":
  root=Tk()
  packframe=PackFrame(root)
  packframe.mainloop()



Output


pack tkinter