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:
Tkinter window can be created by using the following steps.
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 |
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: