Ad

Python GUI - Tkinter

Python provides the standard library Tkinter for creating the graphical user interface for desktop based applications.

Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. Graphical User Interface(GUI) is a form of user interface which allows users to interact with computers through visual indicators using items such as icons, menus, windows, etc. It has advantages over the Command Line Interface(CLI) where users interact with computers by writing commands using keyboard only and whose usage is more difficult than GUI.

Some other Python libraries are available for creating GUI applications like Python turtle, Python Qt, wxPython, jPythonf etc

Key features of Tkinter:

  1. Standard Library: Tkinter is the inbuilt python module, no need to install it separately.
  2. Cross-Platform: Applications built with Tkinter can run on various operating systems, including Windows, macOS, and Linux.
  3. Widget-Based: Tkinter provides a collection of pre-built GUI elements, known as widgets, such as buttons, labels, text boxes, entry fields, menus, and more.
  4. Event-Driven Programming: Tkinter supports event-driven programming, where actions are triggered and handled in response to user interactions like clicks, key presses, or other events.

Tkinter window can be created by using the following steps.

  1. Import the Tkinter module
  2. Create the main application window
  3. Add the widgets like labels, buttons, frames, etc. to the window
  4. Call the main event loop so that the actions can take place on the user's computer screen

Widgets in Tkinter

Widgets in Tkinter are the elements of GUI application which provides various controls (such as Labels, Buttons, ComboBoxes, CheckBoxes, MenuBars, RadioButtons and many more) to users to interact with the application.

S.No Widgets Description
Label It is used to display text or image on the screen
Button It is used to add buttons to your application
Entry It is used to input single line text entry from user

Example

from tkinter import *

# Create the main window
window = Tk()
window.title("My Tkinter App")
window.geometry("300x200") # Set window size

# Add a label
label = Label(window, text="Hello, Tkinter!")
label.pack(pady=20) # Pack the label into the window with padding

# Start the event loop
window.mainloop()

Button: A clickable button that can trigger an action.

button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()

Entry: It is used to input the single line text

from tkinter import *
root = Tk()
Label(root, text='First Name').grid(row=0)
Label(root, text='Last Name').grid(row=1)
e1 = Entry(root)
e2 = Entry(root)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
root.mainloop()

CheckButton: A checkbox can be toggled on or off.

from tkinter import *

master = Tk()
var1 = IntVar()
Checkbutton(master, text='Option 1', variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text='Option 2', variable=var2).grid(row=1, sticky=W)
mainloop()

Radio Button

v = StringVar(master, "1")
Radiobutton(master, text="Radio Option 1", variable=v, value="value1").pack(side=TOP, ipady = 5)

Ad: