import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_w.mainloop() # Keep the window open
| Entry e1 | e1=tk.Entry(my_w,text='plus2net',font=24) e1.get() to read entries e1.delete(0,end) to remove entries textvariable=StringVar() To read or update user entries | |
| Button b1 | b1=tk.Button(my_w,text=’My Button’,width=10,bg=’green’) command=lambda:my_function() to add click event w=b1.cget(‘width’) width of the button state= normal, active or disabled relief =raised , sunken ,flat, ridge, solid & groove Border style | |
| Checkbutton | c1_v1=tk.StringVar() c1 = tk.Checkbutton(my_w, text='PHP', variable=c1_v1, onvalue='Yes',offvalue='No',command=my_upd) c1_v1.get() Read the value c1_v1.set(‘No) Set the value | |
| Radiobutton | r1_v = tk.StringVar() r1_v.set('Passed') Set the value or select the radio button by default r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value='Passed',command=my_upd) r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value='Failed',command=my_upd) | |
| Combobox | sel=tk.StringVar() cb1 = ttk.Combobox(my_w, values=months,width=7,textvariable=sel) cb1.get() Value of the selected option cb1.current() index of the current selection cb1.delete(0,'end') Clear the selection cb1['values'] +=('my new',) Adding option state=active, normal, disabled | |
| Label | font1=[‘Times’,28,’underline’] my_str = tk.StringVar() l1 = tk.Label(my_w, textvariable=my_str, width=10,font=font1,bg='yellow', anchor='center' ) my_str.get() Read the Label my_str.set(‘Welcome’) Update the text relief=raised, sunken, ridge, solid,flat, groove | |
| Text | t1 = tk.Text(my_w, height=1, width=20,bg='yellow') my_str=t1.get("1.0",'end-1c') Read the entry t1.insert(tk.END, my_str) Insert text | |
| Treeview | trv=ttk.Treeview(my_w,selectmode='browse') trv["columns"]=("1","2") trv['show']='headings' trv.column("1",width=30,anchor='c') trv.column("2",width=80,anchor='c') trv.heading("1",text="id") trv.heading("2",text="Name") i=1 trv.insert("",'end',iid=i, values=(i,'Alex') ) | |
| Progressbar | prg1 = ttk.Progressbar(my_w,orient = HORIZONTAL,value=70,length = 300,mode = 'determinate') prg1['value'] =80 Update the value or read the value | |
| Spinbox | t1 = StringVar() sb1 = Spinbox(my_w, from_= 0, to = 10,width=5,textvariable=t1) t1.set(8) Update t1.get() Read | |
| StringVar() | str1=tk.StringVar(value='Option 1') str1.set(‘Option 2’) Update str1.get() Read str1.trace_add('write',my_upd) | |
| Events |
my_w.bind("<Key>", my_event) Key press event my_w.bind("<Button-1>", my_event) Mouse left click event my_w.bind("<Enter>", my_event) Mouse enters widget event.widget Widget object that triggered the event event.x, event.y Mouse coordinates | |
| Message Box |
from tkinter import messagebox messagebox.showinfo("Title", "Info Message") Information message messagebox.showwarning("Title", "Warning Message") messagebox.askyesno("Confirm", "Do you want to exit?") Returns True or False messagebox.askquestion("Confirm", "Delete item?") Returns 'yes' or 'no' | |
| Layout |
w.grid(row=0, column=1, padx=10, pady=5) Grid placement w.pack(side='left', fill='x', expand=True) Pack placement with expansion w.place(x=50, y=30) Place with absolute coordinates sticky='ewns' Stretching in all directions in grid | |
| Canvas |
canvas1 = tk.Canvas(my_w, width=300, height=200, bg='white') canvas1.create_line(10, 10, 200, 10, fill='blue', width=2) Draw a line canvas1.create_rectangle(50, 30, 150, 100, fill='red') Draw a rectangle canvas1.create_oval(100, 50, 200, 150, fill='green') Draw an oval (circle) canvas1.create_text(150, 100, text="Hello", font=('Arial', 12)) Draw text | |
| PanedWindow |
pw = tk.PanedWindow(my_w, orient=tk.HORIZONTAL) left = tk.Label(pw, text="Left Pane", bg='lightblue') right = tk.Label(pw, text="Right Pane", bg='lightgreen') pw.add(left) pw.add(right) pw.pack(fill=tk.BOTH, expand=1) Resizable panes | |
| Notebook (Tabs) |
notebook = ttk.Notebook(my_w) tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text='Tab 1') notebook.add(tab2, text='Tab 2') notebook.pack(expand=1, fill='both') Tabbed interface | |
| Scale |
scale1 = tk.Scale(my_w, from_=0, to=100, orient=tk.HORIZONTAL) scale1.get() Get current value scale1.set(50) Set value resolution=5 Step size | |
| Scrollbar |
scrollbar = tk.Scrollbar(my_w, orient=tk.VERTICAL) listbox = tk.Listbox(my_w, yscrollcommand=scrollbar.set) scrollbar.config(command=listbox.yview) scrollbar.pack(side='right', fill='y') listbox.pack(side='left', fill='both', expand=True) Vertical scrollbar for Listbox | |
| Listbox |
lb = tk.Listbox(my_w, selectmode='multiple') lb.insert(0, 'Python') lb.insert(1, 'Java') selected = lb.curselection() Get selected indices lb.get(selected[0]) Get selected value | |
| Toplevel (Popup Window) |
def open_window(): top = tk.Toplevel(my_w) top.title("New Window") tk.Label(top, text="This is a popup").pack() btn = tk.Button(my_w, text="Open", command=open_window) btn.pack() Popup child window | |
| Menu |
menu = tk.Menu(my_w) my_w.config(menu=menu) file_menu = tk.Menu(menu, tearoff=0) menu.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Open") file_menu.add_separator() file_menu.add_command(label="Exit", command=my_w.quit) Dropdown file menu | |
| Tooltip (with Hover Effect) |
from idlelib.tooltip import Hovertip btn = tk.Button(my_w, text="Hover me") btn.pack() Hovertip(btn, "This is a tooltip") Show tip on hover | |
| Frame |
frame1 = tk.Frame(my_w, bg="lightgray", width=200, height=100) frame1.pack(padx=10, pady=10) tk.Label(frame1, text="Inside Frame").pack() Group widgets inside | |
| Separator |
sep = ttk.Separator(my_w, orient='horizontal') sep.pack(fill='x', padx=10, pady=5) Draw a horizontal separator | |
| PanedWindow (Vertical) |
pw = tk.PanedWindow(my_w, orient='vertical') pw.pack(fill='both', expand=True) top = tk.Label(pw, text="Top Pane", bg='lightblue') bottom = tk.Label(pw, text="Bottom Pane", bg='lightgreen') pw.add(top) pw.add(bottom) Vertically split panels | |
| File Dialog |
from tkinter import filedialog file_path = filedialog.askopenfilename(title="Open File") print(file_path) Select a file | |
| Color Picker |
from tkinter import colorchooser color = colorchooser.askcolor(title="Choose Color") print(color[1]) Get HEX value | |
| Date Picker (using tkcalendar) |
from tkcalendar import DateEntry cal = DateEntry(my_w, width=12, background='darkblue', foreground='white', borderwidth=2) cal.pack(pady=10) print(cal.get()) Read selected date | |
| ScrolledText |
from tkinter.scrolledtext import ScrolledText txt = ScrolledText(my_w, width=30, height=10) txt.pack() txt.insert(tk.END, "Welcome to plus2net") Multi-line text with scroll | |
| Timer / Clock |
def update_time(): current = time.strftime('%H:%M:%S') lbl.config(text=current) lbl.after(1000, update_time) lbl = tk.Label(my_w, font=('Helvetica', 24)) lbl.pack() update_time() Live time display | |
| ttkbootstrap Window |
import ttkbootstrap as tb my_w = tb.Window(themename='superhero') my_w.title("Plus2net App") my_w.geometry("400x250") my_w.mainloop() Create styled window | |
| ttkbootstrap Button |
btn = tb.Button(my_w, text="Submit", bootstyle="success", command=my_fun) btn.pack(pady=10) btn.config(state='disabled') Add event & style | |
| ttkbootstrap LabelFrame |
frm = tb.LabelFrame(my_w, text="Student Info", bootstyle="info") frm.pack(padx=10, pady=10, fill='x') Group widgets together | |
| Python 3.12 Match Case |
val = "two" match val: case "one": print("1") case "two": print("2") case _: print("Other") Modern pattern matching | |
| Python 3.12 f-string Debug |
name = "plus2net" print(f"{name=}") Shows name='plus2net' | |
| ttkbootstrap Toast Notification |
from ttkbootstrap.dialogs import ToastNotification toast = ToastNotification(title="Saved", message="Your file is saved", duration=3000) toast.show_toast() Show popup message | |
| ttkbootstrap Font Dialog |
from ttkbootstrap.dialogs import FontDialog fd = FontDialog() fd.show() print(fd.result) Choose fonts easily | |
| ttkbootstrap Color Picker |
from ttkbootstrap.dialogs.colorchooser import ColorChooserDialog cd = ColorChooserDialog(title="Choose") cd.show() print(cd.result) Pick HEX color |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.