sintax :
from tkinter import *
root = Tk()
gambar = PhotoImage(file="/home/moham/moham.png")
w1 = Label(root, image=gambar).pack(side="right")
root.mainloop()
tampilan;
menampilkan teks di tengah gambar
sintax :
from tkinter import *
root = Tk()
gambar = PhotoImage(file="/home/moham/moham.png")
teks = """hey hey... arahkan pandangan kalia ke sini ya...
gimana ? ini teks nya temen - temen..
sudah berada di tengah ya....
berati program yang saya bagikan ini bisa kita katakan sukses ya..
makanya simak terus tutorial - tutorial lainnya
di mn-belajarpython.blogspot.co.id ini ya.. hehe
"""
w = Label(root, fg = "white",
compound = CENTER,
text=teks,
image=gambar).pack(side="right")
root.mainloop()
tampilan :
2. Membuat garis di window tkinter.
sintax :
from tkinter import Tk, Canvas, Frame, BOTH
class MembuatGaris(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Lines")
self.pack(fill=BOTH, expand=1)
self.window.geometry("400x250")
self.buatGaris()
def buatGaris(self):
kanvas = Canvas(self)
kanvas.create_line(15, 25, 200, 25)
kanvas.pack(fill=BOTH, expand=1)
if __name__ == '__main__':
root = Tk()
ex = MembuatGaris(root)
root.mainloop()
tampilan;
membuat garis putus - putus
sintax :
from tkinter import Tk, Canvas, Frame, BOTH
class MembuatGaris(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Lines")
self.pack(fill=BOTH, expand=1)
self.window.geometry("400x250")
self.buatGaris()
def buatGaris(self):
kanvas = Canvas(self)
kanvas.create_line(300, 35, 300, 200, dash=(4, 2))
kanvas.pack(fill=BOTH, expand=1)
if __name__ == '__main__':
root = Tk()
ex = MembuatGaris(root)
root.mainloop()
tampilan :
3. Membuat Bintang di tkinter.
sintax :
from tkinter import *
class MembuatBintang():
def __init__(self):
self.buatBintang()
def buatBintang(self):
kanvas = Canvas(width=200, height=200)
kanvas.pack()
koordinat = [100, 140, 110, 110, 140, 100, 110, 90, 100, 60, 90, 90, 60, 100, 90, 110]
kanvas.create_polygon(koordinat, outline='green', fill='yellow', width=3)
Tk()
MembuatBintang()
mainloop()
tampilan;
4. Membuat lingkaran, oval, kotak dan lain - lain.
sintax :
from tkinter import Tk, Canvas, Frame, BOTH
class MembuatBerbagaiBentuk(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Shapes")
self.pack(fill=BOTH, expand=1)
self.window.geometry("330x220+300+300")
self.buatBentuk()
def buatBentuk(self):
canvas = Canvas(self)
canvas.create_oval(10, 10, 80, 80, outline="green", fill="blue", width=2)
canvas.create_oval(110, 10, 210, 80, outline="green", fill="blue", width=2)
canvas.create_rectangle(230, 10, 290, 60, outline="green", fill="blue", width=2)
canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="green", fill="blue", width=2)
points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200]
canvas.create_polygon(points, outline='green', fill='blue', width=2)
canvas.pack(fill=BOTH, expand=1)
if __name__ == '__main__':
root = Tk()
ex = MembuatBerbagaiBentuk(root)
root.mainloop()
tampilan;
5. Menampilkan 3 kotak warna.
sintax :
from tkinter import Tk, Canvas, Frame, BOTH
class MembuatWarna(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("Colours")
self.pack(fill=BOTH, expand=1)
self.window.geometry("400x100+300+300")
self.buatWarna()
def buatWarna(self):
kanvas = Canvas(self)
kanvas.create_rectangle(30, 10, 120, 80, outline="#f50", fill="#fb0")
kanvas.create_rectangle(150, 10, 240, 80, outline="#fb0", fill="#f50")
kanvas.create_rectangle(270, 10, 370, 80, outline="#0f5", fill="#05f")
kanvas.pack(fill=BOTH, expand=1)
if __name__ == '__main__':
root = Tk()
ex = MembuatWarna(root)
root.mainloop()
tampilan;
6. Membuat window tikinter seperti buku kotak.
sintax :
from tkinter import *
class BuatKotak():
def __init__(self):
self.buatCanvas()
def buatCanvas(self):
kanvas = Canvas(master, width=200, height=200)
kanvas.pack()
self.pembuatKotak(kanvas, 12)
def pembuatKotak(self, canvas, jarak):
for x in range(jarak,200,jarak):
canvas.create_line(x, 0, x, 200)
for y in range(jarak,200,jarak):
canvas.create_line(0, y, 200, y)
master = Tk()
BuatKotak()
mainloop()
tampilan;
7. Membuat window tkinter seperti terowongan.
sintax :
from tkinter import *
class Terowongan():
def __init__(self):
self.buatKanvas()
self.buatTerowongan()
def buatKanvas(self):
self.kanvas = Canvas(width=200, height=100)
self.kanvas.pack()
def buatTerowongan(self):
self.kanvas.create_rectangle(50, 20, 150, 80, fill="green")
self.kanvas.create_rectangle(65, 35, 135, 65, fill="yellow")
self.kanvas.create_line(0, 0, 50, 20, fill="green", width=3)
self.kanvas.create_line(0, 100, 50, 80, fill="green", width=3)
self.kanvas.create_line(150, 20, 200, 0, fill="green", width=3)
self.kanvas.create_line(150, 80, 200, 100, fill="green", width=3)
Tk()
Terowongan()
mainloop()
tampilan;
8. Membuat teks keren di window tkinter.
sintax :
from tkinter import Tk, Canvas, Frame, BOTH, W
class MembuatTeks(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.window = parent
self.initUI()
def initUI(self):
self.window.title("membuat tulisan keren")
self.pack(fill=BOTH, expand=1)
self.window.geometry("580x250")
self.buatTeks()
def buatTeks(self):
kanvas = Canvas(self)
kanvas.create_text(20, 30, anchor=W, font="Purisa", text="oke, ini dia temen - temen teks nya...")
kanvas.create_text(20, 60, anchor=W, font="Purisa", text="gimana ? ya cukup keren lah ya...")
kanvas.create_text(20, 130, anchor=W, font="Purisa", text="tentu lebih keren bila kita bandingkan dengan teks biasa pada")
kanvas.create_text(20, 160, anchor=W, font="Purisa", text="umumnya yang cendrung standard dan membosankan..")
kanvas.create_text(20, 190, anchor=W, font="Purisa", text="jangan lupa simak tutorial menarik lainnya ya..")
kanvas.create_text(20, 220, anchor=W, font="Purisa", text="tentu saja di mn-belajarpython.blogspot.co.id ini ya...")
kanvas.pack(fill=BOTH, expand=1)
if __name__ == '__main__':
root = Tk()
ex = MembuatTeks(root)
root.mainloop()
tampilan;
9. Menggambar dengan kursor di window tkinter.
sintax :
from tkinter import *
class Menggambar():
def __init__(self):
self.buatKanvas()
self.pesan()
def buatKanvas(self):
self.kanvas = Canvas(master, width=500, height=150)
self.kanvas.pack(expand=YES, fill=BOTH)
self.kanvas.bind("", self.gambar)
def pesan(self):
message = Label(master, text="klik dan seret untuk menggambar")
message.pack(side=BOTTOM)
def gambar(self,event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
self.kanvas.create_oval(x1, y1, x2, y2, fill="green")
master = Tk()
Menggambar()
mainloop()
tampilan;
10. Menampilkan Ikon bawaan tkinter.
sintax :
from tkinter import *
class MenampilkanIkon():
def __init__(self):
self.buatKanvas()
self.panggilIkon()
self.tampilIkon()
def buatKanvas(self):
self.kanvas = Canvas(master, width=300, height=80)
self.kanvas.pack()
def panggilIkon(self):
self.ikon = ["error", "gray75", "gray50", "gray25", "gray12", "hourglass", "info", "questhead", "question",
"warning"]
def tampilIkon(self):
jmlIkon = len(self.ikon)
posisi = int(300 / jmlIkon)
for i in range(0, jmlIkon):
self.kanvas.create_bitmap((i + 1) * posisi - posisi / 2, 50, bitmap=self.ikon[i])
master = Tk()
MenampilkanIkon()
mainloop()
tampilan;