tkinter samples

Labels & Button

Tkinter can be used to create a GUI with Python. The example below is a little example. This was tested with Python 3.6.9 but any Python version 3.x should do.

The GUI shows a colored label and a button. If you click on the button the label changes, its set to a StringVar.

Related course: Python Desktop Apps with Tkinter

import tkinter as tk 

window = tk.Tk() 
window.title('my window') 
window.geometry('300x150') 

var = tk.StringVar(window, 'OMG! this is tkinter') 
l = tk.Label(window,textvariable=var,bg='green',font=('Arial',12),width=15,height=2) 
l.pack()  

on_hit = False 
def hit_me(): 
    global on_hit 
    global var
    if on_hit == False: 
        var.set('you hit me') 
        on_hit = True 
    else: 
        var.set('tkinter')
        on_hit = False 

b = tk.Button(window,text='hit me',width=15,height=2,command=hit_me) 
b.pack() 
window.mainloop() 

Entry & Text input and Text box

Entry, text input and a text box in Python tkinter. You can click the buttons to interact with the text field.

import tkinter as tk

window = tk.Tk() 
window.title('my window') 
window.geometry('300x150') 
e = tk.Entry(window,show=None) 
show='#' 
e.pack() 

def insert_point(): 
    var = e.get() 
    t.insert('insert',var) 

def insert_end(): 
    var = e.get() 
    t.insert('end',var) 

def insert_start(): 
    var = e.get() 
    t.insert(1.0,var) 

b1 = tk.Button(window,text='insert point',width=15,height=2,command=insert_point) 
b1.pack() 
b2 = tk.Button(window,text = 'Insert end',command = insert_end) 
b2.pack() 
b3 = tk.Button(window,text = 'Insert start',command=insert_start) 
b3.pack() 
t = tk.Text(window,height=2) 
t.pack() 
window.mainloop() 

tkinter text input

tkinter listbox

A listbox example in tkinter. You can select any of the options and click on the button to output the result.

import tkinter as tk

window = tk.Tk() 
window.title('my window') 
window.geometry('300x150') 
var1 = tk.StringVar() 
l = tk.Label(window,bg='yellow',width=10,textvariable=var1) 
l.pack() 

def print_selection(): 
    value = lb.get(lb.curselection()) 
    var1.set(value) 

b1 = tk.Button(window,text='print selection',width= 15,height = 2,command = print_selection) 
b1.pack() 
var2 = tk.StringVar() 
var2.set((11,22,33,44))
lb = tk.Listbox (window,listvariable=var2) 
list_items = [1,2,3,4] 
for item in list_items: 
    lb.insert('end',item) 

lb.insert(1,'first') 
lb.insert(2,'second') 
lb.delete(2) 
lb.pack() 
mwindow.mainloop()

python tkinter listbox

tkinter radiobutton

A tkinter radiobutton examlpe gui. Chose any of the options.

import tkinter as tk 

window = tk.Tk() 
window.title('my window') 
window.geometry('300x150') 

var = tk.StringVar() 
l = tk.Label(window,bg='yellow',width=20,text='empty') 
l.pack() 

def print_selection(): 
    l.config(text='you have selected'+var.get()) 

r1 = tk.Radiobutton(window,text='Option A',variable=var,value='A',command=print_selection) 
r1.pack() 
r2 = tk.Radiobutton(window,text='Option B',variable=var,value='B',command=print_selection) 
r2.pack() 
r3 = tk.Radiobutton(window,text='Option C',variable=var,value='C',command=print_selection) 
r3.pack() 

window.mainloop() 

tkinter radiobutton

Related course: Python Desktop Apps with Tkinter