简体   繁体   中英

How to find solutions to this stringvar problem in Python and tkinter?

We're trying to pass a variables through a tkinter form into a Mysql database using mysql.connector

It returns the following error:

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

We tried converting it from a tuple to a str object

This is our code so far :

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()

It should take input from the tkinter form and insert it into the database the fields for the Database are: ID, name, Address,phone, car_plate

You assign a string to sql_insert_query but in cursor.execute(sql_insert_query(pram)) you call it like a function. It should rather be something like:

result = cursor.execute(sql_insert_query, pram)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM