如何在MySQL Python中修复“ TypeError:需要一个整数(必须输入IntVar类型)”

我正在尝试在数据库表中插入日期。但是我得到了

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,**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将以任何日期格式存储到数据库中,并且我将根据给定的出生日期计算年龄。

suse119 回答:如何在MySQL Python中修复“ TypeError:需要一个整数(必须输入IntVar类型)”

这里有一些修改,希望对您有所帮助。基本上,您需要定义如何输入日期(因此,我选择格式system.web.Mail)。也最好以StringVar的形式输入。然后,在已注册的方法中,将yyyymmdd解析为日期时间对象,然后可以将其放入数据库中。

使用“ try-except”格式错误的任何日期均不接受。

代码

yyyymmdd
,

request.session['my_key'] = 'My Value' object ,不是整数。由于它是self.birth_day,因此您需要调用IntVar方法来检索值。

get
本文链接:https://www.f2er.com/3147057.html

大家都在问