1.1. Text Normal
sintax :
FROM TKINTER IMPORT *
ROOT = TK()
W = LABEL(ROOT, TEXT="HELLO TKINTER!")
W.PACK()
ROOT.MAINLOOP()
Tampilan
1.2. teks dengan berbagai jenis font
sintax :
from tkinter import *
root = Tk()
Label(root,
text="Teks ini menggunakan jenis font 'Times' ",
font = "Times").pack()
Label(root,
text="Teks ini menggunakan font 'Helvetica' dengan style 'italic' ukuran 16",
font = "Helvetica 16 bold italic").pack()
Label(root,
text="Teks ini menggunakan font 'Verdana' dengan ukuran '10' dan style 'bold' ",
font = "Verdana 10 bold").pack()
root.mainloop()
tampilan
1.3. teks agar berwarna
sintax
from tkinter import *
root = Tk()
Label(root,
text="Teks ini menggunakan jenis font 'Times' ",
fg = "red",
font = "Times").pack()
Label(root,
text="Teks ini menggunakan font 'Helvetica' dengan style 'italic' ukuran 16",
fg = "dark green",
font = "Helvetica 16 bold italic").pack()
Label(root,
text="Teks ini menggunakan font 'Verdana' dengan ukuran '10' dan style 'bold' ",
fg = "blue",
font = "Verdana 10 bold").pack()
root.mainloop()
tampilan
1.4. memberikan background warna pada teks
sintax :
from tkinter import *
root = Tk()
Label(root,
text="Teks ini menggunakan jenis font 'Times' ",
fg = "red",
bg = "yellow",
font = "Times").pack()
Label(root,
text="Teks ini menggunakan font 'Helvetica' dengan style 'italic' ukuran 16",
fg = "dark green",
bg = "light green",
font = "Helvetica 16 bold italic").pack()
Label(root,
text="Teks ini menggunakan font 'Verdana' dengan ukuran '10' dan style 'bold' ",
fg = "blue",
bg = "light blue",
font = "Verdana 10 bold").pack()
root.mainloop()
tampilan
2. Membuat Tombol.
sintax :
from tkinter import Tk, BOTH
from tkinter.ttk import Frame, Button
class membuatTombol(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.tombol()
self.parent.title("membuat tombol")
self.pack(fill=BOTH, expand=1)
def tombol(self):
variabelTombol = Button(self, text="ini tombol")
variabelTombol.place(x=50, y=50)
if __name__ == '__main__':
root = Tk()
root.geometry("250x150+300+300")
app = membuatTombol(root)
root.mainloop()
TAMPILAN
menambahkan Action atau perintah exit pada tombol.
sintax :
from tkinter import Tk, BOTH
from tkinter.ttk import Frame, Button
class membuatTombol(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.tombol()
self.parent.title("membuat tombol")
self.pack(fill=BOTH, expand=1)
def tombol(self):
variabelTombol = Button(self, text="exit",command=self.quit)
variabelTombol.place(x=50, y=50)
if __name__ == '__main__':
root = Tk()
root.geometry("250x150+300+300")
app = membuatTombol(root)
root.mainloop()
3. Membuat CheckBox.
sintax :
from tkinter import Tk, Frame, Checkbutton, BOTH
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.buatCheckButton()
def initUI(self):
self.parent.title("Checkbutton")
self.pack(fill=BOTH, expand=True)
self.parent.geometry("250x150")
def buatCheckButton(self):
cb = Checkbutton(self, text="ini checkbox 1")
cb.place(x=50, y=50)
cb = Checkbutton(self, text="ini checkbox 2")
cb.place(x=50, y=70)
cb = Checkbutton(self, text="ini checkbox 3")
cb.place(x=50, y=90)
cb = Checkbutton(self, text="ini checkbox 4")
cb.place(x=50, y=110)
if __name__ == '__main__':
root = Tk()
app = Example(root)
root.mainloop()
TAMPILAN
memberikan perintah pada check box
sintax:
from tkinter import Tk, Frame, Checkbutton
from tkinter import BooleanVar, BOTH
class membuatCheckbox(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.pembuatanCheckbox()
def initUI(self):
self.parent.title("lihat disini..")
self.parent.geometry("250x150")
self.pack(fill=BOTH, expand=True)
self.penentu = BooleanVar()
def pembuatanCheckbox(self):
checkBox = Checkbutton(self, text="judul", variable=self.penentu, command=self.diKlik)
checkBox.select()
checkBox.place(x=50, y=50)
def diKlik(self):
if self.penentu.get() == True:
self.master.title("lihat disini..")
else:
self.master.title("")
if __name__ == '__main__':
root = Tk()
app = membuatCheckbox(root)
root.mainloop()
tampilan
4. Membuat ListBox.
sintax :
from tkinter.ttk import Frame, Label
from tkinter import Tk, BOTH, Listbox, StringVar, END
class membuatListbox(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.buatListbox()
def initUI(self):
self.window.title("Listbox")
self.window.geometry("300x250")
self.pack(fill=BOTH, expand=1)
def buatListbox(self):
daftarNama = ['Moham', 'Moh. nikmat',
'nama ke 3', 'nama ke 4', 'Dan seterusnya..']
listBox = Listbox(self)
for i in daftarNama:
listBox.insert(END, i)
listBox.bind("<>", self.onSelect)
listBox.pack(pady=15)
self.nilai = StringVar()
self.teks = Label(self, text=0, textvariable=self.nilai)
self.teks.pack()
def onSelect(self, val):
sender = val.widget
idx = sender.curselection()
nilaiPerubahan = sender.get(idx)
self.nilai.set(nilaiPerubahan)
if __name__ == '__main__':
root = Tk()
ex = membuatListbox(root)
root.mainloop()
TAMPILAN
5. Membuat Radio Button.
sintax :
from tkinter import *
class RadioButton(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.v = IntVar()
self.teks()
self.radioButton()
def teks(self):
Label(root, text="""pilih bahasa yang kalian sukai :""", justify=LEFT, padx=20).pack()
def radioButton(self):
Radiobutton(root, text="Indonesia", padx=20, command=self.tampilkan, variable=self.v, value=1).pack(anchor=W)
Radiobutton(root, text="Bahasa Inggris", padx=20, variable=self.v, command=self.tampilkan, value=2).pack(anchor=W)
def tampilkan(self):
if self.v.get() == 1:
print ("Bahasa Indonesia")
elif self.v.get() == 2 :
print ("Bahasa inggris")
if __name__ == '__main__':
root = Tk()
RadioButton(root)
mainloop()
TAMPILAN
memodifikasi tampilan dari Radio Button
sintax :
from tkinter import *
class RadioButton (Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.v = IntVar()
self.teks()
self.radioButton()
def teks(self):
Label(root, text="""pilih bahasa yang kalian sukai :""", justify=LEFT, padx=20).pack()
def radioButton(self):
Radiobutton(root, text="Indonesia", padx=20, command=self.ShowChoice, indicatoron=0, width=25, variable=self.v, value=1).pack(anchor=W)
Radiobutton(root, text="Bahasa Inggris", padx=20, variable=self.v, command=self.ShowChoice, indicatoron=0, width=25, value=2).pack(anchor=W)
def ShowChoice(self):
if self.v.get() == 1:
print ("Bahasa Indonesia")
elif self.v.get() == 2 :
print ("Bahasa inggris")
if __name__ == '__main__':
root = Tk()
RadioButton(root)
mainloop()
tampilan
6. Membuat Scale atau slider.
sintax :
from tkinter.ttk import Frame, Label, Scale
from tkinter import Tk, BOTH, IntVar, LEFT
class pembuatanScale(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.membuatScale()
self.membuatTeks()
def initUI(self):
self.window.title("Scale")
self.window.geometry("250x100")
self.pack(fill=BOTH, expand=1)
def membuatScale(self):
scale = Scale(self, from_=0, to=100, command=self.onScale)
scale.pack(side=LEFT, padx=15)
def membuatTeks(self):
self.angka = IntVar()
self.teks = Label(self, text=0, textvariable=self.angka)
self.teks.pack(side=LEFT)
def onScale(self, nilai):
v = int(float(nilai))
self.angka.set(v)
if __name__ == '__main__':
root = Tk()
ex = pembuatanScale(root)
root.mainloop()
TAMPILAN
7. Membuat Kolom Teks.
sintax :
from tkinter import *
class MembuatKolomTeks(Frame) :
def __init__(self) :
self.buatTeks()
self.buatKolom()
def buatTeks(self):
Label(text="First Name").grid(row=0)
Label(text="Last Name").grid(row=1)
def buatKolom(self):
Entry().grid(row=0, column=1)
Entry().grid(row=1, column=1)
master = Tk()
MembuatKolomTeks()
mainloop()
TAMPILAN
membuat perintah pada code program kolom teks
sintax :from tkinter import *
class MembuatKolomTeks(Frame) :
def __init__(self, master) :
self.buatTeks()
self.buatKolom(master)
self.buatTombol()
def buatTeks(self):
Label(text="First Name").grid(row=0)
Label(text="Last Name").grid(row=1)
def buatKolom(self, master):
self.kolom1 = Entry(master)
self.kolom2 = Entry(master)
self.kolom1.grid(row=0, column=1)
self.kolom2.grid(row=1, column=1)
def buatTombol(self):
Button(text='Show', command=self.perintah).grid(row=3, column=1)
def perintah(self):
print("First Name: %s \nLast Name: %s" % (self.kolom1.get(), self.kolom2.get()))
master = Tk()
MembuatKolomTeks(master)
mainloop()
tampilan
menambahkan teks secara otomatis pada kolom teks
sintax :from tkinter import *
class MembuatKolomTeks(Frame) :
def __init__(self) :
self.buatTeks()
self.buatKolom()
self.buatTombol()
def buatTeks(self):
Label(text="First Name").grid(row=0)
Label(text="Last Name").grid(row=1)
def buatKolom(self):
self.kolom1 = Entry()
self.kolom2 = Entry()
self.kolom1.insert(10, "masukkan nama disini")
self.kolom2.insert(10, "nama akhirnya disini")
self.kolom1.grid(row=0, column=1)
self.kolom2.grid(row=1, column=1)
def buatTombol(self):
Button(text='Show', command=self.perintah).grid(row=3, column=1)
def perintah(self):
print("First Name: %s \nLast Name: %s" % (self.kolom1.get(), self.kolom2.get()))
master = Tk()
MembuatKolomTeks()
mainloop()
tampilan
8. Membuat Text field.
sintax :
from tkinter import Tk, BOTH, X
from tkinter.ttk import Frame, Entry
class membuatLabel(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.teksEdit()
def initUI(self):
self.pack(fill=BOTH)
root.geometry("300x100+300+300")
root.title("Text field/kolom teks")
def teksEdit(self):
teksField = Frame(self)
teksField.pack()
masukKeWindow = Entry(teksField)
masukKeWindow.pack()
if __name__ == '__main__':
root = Tk()
app = membuatLabel(root)
root.mainloop()
TAMPILAN
menyandingkan text field dengan sebuah teks atau label
sintax:
from tkinter import Tk, Text, TOP, BOTH, X, N, LEFT
from tkinter.ttk import Frame, Label, Entry
class membuatLabel(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.teksEdit()
def initUI(self):
self.pack(fill=BOTH)
root.geometry("300x100+300+300")
root.title("Text field/kolom teks")
def teksEdit(self):
teksField = Frame(self)
teksField.pack(fill=X, padx=10)
teks = Label(teksField, text="Judul :", width=6)
teks.pack(side=LEFT, padx=5, pady=5)
masukKeWindow = Entry(teksField)
masukKeWindow.pack(fill=X)
if __name__ == '__main__':
root = Tk()
app = membuatLabel(root)
root.mainloop()
tampilan
membuat kolom komentar sederhana
sintax:
from tkinter import Tk, Text, TOP, BOTH, X, N, LEFT, RIGHT, RAISED
from tkinter.ttk import Frame, Label, Entry, Button
class membuatKomentar(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("komentar")
self.pack(fill=BOTH, expand=True)
self.elemen1()
self.elemen2()
self.elemen3()
self.tombol()
def elemen1(self) :
teksField1 = Frame(self)
teksField1.pack(fill=X)
teks1 = Label(teksField1, text="judul :", width=6)
teks1.pack(side=LEFT, padx=5, pady=5)
masukkanKeWindow1 = Entry(teksField1)
masukkanKeWindow1.pack(fill=X, padx=5, expand=True)
def elemen2(self):
teksField2 = Frame(self)
teksField2.pack(fill=X)
teks2 = Label(teksField2, text="jenis :", width=6)
teks2.pack(side=LEFT, padx=5, pady=5)
masukkanKeWindow2 = Entry(teksField2)
masukkanKeWindow2.pack(fill=X, padx=5, expand=True)
def elemen3(self):
teksField3 = Frame(self)
teksField3.pack(fill=BOTH, expand=True)
teks3 = Label(teksField3, text="ulasan :", width=6)
teks3.pack(side=LEFT, anchor=N, padx=5, pady=5)
masukkanKeWindow2 = Text(teksField3)
masukkanKeWindow2.pack(fill=BOTH, pady=5, padx=5, expand=True)
def tombol(self):
tombolTutup = Button(self, text="Tutup", command=self.quit)
tombolTutup.pack(side=RIGHT, padx=5, pady=5)
tombolOke = Button(self, text="Oke", command=self.quit)
tombolOke.pack(side=RIGHT)
if __name__ == '__main__':
root = Tk()
root.geometry("300x450")
app = membuatKomentar(root)
root.mainloop()
tampilan
sintax :
from tkinter import *
class BuatKolomTeks():
def __init__(self):
self.KolomTeks()
def KolomTeks(self):
T = Text(height=7, width=30)
T.pack()
T.insert(END, "nah ini dia tampilan \ndari kolom teks"
"\ndengan beberapa baris. \nbisa kita lihat"
"\nsama - sama ya temen - temen...\n")
window = Tk()
window.geometry("300x150")
BuatKolomTeks()
mainloop()
TAMPILAN
mengkonfigurasikan kolom teks dan scrollbar
sintax :
from tkinter import *
class BuatKolomTeks():
def __init__(self):
self.KolomTeks()
self.scrollBar()
def KolomTeks(self):
self.T = Text(height=5, width=30)
self.T.pack()
self.T.insert(END, "nah ini dia tampilan \ndari kolom teks"
"\ndengan beberapa baris.\n\n\nbisa kita lihat"
"\nsama - sama ya temen - temen...\ntes\ntes\ntes\ntes\ntes\ntes\n")
def scrollBar(self):
S = Scrollbar()
S.pack(side=RIGHT, fill=Y)
self.T.pack(side=LEFT, fill=Y)
S.config(command=self.T.yview)
self.T.config(yscrollcommand=S.set)
window = Tk()
window.geometry("230x150")
BuatKolomTeks()
mainloop()
tampilan
10. Membuat Menubar.
sintax :
from tkinter import Tk, Frame, Menu
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.membuatMenu()
def initUI(self):
self.window.title("Membuat menu bar")
self.window.geometry("250x150")
def membuatMenu(self):
menubar = Menu(self.window)
self.window.config(menu=menubar)
fileMenu = Menu(menubar)
fileMenu.add_command(label="Exit", command=self.perintahKeluar)
menubar.add_cascade(label="File", menu=fileMenu)
def perintahKeluar(self):
self.quit()
if __name__ == '__main__':
root = Tk()
app = Example(root)
root.mainloop()
TAMPILAN
11. Membuat Submenu di menu bar.
sintax :
from tkinter import Tk, Frame, Menu
class membuatSubMenu(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.membuatMenu()
def initUI(self):
self.window.title("Submenu")
self.window.geometry("250x150")
def membuatMenu(self):
menubar = Menu(self.window)
self.window.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="New feed")
submenu.add_command(label="Bookmarks")
submenu.add_command(label="Mail")
fileMenu.add_cascade(label='Import', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
def onExit(self):
self.quit()
if __name__ == '__main__':
root = Tk()
app = membuatSubMenu(root)
root.mainloop()
TAMPILAN
12. Membuat toolbar di bawah menubar.
sintax :
from tkinter import Tk, Frame, Menu, Button, LEFT, TOP, X, FLAT, RAISED
class membuatToolbar(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Toolbar")
self.window.geometry("250x150")
self.buatToolBar()
self.konfigurasiToolbardanMenuBar()
def buatToolBar(self):
self.toolbar = Frame(self.window, bd=1, relief=RAISED)
exitButton = Button(self.toolbar, text="exit", relief=FLAT,command=self.quit)
exitButton.pack(side=LEFT, padx=2, pady=2)
def konfigurasiToolbardanMenuBar(self):
self.toolbar.pack(side=TOP, fill=X)
self.pack()
def onExit(self):
self.quit()
if __name__ == '__main__':
root = Tk()
app = membuatToolbar(root)
root.mainloop()
TAMPILAN
menggabungkan menubar dan toolbar
sintax :
from tkinter import Tk, Frame, Menu, Button, LEFT, TOP, X, FLAT, RAISED
class membuatToolbar(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Toolbar")
self.window.geometry("250x150")
self.buatMenuBar()
self.buatToolBar()
self.konfigurasiToolbardanMenuBar()
def buatMenuBar(self):
self.menubar = Menu(self.window)
self.fileMenu = Menu(self.window, tearoff=0)
self.fileMenu.add_command(label="Exit", command=self.onExit)
self.menubar.add_cascade(label="File", menu=self.fileMenu)
def buatToolBar(self):
self.toolbar = Frame(self.window, bd=1, relief=RAISED)
exitButton = Button(self.toolbar, text="exit", relief=FLAT,command=self.quit)
exitButton.pack(side=LEFT, padx=2, pady=2)
def konfigurasiToolbardanMenuBar(self):
self.toolbar.pack(side=TOP, fill=X)
self.window.config(menu=self.menubar)
self.pack()
def onExit(self):
self.quit()
if __name__ == '__main__':
root = Tk()
app = membuatToolbar(root)
root.mainloop()
tampilan
13. Menampilkan PopUp menu saat klik kanan.
sintax :
from tkinter import Tk, Frame, Menu
class menuKlikKanan(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
self.buatMenu()
def initUI(self):
self.window.title("klik kanan di window")
self.window.geometry("250x150")
def buatMenu(self):
self.menu = Menu(self.window, tearoff=0)
self.menu.add_command(label="Beep", command=self.bell)
self.menu.add_command(label="Exit", command=self.perintah)
self.window.bind("", self.tampilkanMenu)
self.pack()
def tampilkanMenu(self, e):
self.menu.post(e.x_root, e.y_root)
def perintah(self):
self.quit()
if __name__ == '__main__':
root = Tk()
app = menuKlikKanan(root)
root.mainloop()
TAMPILAN