How to fix "TypeError: an integer is required (got type IntVar)" in mysql python
How to fix "TypeError: an integer is required (got type IntVar)" in mysql python
我正在尝试在我的数据库中插入日期 table。但我得到了
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 将以任何日期格式存储到数据库中,我将根据给定的生日计算年龄。
此处进行一些修改,希望对您有所帮助。基本上您需要定义日期的输入方式(所以我选择格式 yyyymmdd
)。也最好以 StringVar 的形式输入。然后在注册的方法中将 yyyymmdd
解析为日期时间对象,然后您可以将其放入数据库。
使用'try-except'任何格式错误的日期将不被接受。
代码
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):
birthday = self.birth_day.get()
try:
_year = int(birthday[0:4])
_month = int(birthday[4:6])
_day = int(birthday[6:8])
birthday = datetime.datetime(_year, _month, _day)
print(birthday) # for debugging purpose you can remove
except Exception as e:
print(e)
birthday = ""
if (birthday == ""):
messagebox.showerror("Error!","Please complete the required field")
else:
sql_string = "INSERT INTO residence (BIRTH_DATE) VALUES(%s)"
print(sql_string) # for debugging purpose you can remove
self.cursor.execute (sql_string,(birthday))
self.db.commit()
messagebox.showinfo("Success!","Registration successful")
self.cursor.close()
self.db.close()
def register(self):
self.master_register = Toplevel()
self.birth_day = StringVar()
self.label_birthday = Label(self.master_register, text = "Birth Day [yyyymmdd]")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = 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()
root = Tk()
Main(root)
root.mainloop()
self.birth_day
是一个 对象 ,不是整数。由于是IntVar
,需要调用get
方法取值。
datetime.datetime(self.birth_day.get())
我正在尝试在我的数据库中插入日期 table。但我得到了
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 将以任何日期格式存储到数据库中,我将根据给定的生日计算年龄。
此处进行一些修改,希望对您有所帮助。基本上您需要定义日期的输入方式(所以我选择格式 yyyymmdd
)。也最好以 StringVar 的形式输入。然后在注册的方法中将 yyyymmdd
解析为日期时间对象,然后您可以将其放入数据库。
使用'try-except'任何格式错误的日期将不被接受。
代码
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):
birthday = self.birth_day.get()
try:
_year = int(birthday[0:4])
_month = int(birthday[4:6])
_day = int(birthday[6:8])
birthday = datetime.datetime(_year, _month, _day)
print(birthday) # for debugging purpose you can remove
except Exception as e:
print(e)
birthday = ""
if (birthday == ""):
messagebox.showerror("Error!","Please complete the required field")
else:
sql_string = "INSERT INTO residence (BIRTH_DATE) VALUES(%s)"
print(sql_string) # for debugging purpose you can remove
self.cursor.execute (sql_string,(birthday))
self.db.commit()
messagebox.showinfo("Success!","Registration successful")
self.cursor.close()
self.db.close()
def register(self):
self.master_register = Toplevel()
self.birth_day = StringVar()
self.label_birthday = Label(self.master_register, text = "Birth Day [yyyymmdd]")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = 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()
root = Tk()
Main(root)
root.mainloop()
self.birth_day
是一个 对象 ,不是整数。由于是IntVar
,需要调用get
方法取值。
datetime.datetime(self.birth_day.get())