Hello guys, I’m new at programming.
I did the project with almost the same code with source code from Youtube. But I got some error with different than video I watched. Here the source code.
from tkinter import *
from tkinter import ttk
import time
import random
import sys
import pymysql
class Project:
def __init__(self, root):
self.root = root
#==========Membuat window============
self.root.geometry("1350x690+0-0")
#===========Membuat logo dan title=========
self.root.title("Pengukuran Tinggi dan Berat Balita")
self.root.iconbitmap("logo.ico")
#==========Membuat Variabel============
self.root.ID_vr = StringVar()
self.root.Nama = StringVar()
self.root.Jenis_Kelamin = StringVar()
self.root.Tempat_Lahir = StringVar()
self.root.Tanggal_Lahir = StringVar()
self.root.Anak_Ke = StringVar()
self.root.Nama_Ayah = StringVar()
self.root.Nama_Ibu = StringVar()
self.root.txt_Alamat = StringVar()
#===========Membuat jam===========
localtime = time.asctime (time.localtime(time.time()))
def Title():
#Membuat info title
Title = Label(self.root, font = ('arial', 20, 'bold'), text = 'PENGUKURAN TINGGI DAN BERAT BALITA', fg = 'black', bg = 'blue' , bd = 10, relief = GROOVE)
Title.pack(side = TOP, fill = X)
Title()
#Membuat info jam // anchor untuk posisi jam
clock = Label(self.root, font = ('times', 12, 'bold'), text = localtime, fg = 'black', bd = 10, anchor = 'nw')
clock.pack(fill = 'both', expand = 1)
def tiktok(self):
#Mengatur jam sesuai waktu PC // %c untuk format jam lengkap dengan hari
time_string = time.strftime('%c')
clock.config(text = time_string)
clock.after(200, tiktok)
tiktok(self)
#============Membuat Frame Data===========
Data_Frame = Frame(self.root, bd = 4, relief = RIDGE, bg = 'yellow')
Data_Frame.place(x = 0, y = 120, width = 550, height = 540)
#============Membuat Judul Frame Data==========
lbl_title1 = Label(Data_Frame, text = 'Manage Data', bg = 'yellow' , fg = 'red' ,font = ('arial', 20, 'bold'))
lbl_title1.grid(row = 0, column = 0, padx = 0, pady = 0)
#===========Membuat Label - Search Engine=================
lbl_search = Label(Data_Frame, text = 'Search By',bg = 'yellow', fg = 'black', font = ('arial', 10))
lbl_search.grid(row = 1 , column = 0, padx = 10, pady = 0, sticky = 'w')
#============Membuat Combo Box - Search Engine=================
combo_Search = ttk.Combobox(Data_Frame, font = ('arial', 10))
combo_Search['values'] = ('ID', 'Nama', 'Jenis Kelamin', 'Nama Ibu')
combo_Search.grid(row = 2, column = 0, padx = 10, pady = 5, sticky = 'w')
#===========Membuat Searching Entry================
txt_Search = Entry(Data_Frame, font = ('arial', 11))
txt_Search.grid(row = 2, column = 0, padx = 180, pady = 5, ipadx = 40, sticky = 'w')
#============Membuat Button Frame - Search Button=============
btn_Frame1 = Frame(Data_Frame, bg = 'yellow')
btn_Frame1.place(x = 440, y = 62, width = 100, height = 30)
#============Membuat Search Button==============
search_Button = Button(btn_Frame1, text = 'SEARCH', width = 12)
search_Button.grid(row = 2, column = 0, padx = 0, pady = 0)
#============Membuat Label - No. Registrasi==========
lbl_search = Label(Data_Frame, text = 'No. Registrasi', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_search.grid(row = 3 , column = 0, padx = 10, pady = 10, sticky = 'w')
#===========Membuat ID Entry================
txt_ID = Entry(Data_Frame, textvariable = self.root.ID_vr, font = ('arial', 11))
txt_ID.grid(row = 3, column = 0, padx = 180, pady = 10, ipadx = 80)
#============Membuat Label - Nama==========
lbl_ID = Label(Data_Frame, text = 'Nama', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_ID.grid(row = 4 , column = 0, padx = 10, pady = 0, sticky = 'w',)
#===========Membuat Nama Entry================
txt_ID = Entry(Data_Frame, textvariable = self.root.Nama, font = ('arial', 11))
txt_ID.grid(row = 4, column = 0, padx = 180, pady = 0, ipadx = 80)
#============Membuat Label - Jenis Kelamin==========
lbl_Gender = Label(Data_Frame, text = 'Jenis Kelamin', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_Gender.grid(row = 5 , column = 0, padx = 10, pady = 5, sticky = 'w',)
#============Membuat Combo Box - Jenis Kelamin=================
combo_Gender = ttk.Combobox(Data_Frame, textvariable = self.root.Jenis_Kelamin, font = ('arial', 10))
combo_Gender['values'] = ('Laki-Laki', 'Perempuan')
combo_Gender.grid(row = 5, column = 0, padx = 10, pady = 0, ipadx = 80)
#============Membuat Label - TTL==========
lbl_TTL = Label(Data_Frame, text = 'Tempat / Tanggal Lahir', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_TTL.grid(row = 6 , column = 0, padx = 10, pady = 10, sticky = 'w',)
#============Membuat Box Frame - Tempat Lahir=============
box_Frame1 = Frame(Data_Frame, bg = 'yellow')
box_Frame1.place(x = 180, y = 205, width = 120, height = 30)
#===========Membuat Entry Tempat Lahir================
txt_Tempat = Entry(box_Frame1, textvariable = self.root.Tempat_Lahir, font = ('arial', 11))
txt_Tempat.grid(row = 6, column = 0, padx = 0, pady = 0, ipadx = 120)
#============Membuat Box Frame - Tanggal Lahir=============
box_Frame2 = Frame(Data_Frame, bg = 'yellow')
box_Frame2.place(x = 310, y = 205, width = 193, height = 30)
#===========Membuat Entry Tanggal Lahir================
txt_Tempat = Entry(box_Frame2, textvariable = self.root.Tanggal_Lahir, font = ('arial', 11))
txt_Tempat.grid(row = 6, column = 0, padx = 0, pady = 0, ipadx = 193)
#============Membuat Label - Anak ke-berapa==========
lbl_Anak = Label(Data_Frame, text = 'Anak Ke- (Angka)', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_Anak.grid(row = 7 , column = 0, padx = 10, pady = 10, sticky = 'w',)
#===========Membuat Entry - Anak ke-berapa================
txt_Anak = Entry(Data_Frame, textvariable = self.root.Anak_Ke, font = ('arial', 11))
txt_Anak.grid(row = 7, column = 0, padx = 180, pady = 10, ipadx = 80)
#============Membuat Label - Nama Ayah==========
lbl_Ayah = Label(Data_Frame, text = 'Nama Ayah', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_Ayah.grid(row = 8 , column = 0, padx = 10, pady = 10, sticky = 'w',)
#===========Membuat Entry - Nama Ayah================
txt_Ayah = Entry(Data_Frame, textvariable = self.root.Nama_Ayah, font = ('arial', 11))
txt_Ayah.grid(row = 8, column = 0, padx = 180, pady = 10, ipadx = 80)
#============Membuat Label - Nama Ibu==========
lbl_Ibu = Label(Data_Frame, text = 'Nama Ibu', bg = 'yellow', fg = 'black', font = ('arial', 12))
lbl_Ibu.grid(row = 9 , column = 0, padx = 10, pady = 10, sticky = 'w',)
#===========Membuat Box - Nama Ibu================
txt_Ibu = Entry(Data_Frame, textvariable = self.root.Nama_Ibu, font = ('arial', 11))
txt_Ibu.grid(row = 9, column = 0, padx = 180, pady = 10, ipadx = 80)
#=============Membuat Label - Alamat===================
lbl_Alamat = Label(Data_Frame, text = 'Alamat\n\n' , bg = 'yellow' , fg = 'black' , font =('arial', 12))
lbl_Alamat.grid(row = 10 , column = 0 , padx = 10, pady = 10 , sticky = 'w')
#=============Membuat Entry - Alamat====================
self.root.txt_Alamat = Text(Data_Frame, width = 20 , height = 4 , font = ('', 11))
self.root.txt_Alamat.grid(row = 10 , column = 0 , padx = 180 , pady = 10 , ipadx = 80 , ipady = 5)
#===============Membuat Frame Tombol===========
btn_Frame2 = Frame(Data_Frame, bg = 'yellow')
btn_Frame2.place(x = 120, y = 490, width = 400, height = 35)
#================Membuat Tombol Navigasi=============
Newbtn = Button(btn_Frame2, text = 'New', width = 10, command = self.add_balita).grid(row = 0, column = 0, padx = 5, pady = 2)
Updatebtn = Button(btn_Frame2, text = 'Update', width = 10, command = self.update_data).grid(row = 0, column = 2, padx = 5, pady = 2)
Clearbtn = Button(btn_Frame2, text = 'Clear', width = 10, command = self.clear).grid(row = 0, column = 4, padx = 5, pady = 2)
Delbtn = Button(btn_Frame2, text = 'Delete', width = 10).grid(row = 0, column = 6, padx = 5, pady = 2)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#============Membuat Frame Tabel===========
Table_Frame = Frame(self.root, bd = 4, relief = RIDGE, bg = 'yellow')
Table_Frame.place(x = 600, y = 120, width = 750, height = 540)
#==============Membuat Judul Frame Data Tabel======================
lbl_title2 = Label(Table_Frame , text = 'Data List' , bg = 'yellow' , fg = 'red' , font = ('arial', 20 , 'bold'))
lbl_title2.grid(row = 0 , columnspan = 2 , padx = 300, pady = 0)
#=============Membuat Frame Data Tabel=========================
data_Frame = Frame(Table_Frame , bd = 4 , relief = RIDGE , bg = 'yellow')
data_Frame.place(x = 10 , y = 60 , width = 725 , height = 460)
#=============Membuat Tabel===============
scroll_x = Scrollbar(data_Frame, orient = HORIZONTAL)
scroll_y = Scrollbar(data_Frame, orient = VERTICAL)
self.baby_Table = ttk.Treeview(data_Frame, column = ('ID', 'Nama', 'Jenis Kelamin', 'Tempat Lahir', 'Tanggal Lahir', 'Anak Ke-' , 'Nama Ayah' , 'Nama Ibu' , 'Alamat'), xscrollcommand = scroll_x.set , yscrollcommand = scroll_y.set)
scroll_x.pack(side = BOTTOM , fill = X)
scroll_y.pack(side = RIGHT, fill = Y)
scroll_x.config(command = self.baby_Table.xview)
scroll_y.config(command = self.baby_Table.yview)
self.baby_Table.heading('ID', text = 'ID')
self.baby_Table.heading('Nama', text = 'Nama')
self.baby_Table.heading('Jenis Kelamin' , text = 'Jenis Kelamin')
self.baby_Table.heading('Tempat Lahir', text = 'Tempat Lahir')
self.baby_Table.heading('Tanggal Lahir' , text = 'Tanggal Lahir')
self.baby_Table.heading('Anak Ke-', text = 'Anak Ke-')
self.baby_Table.heading('Nama Ayah', text = 'Nama Ayah')
self.baby_Table.heading('Nama Ibu', text = 'Nama Ibu')
self.baby_Table.heading('Alamat', text = 'Alamat')
self.baby_Table['show'] = 'headings'
self.baby_Table.column('ID', width = 50)
self.baby_Table.column('Nama', width = 150)
self.baby_Table.column('Jenis Kelamin', width = 150)
self.baby_Table.column('Tempat Lahir', width = 150)
self.baby_Table.column('Tanggal Lahir', width = 150)
self.baby_Table.column('Anak Ke-', width = 150)
self.baby_Table.column('Nama Ayah', width = 150)
self.baby_Table.column('Nama Ibu', width = 150)
self.baby_Table.column('Alamat', width = 150)
self.baby_Table.pack(fill = BOTH, expand = 1)
self.fetch_data()
self.baby_Table.bind('<ButtonRelease-1>', self.get_cursor)
def add_balita(self):
con = pymysql.connect(host = 'localhost', user = 'root', password = '', database = 'stm')
cur = con.cursor()
cur.execute('insert into baby values(%s, %s, %s, %s, %s, %s, %s, %s, %s)',(self.root.ID_vr.get(),
self.root.Nama.get(),
self.root.Jenis_Kelamin.get(),
self.root.Tempat_Lahir.get(),
self.root.Tanggal_Lahir.get(),
self.root.Anak_Ke.get(),
self.root.Nama_Ayah.get(),
self.root.Nama_Ibu.get(),
self.root.txt_Alamat.get('1.0', END)
))
con.commit()
self.fetch_data()
con.close()
def fetch_data(self):
con = pymysql.connect(host = 'localhost', user = 'root', password = '', database = 'stm')
cur = con.cursor()
cur.execute('select * from baby')
rows = cur.fetchall()
if len(rows) != 0:
self.baby_Table.delete(*self.baby_Table.get_children())
for row in rows:
self.baby_Table.insert('', END, values = row)
con.commit()
con.close()
def clear(self):
self.root.ID_vr.set('')
self.root.Nama.set('')
self.root.Jenis_Kelamin.set('')
self.root.Tempat_Lahir.set('')
self.root.Tanggal_Lahir.set('')
self.root.Anak_Ke.set('')
self.root.Nama_Ayah.set('')
self.root.Nama_Ibu.set('')
self.root.txt_Alamat.set('')
def get_cursor(self, ev):
cursor_row = self.baby_Table.focus()
contents = self.baby_Table.item(cursor_row)
row = contents['values']
self.root.ID_vr.set(row[0])
self.root.Nama.set(row[1])
self.root.Jenis_Kelamin.set(row[2])
self.root.Tempat_Lahir.set(row[3])
self.root.Tanggal_Lahir.set(row[4])
self.root.Anak_Ke.set(row[5])
self.root.Nama_Ayah.set(row[6])
self.root.Nama_Ibu.set(row[7])
self.root.txt_Alamat.delete('1.0', END)
self.root.txt_Alamat.insert(END, row[8])
def update_data(self):
con = pymysql.connect(host = 'localhost', user = 'root', password = '', database = 'stm')
cur = con.cursor()
cur.execute('update balita set nama = %s, jenis_K = %s, tempat_L = %s, tanggal_L = %s, anak_K = %s, nama_A = %s, nama_I = %s, Alamat = %s where id_var = %s',(self.root.Nama.get(),
self.root.Jenis_Kelamin.get(),
self.root.Tempat_Lahir.get(),
self.root.Tanggal_Lahir.get(),
self.root.Anak_Ke.get(),
self.root.Nama_Ayah.get(),
self.root.Nama_Ibu.get(),
self.root.txt_Alamat.get('1.0', END),
self.root.ID_vr.get()
))
con.commit()
self.fetch_data()
self.clear()
con.close()
#================Jalankan Program==============
root = Tk()
ob = Project(root)
root.mainloop()
And here’s the error.
C:\root>python GUI.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\root\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "GUI.py", line 281, in update_data
self.root.ID_vr.get()
File "C:\root\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "C:\root\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "C:\root\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\root\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "C:\root\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "C:\root\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "C:\root\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\root\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1054, "Unknown column 'jenis_K' in 'field list'")
Hope you will solve the problem. Thanks.