Reputation: 25
How to update Time every time I add a data in my tkinter to mysql the time it enter is the time i run my python code in terminal.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import pymysql
from datetime import date , datetime
class DataEntryForm:
def __init__(self,root):
self.root = root
self.root.geometry('600x500+0+0')
self.root.configure(background='gainsboro')
#Variable Declaration
BluetoothAddress=StringVar()
curr_time=datetime.now()
Time=curr_time.strftime('%H:%M:%S')
Date=date.today()
def addData():
if BluetoothAddress.get()=='':
tkinter.messagebox.showerror('No Bluetooth Address','Please Enter Bluetooth Address')
else:
sqlCon = pymysql.connect(host='localhost',user='root',password='GAEPCI2020',database='heshevo')
cur = sqlCon.cursor()
cur.execute("INSERT into datam5 values(%s,%s,%s)",
(BluetoothAddress.get(),
Time,
Date))
sqlCon.commit()
update()
sqlCon.close()
#tkinter.messagebox.showinfo('Data Entered Succesfully', 'Bluetooth Address Entered Succesfully')
Upvotes: 0
Views: 104
Reputation: 1645
Here you go. As Bruno Vermeulen mentioned, your init function is only called when the class is instanciated, and never again afterwards, so move the datetime.now() into the addData function so the time is calculated each time when the function is called. I assume you modified this code from someone else, just as a tip, i would recommend starting from scratch to understand the basic concepts of how classes work. You can test such stuff pretty easy, just remove the bluetooth and mysql code and start with a minimal example and datetime, call some function to see if the time changes etc.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import pymysql
from datetime import date , datetime
class DataEntryForm:
def __init__(self,root):
self.root = root
self.root.geometry('600x500+0+0')
self.root.configure(background='gainsboro')
#Variable Declaration
BluetoothAddress=StringVar()
def addData():
if BluetoothAddress.get()=='':
tkinter.messagebox.showerror('No Bluetooth Address','Please Enter Bluetooth Address')
else:
sqlCon = pymysql.connect(host='localhost',user='root',password='GAEPCI2020',database='heshevo')
cur = sqlCon.cursor()
cur.execute("INSERT into datam5 values(%s,%s,%s)",
(BluetoothAddress.get(),
datatime.now().strftime('%H:%M:%S'),
date.today()))
sqlCon.commit()
update()
sqlCon.close()
#tkinter.messagebox.showinfo('Data Entered Succesfully', 'Bluetooth Address Entered Succesfully')
Upvotes: 1