-1

I am trying to get the data from fields but it is not returning anything. Please help in obtaining the data in the variables so that the same can be inserted in the MySQL. I am trying to get the data from fields but it is not returning anything. Please help in obtaining the data in the variables so that the same can be inserted in the MySQL.

from tkinter import *
from tkinter import messagebox
import pymysql
import mysql.connector


def front():
    root1 = Toplevel()
    root1.title("PAYROLL MANAGEMENT")
    root1.geometry("500x200+500+260")

    label5 = Label(root1, text="PAYROLL MANAGEMENT SYSTEM", font=("Georgia", 19,"bold"), fg="mediumblue")
    label5.place(x=17, y=0)

    print1="Enter the name of the database: "
    msg4 = Message(root1, text=print1, font=('Georgia', 14), width=885, fg="black")
    msg4.place(x=10, y=60)

    Entry22 = Entry(root1,bg="lightyellow")
    Entry22.place(x=350, y=73)


    print1 = "Enter the name of the table: "
    msg40 = Message(root1, text=print1, font=('Georgia', 14), width=885, fg="black")
    msg40.place(x=10, y=90)

    Entry22_ = Entry(root1,bg="lightyellow")
    Entry22_.place(x=350, y=105)

    def data1():

        global a
        a=Entry22.get()
        global b
        b = Entry22_.get()
        print(b)

        mydatabase = mysql.connector.connect(host="localhost", user="root", passwd="mysql@123")
        mycursor = mydatabase.cursor()
        mycursor.execute("CREATE DATABASE if not exists %s" %a)
        print("Database created Succesfully....")
        mycursor = mydatabase.cursor()
        mycursor.execute("Use " + a)
        query = "Create table if not exists " + b + " \
        (Employee_Code varchar(20) not null primary key,\
        Department varchar(30),\
        Designation varchar(30),\
        Dob varchar(30),\
        Address varchar(50),\
        Month varchar(20),\
        Year varchar(20),\
        Email varchar(40),\
        Doj varchar(30),\
        Gender varchar(30),\
        Employee_Name varchar(40) not null,\
        Aadhaar_No varchar(12),\
        Absents varchar(10),\
        PF_No varchar(10),\
        Age varchar(10),\
        Total_Days varchar(10),\
        Basic_Salary varchar(10),\
        Contact_No varchar(10),\
        Medical varchar(10),\
        Convince varchar(10),\
        Provident_Fund varchar(10),\
        Net_Salary varchar(10))"

        print("Table " + b + " created succesfully....")
        mycursor.execute(query)
        payroll()
    myButton = Button(root1, text="Enter",command=data1)
    myButton.place(x=220, y=150)
    root1.mainloop()


def payroll():
    class EmployeeSystem:
        def __init__(self, root):
            self.root = root
            self.root.title("Employee Payroll Management System")
            self.root.geometry("1365x740+80+20")
            self.root.config(bg="white")
            title = Label(self.root, text="Payroll Management System", font=("times new roman", 30, "bold"), bg="blue",
                          fg="white", anchor="w", padx=10).place(x=0, y=0, relwidth=1)

            # ============================Frame1====================

            self.var_empcode = StringVar()
            self.var_designation = StringVar()
            


            Frame1 = Frame(self.root, bd=5, relief=RIDGE, bg="white")
            Frame1.place(x=10, y=70, width=750, height=650)
            title1 = Label(Frame1, text="Employee Details", font=("times new roman", 20), bg="lightgray", fg="black",anchor="w", padx=10).place(x=0, y=0, relwidth=1)
            lbl_code = Label(Frame1, text="Employee code", font=("times new roman", 20), bg="white", fg="black").place(x=10, y=70)
           # txt_code = Entry(Frame1, font=("times new roman", 15), textvariable = self.var_empcode, bg="lightyellow",fg="black").place(x=220, y=75, width=200)
            a = Entry(Frame1, font=("times new roman", 15), bg="lightyellow",fg="black").place(x=220, y=75, width=200)

            but_serach = Button(Frame1, text="Search", font=("times new roman", 20), bg="gray", fg="black").place(x=470,y=74,height=30)

            # ===============row1
            lbl_designation = Label(Frame1, text="Designation", font=("times new roman", 20), bg="white",fg="black").place(x=10, y=120)
            txt_designation = Entry(Frame1, font=("times new roman", 15), textvariable=self.var_designation,bg="lightyellow", fg="black").place(x=170, y=125, width=170)
            lbl_doj = Label(Frame1, text="Date of join", font=("times new roman", 20), bg="white", fg="black").place(x=380, y=120)
            txt_doj = Entry(Frame1, font=("times new roman", 15), textvariable=self.var_DOJ, bg="lightyellow",fg="black").place(x=520, y=125)

           
           
            but_calculate = Button(Frame2, text="Calculate", font=("times new roman", 20),bg="gray", fg="black").place(x=160, y=250, height=30, width=120)
            but_save = Button(Frame2, text="Save", font=("times new roman", 20),command=self.add, bg="gray", fg="black").place(x=300,y=250,height=30,width=120)
            but_clear = Button(Frame2, text="Clear", font=("times new roman", 20), bg="gray", fg="black").place(x=440,y=250,height=30,width=120)

        
        # =====================================================  FUNCTIONS  ==========================
        def add(self):

            mydatabase = mysql.connector.connect(host="localhost", user="root", passwd="mysql@123")
            mycursor = mydatabase.cursor()
            mycursor.execute("Use " + a)
            tup = (self.var_empcode.get(),
                    self.var_Department.get())
            sql = "insert into " + b + " values(%s,%s)"
            print(sql,tup)
            mycursor.execute(sql,tup)
            mydatabase.commit()
            mydatabase.close()
            messagebox.showinfo("Success", "Record added Successfully.", parent=self.root)

    root = Tk()
    obj = EmployeeSystem(root)
    root.mainloop()

root=Tk()
root.geometry("250x100+600+300")
label1=Label (root, text="Username", font=("Georgia", 9,"bold"))
label1.place(x=15,y=10)

label1=Label (root, text="Password", font=("Georgia", 9,"bold"))
label1.place(x=15,y=35)

Entry1=Entry(root)
Entry1.place(x=100,y=10)

Entry2=Entry(root)
Entry2.place(x=100,y=35)
def login():
    s=Entry1.get()
    s1=Entry2.get()
    print(s,s1)
    if s=="" and s1=="":
        front()
    else:
        messagebox.showerror("ERROR", "Wrong Username or Password.")

Button1=Button(root,text="Login",bg="lime",command=login)
Button1.place(x=105,y=70)
root.mainloop()
Pooja_1
  • 19
  • 2
  • 2
    When asking questions here, it's best to provide the code for a relatively small [mre], **not** your whole program. – martineau Dec 26 '21 at 20:15
  • 1
    Please try to reduce the amount of code necessary to reproduce your problem. If the problem is with an entry widget and a function, we really only need one entry widget and none of the labels or other entry widgets. – Bryan Oakley Dec 26 '21 at 21:08
  • Hi! I have tried to minimize code. – Pooja_1 Dec 27 '21 at 13:51
  • You don't get what "minimal" means in this context, which is, namely, too take everything *out* that's not directly relevant to the question at hand. In this case it mean getting rid of *all* the SQL stuff because all you really want to know is how to extract the information that been put in `Entry` widgets to create a long string (which you happen to be planning on using to do something SQL-related). – martineau Dec 27 '21 at 16:56
  • I tried to remove all the SQL stuff from your code myself. One problem I encountered was `AttributeError: 'EmployeeSystem' object has no attribute 'var_DOJ'`. While making those changes this I noticed *may* places in your code where you were making the mistake described in [this](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) question — I don't know if that has any bearing on what you're asking about or not, but it's clearly wrong. – martineau Dec 27 '21 at 17:17
  • Your current code also doesn't define `Frame2` but references it. [mre] should be runnable so other can test their solutions. – martineau Dec 27 '21 at 17:36
  • Avoid using multiple instances of `Tk()`. – acw1668 Dec 28 '21 at 00:26
  • Dear All ... Thank you so much for your help...I was able to solve the problem by removing class...though could not get as to why it is not working in the former case. @martineau Thanks for the reference of the following link: https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name. I did not remove the whole code ...as in parts it is working. – Pooja_1 Dec 28 '21 at 04:54

1 Answers1

0

hmm, you can use StringVar() in case of this, here you can use it like this

from tkinter import *

root = Tk()

abcd = StringVar()
e = Entry(root, textvariable=abcd).pack()

def click_Button():
    content = abcd.get()
    print("Hello ",content)

b = Button(root, text="gethello", command=click_Button).pack()

root.mainloop()

you can use content variable in any form