'Insert data from label to MySql python
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'
Solution 1:[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>
Solution 2:[2]
Change e3=Label(root,text=data).place(x=148,y=80)
to
e3=Label(root,text=data)
e3.place(x=148,y=80)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Charlie |