Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Srno not updating
#1
Hi all
I have written this code where the entries are added to the database. when I run the code the srnor is updated to the next available number. I have added a clear_text function to clear the entry field after the ADD button is pressed. I also added the set srno to next number+1 instance to this function but it is not working. I would like to change the srno. to next number on clicking of ADD button so that the entry field is clear and the srno. shows the next available number to make a new entry.
import tkinter as tk
from tkinter import*
import mysql.connector
from tkinter import messagebox

mydb= mysql.connector.connect(host="localhost",user="root", password="mudit",database="Clinicmaster",auth_plugin="mysql_native_password")
cursor=mydb.cursor()

def addwork():
    
    srno=ent3.get()
    work=ent4.get()
   
    sql=("INSERT INTO workrequired(srno,work)"  "VALUES(%s,%s)" ) 
    cursor.execute(sql,(srno,work))
    mydb.commit()
    messagebox.showinfo("Confirmation","Work Added")
    print("DONE")
    return True

    



cursor.execute('select count(*) from workrequired')
next_num = cursor.fetchall()[0][0]

win=Tk()
win.title("ADD Work")
win.geometry("300x250")
win.configure(background='light blue')
win.resizable(False,False)

frm1=Frame(win,bg="light blue")
frm1.pack(side=tk.LEFT,padx=20)

var3=StringVar()
srno=StringVar()
var4=StringVar()
workrequired=StringVar()

label3=Label(frm1,textvariable=var3,bg="light blue")
var3.set(" ")
label3.grid(row=2,column=1,padx=10,pady=10)

ent3=Entry(frm1,textvariable=srno,width=10)
srno.set(next_num+1)
ent3.grid(row=2,column=2,sticky=tk.W,padx=10,pady=10)

label4=Label(frm1,textvariable=var4,bg="light blue")
var4.set("Work Required")
label4.grid(row=3,column=1,padx=10,pady=10)

ent4=Entry(frm1,textvariable=workrequired)
workrequired.set(" ")
ent4.grid(row=3,column=2,padx=10,pady=10)

def clear_text():
    workrequired.set(' ')
    srno.set(next_num+1)
    
   


btn=Button(frm1, text="ADD",command= lambda:[addwork(), clear_text()])
btn.grid(row=5,column=2,padx=10,pady=10)

win.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020