Creating a Window
import tkinter as tk
win = tk.Tk()
win.title('A Window')
win.mainloop()
Disable Window Resizing
win.resizable(0, 0)
Adding a Label
from tkinter import ttk
ttk.Label(win, text = 'A Label').grid(column = 0, row = 0)
Alternative Method:
myLabel = ttk.Label(win, text = 'A Label')
myLabel.grid(column = 0, row = 0)
Adding a Button
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title('A Window')
def clickMe():
btn.configure(text = 'Clicked')
myLabel.configure(foreground = 'red')
myLabel = ttk.Label(win, text = 'A Label')
myLabel.grid(column = 0, row = 0)
btn = ttk.Button(win, text = 'Click Me', command = clickMe)
btn.grid(column = 1, row = 0)
win.mainloop()
Adding a Textbox
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title('A Window')
def clickMe():
btn.configure(text = 'Hello ' + name.get())
ttk.Label(win, text = 'Enter a name: ').grid(column = 0, row = 0)
name = tk.StringVar()
txtBox = ttk.Entry(win, width = 12, textvariable = name)
txtBox.grid(column = 0, row = 1)
btn = ttk.Button(win, text = 'Click Me', command = clickMe)
btn.grid(column = 1, row = 1)
win.mainloop()
To set the Focus on a Widget
txtBox.focus()
To Disable a Widget
btn.configure(state = 'disabled')
Adding a Combo Box
city = tk.StringVar()
cBox = ttk.Combobox(win, width = 12, textvariable = city)
cBox['values'] = ('Mumbai', 'Kolkata', 'Bangalore', 'Bhubaneshwar')
cBox.grid(column = 1, row = 1)
cBox.current(0)
Adding Checkboxes
var1 = tk.IntVar()
c1 = tk.Checkbutton(win, text = 'Disabled', variable = var1, state = 'disabled')
c1.select()
c1.grid(column = 0, row = 4, sticky = tk.W)
var2 = tk.IntVar()
c2 = tk.Checkbutton(win, text = 'Unchecked', variable = var2)
c2.deselect()
c2.grid(column = 1, row = 4, sticky = tk.W)
var3 = tk.IntVar()
c3 = tk.Checkbutton(win, text = 'Enabled', variable = var3)
c3.select()
c3.grid(column = 2, row = 4, sticky = tk.W)
Adding Radio Buttons
def radCall():
radSel = radVar.get()
if radSel == 1:
win.configure(background = 'blue')
elif radSel == 2:
win.configure(background = 'gold')
elif radSel == 3:
win.configure(background = 'red')
radVar = tk.IntVar()
r1 = tk.Radiobutton(win, text = 'Blue', variable = radVar, value = 1, command = radCall)
r1.grid(column = 0, row = 0, sticky = tk.W)
r2 = tk.Radiobutton(win, text = 'Gold', variable = radVar, value = 2, command = radCall)
r2.grid(column = 0, row = 1, sticky = tk.W)
r3 = tk.Radiobutton(win, text = 'Red', variable = radVar, value = 3, command = radCall)
r3.grid(column = 0, row = 2, sticky = tk.W)
Adding a Scrolled Text Widget
from tkinter import scrolledtext
win = tk.Tk()
win.title('A Window')
scrolW = 30
scrolH = 3
scr = scrolledtext.ScrolledText(win, width = scrolW, height = scrolH, wrap = tk.WORD)
#the default wrap value is tk.CHAR
scr.grid(column = 0, columnspan = 3)
Using Loops to Add Several Widgets
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title('A Window')
colors = ['Blue', 'Gold', 'Red']
def radCall():
radSel = radVar.get()
if radSel == 0:
win.configure(background = colors[0])
elif radSel == 1:
win.configure(background = colors[1])
elif radSel == 2:
win.configure(background = colors[2])
radVar = tk.IntVar()
radVar.set(99)
for row in range(3):
r = 'rad' + str(row)
r = tk.Radiobutton(win, text = colors[row], variable = radVar, value = row, command = radCall)
r.grid(column = 0, row = row, sticky = tk.W)
win.mainloop()