1. Membuat perhitungan
sintax :
from tkinter import *
class MembuatKolomTeks(Frame) :
def __init__(self) :
self.buatTeks()
self.buatKolom()
self.buatTombol()
def buatTeks(self):
self.teks = Label(text="")
self.teks.grid(row = 1, column=0)
def buatKolom(self):
self.kolom1 = Entry()
self.kolom1.bind("", self.hitung)
self.kolom1.grid(row=0, column=0)
def buatTombol(self):
Button(text='cetak', command=self.hitungCetak).grid(row=0, column=1)
def hitung(self, event):
try :
self.teks.configure(text="hasil: " + str(eval(self.kolom1.get())))
except :
self.teks.configure(text="ada yang salah pada kolom")
def hitungCetak(self):
try :
print("hasil: " + str(eval(self.kolom1.get())))
except :
print("ada yang salah pada kolom")
master = Tk()
MembuatKolomTeks()
mainloop()
tampilan :
2. Membuat kalkulator sederhana
sintax :
from functools import partial
import tkinter as tk
class applikasiKalkulator(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Kalkulator tkinter")
self.membuatTombol()
self.penentu = False
def membuatTombol(self):
self.layar = tk.Entry(self, width=25)
self.layar.grid(row=0, column=0, columnspan=5)
btn_list = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'0', '+', '-',
'C', '/', '*',
'='
]
baris = 1
kolom = 0
for penampung in btn_list:
perintah = partial(self.hitung, penampung)
if penampung == '=':
tk.Button(self, text='=', width=22, command=perintah).grid(row=baris, column=kolom, columnspan=5)
else :
tk.Button(self, text=penampung, width=5, command=perintah).grid(row=baris, column=kolom)
kolom += 1
if kolom > 2:
kolom = 0
baris += 1
def hitung(self, key):
if key == '=':
self.penentu = True
try:
result = eval(self.layar.get())
self.layar.delete(0, tk.END)
self.layar.insert(tk.END, str(result))
except:
self.layar.insert(tk.END, "-> Error!")
elif key == 'C':
self.layar.delete(0, tk.END)
else:
if self.penentu :
self.layar.delete(0, tk.END)
self.penentu = False
self.layar.insert(tk.END, key)
panggil = applikasiKalkulator()
panggil.mainloop()
tampilan :
3. Membuat Stopwatch.
sintax :
import tkinter as tk
penghitung = 0
def hitung(teks):
def count():
global penghitung
penghitung += 1
teks.config(text=str(penghitung))
teks.after(1000, count)
count()
root = tk.Tk()
teks = tk.Label(root, font = "Verdana 16 bold")
teks.pack()
hitung(teks)
tombol = tk.Button(root, text='Stop', width=25, command=root.destroy)
tombol.pack()
root.mainloop()
tampilan :
mereset nilai ketika mencapai angka 60 pada penghitung detik
sintax :
import tkinter as tk
detik = 0
menit = 0
def hitung(teks1, teks2):
def count():
global detik
global menit
if detik >= 59 :
detik = 0
menit += 1
detik += 1
teks1.config(text="detik : "+str(detik))
teks2.config(text="menit : "+str(menit))
teks1.after(1000, count)
count()
root = tk.Tk()
teksDetik = tk.Label(root, font = "Verdana 16 bold")
teksMenit = tk.Label(root, font = "Verdana 16 bold")
teksDetik.pack()
teksMenit.pack()
hitung(teksDetik, teksMenit)
tombol = tk.Button(root, text='Stop', width=25, command=root.destroy)
tombol.pack()
root.mainloop()
tampilan :
4. Membuat Games Ular Simple
sintax :
"""
nilai keycode arah kanan, kiri, atas dan bawah pada keybord memiliki nilai yang berbeda
#untuk pengguna windows, silahkan ubah nilai pengecekan keycode nya seperti di bawa ini
if event.keycode == 38:
self.direction = "up"
elif event.keycode == 39:
self.direction = "right"
elif event.keycode == 40:
self.direction = "down"
elif event.keycode == 37:
self.direction = "left"
#untuk pengguna linux, silahkan ubah nilai pengecekan keycode nya seperti di bawa ini
if event.keycode == 111:
self.direction = "up"
elif event.keycode == 114:
self.direction = "right"
elif event.keycode == 116:
self.direction = "down"
elif event.keycode == 113:
self.direction = "left"
"""
import threading
import random
import os.path
from tkinter import *
WIDTH = 500
HEIGHT = 500
class Snake(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Permainan ular. mn-belajarpython.blogspot.com")
self.grid()
frame1 = Frame(self)
frame1.grid()
self.canvas = Canvas(frame1, width = WIDTH, height = HEIGHT, bg ="white")
self.canvas.grid(columnspan = 3)
self.canvas.focus_set()
self.canvas.bind("", self.create)
self.canvas.bind("", self.create)
newGame = Button(frame1, text = "Permainan Baru", command = self.new_game)
newGame.grid(row = 1, column = 0, sticky = E)
self.score_label = Label(frame1)
self.score_label.grid(row = 1, column = 1)
self.high_score_label = Label(frame1)
self.high_score_label.grid(row = 1, column = 2)
self.new_game()
def new_game(self):
self.canvas.delete(ALL)
self.canvas.create_text(WIDTH/2,HEIGHT/2-50,text="selamat datang di game ular ini!"\
+ "\nklik window atau tekan salah satu tombol arah"
+ "\nuntuk memulai game ini..", tag="welcome_text")
rectWidth = WIDTH/25
rect1 = self.canvas.create_rectangle(WIDTH/2-rectWidth/2, HEIGHT/2-rectWidth/2, WIDTH/2+rectWidth/2\
, HEIGHT/2+rectWidth/2, outline="#dbf", fill="#dbf"\
, tag="rect1")
rect2 = self.canvas.create_rectangle(WIDTH/2-rectWidth/2, HEIGHT/2-rectWidth/2, WIDTH/2+rectWidth/2\
, HEIGHT/2+rectWidth/2, outline="#dbf", fill="#dbf"\
, tag="rect2")
rect3 = self.canvas.create_rectangle(WIDTH/2-rectWidth/2, HEIGHT/2-rectWidth/2, WIDTH/2+rectWidth/2\
, HEIGHT/2+rectWidth/2, outline="#dbf", fill="#dbf"\
, tag="rect3")
self.rectWidth = rectWidth
self.lastDirection = None
self.direction = None
self.started = False
self.game_over = False
self.score = 0
if (os.path.isfile("high_score.txt")):
scoreFile = open("high_score.txt")
self.high_score = int(scoreFile.read())
scoreFile.close()
else:
self.high_score = 0
self.high_score_label["text"] = "Score tertinggi: " + str(self.high_score)
self.rectangles = [rect1,rect2,rect3]
self.dot = None
self.move()
def create(self, event):
self.lastDirection = self.direction
if self.game_over == False:
if event.keycode == 38: #untuk linux nilai nya adalah 111
self.direction = "up"
elif event.keycode == 39: #untuk linux nilai nya adalah 114
self.direction = "right"
elif event.keycode == 40: #untuk linux nilai nya adalah 116
self.direction = "down"
elif event.keycode == 37: #untuk linux nilai nya adalah 113
self.direction = "left"
elif event.x < WIDTH/2 and HEIGHT/3 < event.y < HEIGHT-HEIGHT/3:
self.direction = "left"
elif event.x > WIDTH/2 and HEIGHT/3 < event.y < HEIGHT-HEIGHT/3:
self.direction= "right"
elif WIDTH/3 < event.x < WIDTH-WIDTH/3 and event.y < HEIGHT/2:
self.direction = "up"
elif WIDTH/3 < event.x < WIDTH-WIDTH/3 and event.y > HEIGHT/2:
self.direction= "down"
def first_movement(self):
w = self.rectWidth
self.canvas.delete("welcome_text")
if self.direction == "left":
self.canvas.move("rect1",-w,0)
self.canvas.after(100)
self.canvas.move("rect1",-w,0)
self.canvas.move("rect2",-w,0)
elif self.direction == "down":
self.canvas.move("rect1",0,w)
self.canvas.after(100)
self.canvas.move("rect1",0,w)
self.canvas.move("rect2",0,w)
elif self.direction == "right":
self.canvas.move("rect1",w,0)
self.canvas.after(100)
self.canvas.move("rect1",w,0)
self.canvas.move("rect2",w,0)
elif self.direction == "up":
self.canvas.move("rect1",0,-w)
self.canvas.after(100)
self.canvas.move("rect1",0,-w)
self.canvas.move("rect2",0,-w)
self.canvas.after(100)
def _move(self):
w = self.rectWidth
while True:
self.score_label["text"] = "Score: " + str(self.score)
if self.started == False and self.direction != None:
self.first_movement()
self.started = True
elif self.started == True and self.game_over == False:
if self.dot == None:
self.make_new_dot()
lock = threading.Lock()
lock.acquire()
endRect = self.rectangles.pop()
frontCoords = self.canvas.coords(self.rectangles[0])
endCoords = self.canvas.coords(endRect)
if self.direction == "left":
self.canvas.move(self.canvas.gettags(endRect), int(frontCoords[0]-endCoords[0])-w,\
int(frontCoords[1]-endCoords[1]))
elif self.direction == "down":
self.canvas.move(self.canvas.gettags(endRect), int(frontCoords[0]-endCoords[0]),\
int(frontCoords[1]-endCoords[1])+w)
elif self.direction == "right":
self.canvas.move(self.canvas.gettags(endRect), int(frontCoords[0]-endCoords[0])+w,\
int(frontCoords[1]-endCoords[1]))
elif self.direction == "up":
self.canvas.move(self.canvas.gettags(endRect), int(frontCoords[0]-endCoords[0]),\
int(frontCoords[1]-endCoords[1])-w)
self.canvas.after(100)
self.rectangles.insert(0, endRect)
lock.release()
self.check_bounds()
self.check_collide()
elif self.game_over == True:
break;
def move(self):
threading.Thread(target=self._move).start()
def make_new_dot(self):
if self.dot != None:
self.canvas.delete(self.dot)
self.dot = None
dotX = random.random()*(WIDTH-self.rectWidth*2) + self.rectWidth
dotY = random.random()*(HEIGHT-self.rectWidth*2) + self.rectWidth
self.dot = self.canvas.create_rectangle(dotX,dotY,dotX+self.rectWidth,dotY+self.rectWidth\
,outline="#ddd", fill="#ddd", tag="dot")
def grow(self):
w = self.rectWidth
lock = threading.Lock()
lock.acquire()
self.score += 100
endCoords = self.canvas.coords(self.rectangles[len(self.rectangles)-1])
thisTag = "rect" + str(len(self.rectangles) + 1)
x1 = int(endCoords[0])
y1 = int(endCoords[1])
x2 = int(endCoords[2])
y2 = int(endCoords[3])
if self.direction == "left":
x1 += w
x2 += w
elif self.direction == "right":
x1 -= w
x2 -= w
elif self.direction == "down":
y1 -= w
y2 -= w
elif self.direction == "up":
y1 += w
y2 += w
thisRect = self.canvas.create_rectangle(x1, y1, x2, y2, outline="#dbf",\
fill="#dbf", tag=thisTag)
self.rectangles.append(thisRect)
lock.release()
def check_bounds(self):
coordinates = self.canvas.coords(self.rectangles[0])
if len(coordinates) > 0:
if coordinates[0] < 0 or coordinates[1] < 0 or coordinates[2] > WIDTH\
or coordinates[3] > HEIGHT:
self.end_game()
def check_collide(self):
frontCoords = self.canvas.coords(self.rectangles[0])
overlapping = self.canvas.find_overlapping(frontCoords[0],frontCoords[1]\
,frontCoords[2],frontCoords[3])
for item in overlapping:
if item == self.dot:
#Snake collided with dot, grow snake and move dot
self.grow()
self.make_new_dot()
if item in self.rectangles[3:]:
#Snake has collided with its body, end game
self.end_game()
if (self.lastDirection == "left" and self.direction == "right") or\
(self.lastDirection == "right" and self.direction == "left") or\
(self.lastDirection == "up" and self.direction == "down") or\
(self.lastDirection == "down" and self.direction == "up"):
self.end_game()
def end_game(self):
self.game_over = True
self.canvas.create_text(WIDTH/2,HEIGHT/2,text="GAME OVER!")
if self.score > self.high_score:
scoreFile = open("high_score.txt", "w")
scoreFile.write(str(self.score))
scoreFile.close()
self.canvas.create_text(WIDTH/2,HEIGHT/2+20,text=\
"wew.... Kamu mendapatkan Score tinggi baru!")
Snake().mainloop()
tampilan :
pertama kali program dijalankan
setelah ditekan salah satu tombol arah
kalau kalah dalam permainan
mengambarkan perolehan nilai