Я пытаюсь вставить дату в таблицу базы данных. но я получил
Traceback (most recent call last):
File "D:\SchoolApp\Python\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/SchoolApp/Python/register only.py", line 45, in register
self.entry_birthday = datetime.datetime(self.birth_day)
TypeError: an integer is required (got type IntVar)
Я преобразовал свою переменную self.get_birthday
в int(self.get_birthday)
, но это все еще ошибка.
Вот мой код
import tkinter as tk
import pymysql
from tkinter import *
from tkinter import messagebox
import datetime
class Main(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
master.title("Thesis")
self.register_button = Button(master, text="Register", command=self.register)
self.register_button.pack()
self.db = pymysql.connect(host = "localhost",user = "root",passwd = "",db = "database")
self.cursor = self.db.cursor()
self.QueryResident = "CREATE TABLE IF NOT EXISTS residence (BIRTH_DATE date, AGE varchar(255)not null)"
self.cursor.execute(self.QueryResident)
def registered(self,birth_day):
self.get_birth_day =self.birth_day.get()
if (self.get_birth_day == ""):
messagebox.showerror("Error!","Please complete the required field")
else:
self.cursor.execute ("INSERT INTO residence (BIRTH_DATE) VALUES(%s)",(self.get_birth_day))
self.db.commit()
messagebox.showinfo("Success!","Registration successful")
self.cursor.close()
self.db.close()
def register(self):
self.master_register = Toplevel()
self.birth_day = IntVar()
self.label_birthday = Label(self.master_register, text = "Birth Day")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = self.birth_day)
self.entry_birthday = datetime.datetime(self.birth_day)
self.entry_birthday.pack()
self.button_submit = Button(self.master_register, text = "Submit", command = lambda: self.registered(self.birth_day))
self.button_submit.pack()
Мой ожидаемый результатвведенная дата get будет сохранена в базе данных в любом формате даты, и я вычислю возраст на основе данного дня рождения.