如何在Python和tkinter中找到此stringvar问题的解决方案?

时间:2019-03-23 12:33:32

标签: python mysql tkinter

我们正在尝试使用mysql.connector通过tkinter表单将变量传递到Mysql数据库中

它返回以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in _call_
    return self.func(*args)
  File "C:/Users/Fahim/PycharmProjects/CarPark/PermanentUserRegistrationForm.py", line 29, in register
    result = cursor.execute(sql_insert_query(pram))
TypeError: 'str' object is not callable

我们尝试将其从元组转换为str对象

到目前为止,这是我们的代码:

from tkinter import *

def register():
    print(aa.get())
    print(ab.get())
    print(ac.get())
    print(ad.get())
    print(af.get())

    import sys
    import mysql.connector as mc

    try:
        conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')

    except mc.Error as e:
        print("Error %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)

    pram= ('aa.get()' , 'ab.get()' , 'ac.get()' , 'ad.get()' , 'af.get()')

    sql_insert_query = """ INSERT INTO permanent_user_info
                              (ID, name, address, phone,`car_plate`) VALUES (%s,%s,%s,%s,%s)"""
    cursor = conn.cursor()


    result = cursor.execute(sql_insert_query(pram))
    conn.commit()
    cursor.close()
    conn.close()


    def main_screen():
        screen=Tk()
        screen.geometry("300x400")
        screen.title("Permanent User Registration Form")

        Label(text="ID", font=("Calibri", 16)).place(x=100, y=20)
        Label(text="FULL NAME", font=("Calibri", 16)).place(x=100, y=80)
        Label(text="Address", font=("Calibri", 16)).place(x=100, y=140)
        Label(text="Phone Number", font=("Calibri", 16)).place(x=100, y=200)
        Label(text="Plate Number", font=("Calibri",16)).place(x=100,y=260)  

        global aa
        global ab
        global ac
        global ad
        global af

        aa = StringVar()
        ab = StringVar()
        ac = StringVar()
        ad = StringVar()
        af = StringVar()

        Entry(textvariable=aa).place(x=100, y=50)
        Entry(textvariable=ab).place(x=100, y=110)
        Entry(textvariable=ac).place(x=100, y=170)
        Entry(textvariable=ad).place(x=100, y=230)
        Entry(textvariable=af).place(x=100, y=290)

        Button(text="Register", height="2", width="15", 
        command=register).place(x=100, y=350)

        screen.mainloop()

    main_screen()

它应该从tkinter表单中获取输入并将其插入数据库 数据库的字段为:ID,名称,地址,电话,车牌

1 个答案:

答案 0 :(得分:1)

您为sql_insert_query分配了一个字符串,但是在cursor.execute(sql_insert_query(pram))中您将其称为函数。 它应该是这样的:

result = cursor.execute(sql_insert_query, pram)