Introduction to Biostatistical Computing PHC 6937

Python graphical user interface via TKinter

Zhiguang Huo (Caleb)

Wednesday Nov 2nd, 2022

Outline

GUI

Basics about Tkinter

from tkinter import *

window = Tk()
window.title("My first GUI progrom")
window.minsize(width=500, height=800)

window.mainloop()

label

# Label
my_label = Label(text="I am a Label", font = ("Arial", 24, "bold"))
my_label.pack()
# my_label.pack(side = "left")
# my_label.pack(side = "bottom")
# my_label.pack(expand = True)

my_label["text"] = "New Text"
my_label.config(text="New Text")

entry

input = Entry(width = 10)
input.insert(END, string="Some text to begin with.")
input.pack()
print(input.get())

button

def button_clicked():
    new_text = input.get()
    my_label.config(text=new_text)

button = Button(text = "Click Me",command = button_clicked)
button.pack()

text

#Text
text = Text(height=5, width=30)
#Puts cursor in textbox.
text.focus()
#Adds some text to begin with.

text.insert(END, "Example of multi-line text entry.")
#Get's current value in textbox at line 1, character 0
print(text.get("1.0", END))
text.pack()

Spinbox (optional)

def spinbox_used():
    #gets the current value in spinbox.
    print(spinbox.get())
spinbox = Spinbox(from_=0, to=10, width=5, command=spinbox_used)
spinbox.pack()

Scale (optional)

def scale_used(value):
    print(value)
scale = Scale(from_=0, to=100, command=scale_used)
scale.pack()

Checkbutton (optional)

def checkbutton_used():
    #Prints 1 if On button checked, otherwise 0.
    print(checked_state.get())
#variable to hold on to checked state, 0 is off, 1 is on.
checked_state = IntVar()
checkbutton = Checkbutton(text="Is On?", variable=checked_state, command=checkbutton_used)
checked_state.get()
checkbutton.pack()

Radiobutton (optional)

def radio_used():
    print(radio_state.get())
#Variable to hold on to which radio button value is checked.
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Option1", value=1, variable=radio_state, command=radio_used)
radiobutton2 = Radiobutton(text="Option2", value=2, variable=radio_state, command=radio_used)
radiobutton1.pack()
radiobutton2.pack()

Listbox (optional)

def listbox_used(event):
    # Gets current selection from listbox
    print(listbox.get(listbox.curselection()))

listbox = Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits:
    listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
listbox.pack()

layout

place

from tkinter import *

window = Tk()
window.title("My first GUI progrom")
window.minsize(width=500, height=800)
window.config(padx=100, pady = 100)

# Label
my_label = Label(text="I am a Label", font = ("Arial", 24, "bold"))
my_label.pack()
my_label.config(padx=100, pady = 100)

my_label["text"] = "New Text"
my_label.place(x=0,y=0)

grid

grid

from tkinter import *

window = Tk()
window.title("My first GUI progrom")
window.minsize(width=500, height=800)
window.config(padx=100, pady = 100)

# Label
my_label = Label(text="I am a Label", font = ("Arial", 24, "bold"))
# my_label.pack()
my_label.config(padx=100, pady = 100)

my_label["text"] = "New Text"
my_label.grid(column=0, row=0)

grid

def button_clicked():
    new_text = input.get()
    my_label.config(text=new_text)

button = Button(text = "Click Me",command = button_clicked)
# button.pack()
button.grid(column=1, row=1)
input = Entry(width = 10)
input.insert(END, string="Some text to begin with.")
# input.pack()
input.grid(column=2, row=2)

print(input.get())

Temperature converter

\[(F − 32) × 5/9 = C\]

\[(32°F − 32) × 5/9 = 0°C\]

Reference