Tkinter: ======== GUI ==> Graphical User Interface How to use the Tkinter: ======================= 1) Import the tkinter module ============================= Python-2.x ==> Tkinter Python-3.x ==> tkinter Syntax: import tkinter or from tkinter import * 2) we need to create the main object/window (container) 3) we can add widgets to the main object/window. 4) Apply event trigger to the widget =========================================== First Tkinter GUI Application: ============================== Tk(): ===== ==> used to create the main window Syntax: object-name/window-name = Tk(screenname = None, baseName = None, className = "Tk", useTk = 1) mainloop(): =========== window ==> widget ==> to run the GUI app mainloop() ==> Infinite loop import tkinter obj = tkinter.Tk() # can add new window obj.mainloop() =============================================== Adding of Widgets: ================== 1) Label(): =========== ==> can used to display the box where we can put text/image. text/image which can be updated any time as per the code. Syntax: object = Label(master, option = value) from tkinter import * # main window object obj = Tk() # adding the widget widget = Label(obj, text = "Welcome To Ashok IT") widget.pack() obj.mainloop() ============================================ Button(): ========= Syntax: object = Button(master, option = value) import tkinter as tk obj = tk.Tk() obj.title("Counting Seconds") btn = tk.Button(obj, text = "File", width = 10, command = obj.destroy) btn.pack() obj.mainloop() ================================== Entry(): ======== ==> used to enter the single line text from the user. Syntax: object = Entry(master, option = value) 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() =========================================== Radiobutton(): ============= Gender: Male Female Other Syntax: object = Radiobutton(master, option = value) from tkinter import * obj = Tk() v = IntVar() Radiobutton(obj,text="Male",variable=v,value=1).pack() Radiobutton(obj, text = "Female", variable = v, value = 2).pack() Radiobutton(obj,text = "Others", variable = v, value = 3).pack() mainloop() ============================== Checkbutton(): ============== Syntax: object = Checkbutton(master, option = value) from tkinter import * master = Tk() v1 = IntVar() Checkbutton(master, text = "Reading", variable = v1).grid(row = 0, sticky = W) v2 = IntVar() Checkbutton(master, text = "Surfing", variable = v2).grid(row = 1, sticky = W) mainloop() =============================== Listbox(): ========= Syntax: object = Listbox(master, option = value) from tkinter import * top = Tk() Lb = Listbox(top) Lb.insert(1,"Python") Lb.insert(2,"Java") Lb.insert(3,"C++") Lb.insert(4,"C#") Lb.insert(5, "Any Other") Lb.pack() top.mainloop()