0

I want to add a label value in my database from MySQL. I want to do this because I don't want to make the user to select data every time he insert anythig. Here is the code:

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


def Ok():
    tip=e1.get()
    descriere=e2.get()
    data=e3.get()
    timp=e4.get()

    mysqldb = mysql.connector.connect(host="localhost",user="root",password="", database="reglog")
    mycursor= mysqldb.cursor()

    try:
        sql = "INSERT INTO anunturib(id,tip,descriere,data,timp) VALUES (%s,%s,%s,%s,%s)"
        val = ("",tip,descriere,data,timp)
        mycursor.execute(sql,val)
        mysqldb.commit()
        messagebox.showinfo("information","Record inserted succesfully!")

    except Exception as e:
        print(e)
        mysqldb.rollback()
        mysqldb.close()

        

root= Tk();
root.title("Anunturi")
root.geometry("300x250")
global e1
global e2
global e3
global e4



Label(root,text="Tip cerere:").place(x=10,y=10)
Label(root,text="Descriere").place(x=10,y=40)
Label(root,text="Data publicare:").place(x=10,y=80)
Label(root,text="Data expirare:").place(x=10,y=120)

TodayDate = datetime.datetime.now()
an = TodayDate.strftime("%Y")
luna = TodayDate.strftime("%m")
zi = TodayDate.strftime("%d")
data=zi + "-" + luna + "-" + an

e3=Label(root,text=data).place(x=148,y=80)


e1 = Entry(root)
e1.place(x=148,y=10)

e2 = Entry(root)
e2.place(x=148,y=40)



e4 = Entry(root)
e4.place(x=148,y=120)

Button(root,text="Add",command=Ok ,height=3, width=13).place(x=10,y=180)

root.mainloop()

I didn't find anything related to this. In this way I get the error: data=e3.get() AttributeError: 'NoneType' object has no attribute 'get'

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
DariusMan
  • 31
  • 7
  • `e3=Label(root,text=data).place(x=148,y=80)` will assign the return value of `Label.place()` to `e3`. I'm guessing that value is `None`. – snakecharmerb Mar 19 '22 at 18:21

2 Answers2

1

The answer is close to you! What you need is the value of the label, and what is the value of the label? The variable data. So you can not use e3.get() for 2 reasons. Firstly, get() does not exist for Label. Secondly, the text of the label is whatever the value of data is. So you can plainly just insert it:

def Ok():
    tip=e1.get()
    descriere=e2.get()
    # data=e3.get() # Don't define `data`, so `data` is the old data defined before
    timp=e4.get()
    ....
    ....

Also you dont need to manually get the day, month and year and make it all to a string. Instead make use of strftime like:

today_date = datetime.datetime.now()
fmt = '%d-%m-%Y' # Format you want to change a datetime obj to
data = datetime.datetime.strftime(today_date,fmt) 

It also feels like mentioning, you can always get the text of the label with e3.cget('text') or e3['text']. But we don't need it here as it is just the variable data.

Also to understand why you got the initial AttributeError, read: Tkinter: AttributeError: NoneType object has no attribute <attribute name>

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
0

Change e3=Label(root,text=data).place(x=148,y=80) to

e3=Label(root,text=data)
e3.place(x=148,y=80)
Charlie
  • 54
  • 3
  • Ok, now I get this error: File "C:\xampp\htdocs\licenta\python\insert.py", line 10, in Ok data=e3.get() AttributeError: 'Label' object has no attribute 'get' – DariusMan Mar 19 '22 at 18:21
  • The problem is just what the error message says it is. `e3` contains a reference to a `Label` object, and a `Label` object does not have an attribute named `get`...that is, it doesn't support the method `get()`. An `Entry` object does have a `get()` method. That is why the two `get()` calls prior to the one that is failing work fine. - Kinda makes sense, right? A Label is just a visual thing...it doesn't accept input, so it doesn't make sense to try to get a value back from it. – CryptoFool Mar 19 '22 at 18:30
  • Ok, that was clear. Did you have any other option to get that date? I mean maybe I store it in another object? – DariusMan Mar 19 '22 at 18:41