GUI interfaces with TKinter
In [1]:
import tkinter as tk
In [2]:
# Basic loop
# root object
root = tk.Tk()
# simple text
label1 = tk.Label(root, text='hello TK')
# pack
label1.pack()
# main loopof a GUI Program
root.mainloop()
In [3]:
# grid
root = tk.Tk()
label1 = tk.Label(root, text='hello TK')
label2 = tk.Label(root, text='My name is Fernando')
label1.grid(row=0, column = 0)
label2.grid(row=1, column = 2)
root.mainloop()
In [4]:
# Button
# Button action : Function
def clickButton():
my_label = tk.Label(root, text='I clicked a Button')
my_label.pack()
# main window
root = tk.Tk()
#creating widgets
myButton = tk.Button(root, text='Click me!',padx=10, pady =2, command = clickButton).pack()
#main loop
root.mainloop()
In [5]:
# text Entry Widget
# Button action : Function
def clickButton():
my_label = tk.Label(root, text=e.get(),fg ='teal')
my_label.pack()
# main window
root = tk.Tk()
e = tk.Entry(root, width=50, bg='white',fg='black')
e.insert(0, 'Enter your name')
e.pack()
#creating widgets
myButton = tk.Button(root, text='Click me!',padx=10, pady =2, command = clickButton).pack()
#main loop
root.mainloop()
In [6]:
# Calculator
# main window
root = tk.Tk()
root.title('Simple Calculator')
# Button action : Function
def type_number(number):
e.insert(tk.END, number)
# Button action : Function
def clear_display():
e.delete(0, tk.END)
def calc_func():
global num_aux
result = num_aux + int(e.get())
num_aux = None
e.delete(0,tk.END)
e.insert(0,result)
def button_plus():
global num_aux
num_aux = int(e.get())
e.delete(0, tk.END)
# Entry
e = tk.Entry(root, width=35, borderwidth =5)
e.grid(row = 0, column=0, columnspan =3, padx =5, pady=2 )
button_1 = tk.Button(root, text = '1', padx=40, pady=20, command=lambda : type_number(1))
button_2 = tk.Button(root, text = '2', padx=40, pady=20, command=lambda : type_number(2))
button_3 = tk.Button(root, text = '3', padx=40, pady=20, command=lambda : type_number(3))
button_4 = tk.Button(root, text = '4', padx=40, pady=20, command=lambda : type_number(4))
button_5 = tk.Button(root, text = '5', padx=40, pady=20, command=lambda : type_number(5))
button_6 = tk.Button(root, text = '6', padx=40, pady=20, command=lambda : type_number(6))
button_7 = tk.Button(root, text = '7', padx=40, pady=20, command=lambda : type_number(7))
button_8 = tk.Button(root, text = '8', padx=40, pady=20, command=lambda : type_number(8))
button_9 = tk.Button(root, text = '9', padx=40, pady=20, command=lambda : type_number(9))
button_0 = tk.Button(root, text = '0', padx=40, pady=20, command=lambda : type_number(0))
button_add = tk.Button(root, text = '+', padx=39, pady=20, command= button_plus)
button_clear = tk.Button(root, text = 'clear', padx=78, pady=20, command=clear_display)
button_eq = tk.Button(root, text = '=', padx=87, pady=20, command= calc_func)
button_1.grid(row = 1, column= 0)
button_2.grid(row = 1, column= 1)
button_3.grid(row = 1, column= 2)
button_4.grid(row = 2, column= 0)
button_5.grid(row = 2, column= 1)
button_6.grid(row = 2, column= 2)
button_7.grid(row = 3, column= 0)
button_8.grid(row = 3, column= 1)
button_9.grid(row = 3, column= 2)
button_0.grid(row = 4, column= 0)
button_clear.grid(row = 4, column= 1, columnspan=2)
button_add.grid(row = 5, column= 0)
button_eq.grid(row = 5, column= 1, columnspan=2)
#main loop
root.mainloop()
In [30]:
# icon and images
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
root.title('Image viewer')
root.iconbitmap('./img/rulers.ico')
test_img = Image.open("./img/eat.jpeg")
img_size = test_img.size
n=4
test_img = test_img.resize((img_size[0]//n,img_size[1]//n ))
test_img = ImageTk.PhotoImage(test_img)
label1 = tk.Label( image=test_img)
label1.pack()
button_exit = tk.Button(root, text='Quit', command=root.destroy)
button_exit.pack()
root.mainloop()
In [28]:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.resizable(False,False)
wrap_1 = tk.LabelFrame(root)
wrap_2 = tk.LabelFrame(root)
canv_1 = tk.Canvas(wrap_1)
canv_1.pack(side=tk.LEFT)
yscroll_root = ttk.Scrollbar(root, orient='vertical', command= canv_1.yview)
yscroll_root.pack(side=tk.RIGHT, fill='y')
yscroll = ttk.Scrollbar(wrap_1, orient='vertical', command= canv_1.yview)
yscroll.pack(side=tk.RIGHT, fill='y')
wrap_1.pack(fill='both',expand='yes' ,padx=10, pady=10)
wrap_2.pack(fill='both',expand='yes' ,padx=10, pady=10)
for i in range(20):
Button_1 = tk.Button(canv_1, text=i ,padx=50, pady=30)
Button_1.pack()
root.mainloop()