-1
import tkinter as tk
import mysql.connector

conn = mysql.connector.connect(host='localhost', username='root', password='admin')
my_cursor = conn.cursor()


my_cursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE")
my_cursor.execute("USE EMPLOYEE")
my_cursor.execute("CREATE TABLE IF NOT EXISTS EMPLOYEE_DATA(ID INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(255), SALARY INT)")


def save_data():
    query_insert = "INSERT INTO EMPLOYEE VALUES(%s, %s)", (createNewWindow.lbname_entry.get(), createNewWindow.lbsalary_entry.get())
    my_cursor.execute(query_insert)
    conn.commit()

root = tk.Tk()
root.title("Monthly Pay")
root.geometry('440x550')
root.resizable(0,0)


def createNewWindow():
    newWindow = tk.Toplevel()
    newWindow.title("Monthly Pay")
    newWindow.geometry('440x550')
    newWindow.resizable(0, 0)

    lb2 = tk.Label(newWindow, text='NEW ENTRY', font=('Calibri (Body)', 30))
    lb2.pack()

    lbname = tk.Label(newWindow, text='NAME:', font=(10))
    lbname.place(x=10 ,y=80)

    lbname_entry = tk.Entry(newWindow)
    lbname_entry.place(x=110, y=82)

    lbsalary = tk.Label(newWindow, text='SALARY:', font=(10))
    lbsalary.place(x=10, y=120)

    lbsalary_entry = tk.Entry(newWindow)
    lbsalary_entry.place(x=110, y=122)


    btn1 = tk.Button(newWindow, text='SAVE', command=save_data)
    btn1.place(x=180, y=200)

lbl = tk.Label(root, text = 'MONTHLY PAY', font = ('Calibri (Body)', 35))
lbl.pack()

btn1 = tk.Button(root, text = 'New Employee', command = createNewWindow)
btn1.place(x=180 ,y=200)



root.mainloop()

Error

File "C:/Users/RISHI/PycharmProjects/untitled/monthy_pay.py", line 14, in save_data
    query_insert = "INSERT INTO EMPLOYEE VALUES(%s, %s)", (createNewWindow.lbname_entry.get(), createNewWindow.lbsalary_entry.get())
AttributeError: 'function' object has no attribute 'lbname_entry'

I'm getting this error guys. Please help me and also I'm a newbie in python so please explain in simple language.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Manish Gusain
  • 35
  • 1
  • 7
  • Did you mean to declare it as a class? `class createNewWindow` because you cannot get attributes from a function like that – Abbas May 15 '20 at 11:39
  • Read [about scoping rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) - your variables in your function go out of scope as soon as you leave the function. You cannot access them like this – Patrick Artner May 15 '20 at 11:50
  • Tried using class also but then btn1 command is not working. Could anyone of you correct the code please, it will help me alot. Thanks – Manish Gusain May 15 '20 at 14:10

1 Answers1

0

You can pass the values of lbname_entry and lbsalary_entry to save_data():

def save_data(name, salary):
    query_insert = "INSERT INTO EMPLOYEE_DATA (NAME, SALARY) VALUES (%s, %s)"
    my_cursor.execute(query_insert, (name, salary))
    conn.commit()

Then modify the command option of btn1:

btn1 = tk.Button(newWindow, text='SAVE', command=lambda: save_data(lbname_entry.get(), lbsalary_entry.get()))
acw1668
  • 40,144
  • 5
  • 22
  • 34