Home > Software design >  Can't Adapt Type StringVar in TKinter
Can't Adapt Type StringVar in TKinter

Time:09-26

I'm new to Python, and am trying to work with TKinter and a local PostgreSQL database. My form loads, connects to the database successfully, but when I try to submit the form field values, I receive an error that says, "Can't adapt type StringVar".

I'm setting the variables to pass to the query as either StringVar or IntVar (to match the data types in the postgres table). I'm using the variables for the values entered as a textvariable's since that should allow strings and integers. I'm setting the variable types as either StringVar or IntVar (sets and reps are IntVar).

I'm having difficult time understanding why my strings are not being passed as strings. Thanks for any help. I feel like I'm on the right track, but not sure how to debug this error properly.

import tkinter as tk
from tkinter import ttk
from windows import set_dpi_awareness
import tkinter.font as font
import psycopg2

set_dpi_awareness()

root = tk.Tk()
root.title("Workout Log")

# Set StringVar for incoming values
exercise_value = tk.StringVar()
set_value = tk.IntVar()
rep_value = tk.IntVar()
band_value = tk.StringVar()

# Define Function for insert
def save_to_db(*args):
    try:
        connection = psycopg2.connect(user="postgres",
                                  password="XXXXXXXXXXX",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="testDB")
        print("Connection Successful!")
    except:
        print("Unable to connect to database")
    try:
        cur = connection.cursor()
        cur.execute("INSERT INTO workoutlog (name, set, rep, band) VALUES (%s,%s,%s,%s)", (exercise_value, set_value, rep_value, band_value))
        connection.comit()
        cur.close()
    except (Exception, psycopg2.Error) as error:
        print("Failed to insert record", error)



# Setup main frame
main = ttk.Frame(root, padding=(30,15))
main.grid()

root.columnconfigure(0, weight=1)

# Labels and Buttons for Exercise, Set, Rep, Band, and Submit Button
exercise_label = ttk.Label(main, text="Exercise: ")
exercise_input = ttk.Entry(main, width=20, textvariable=exercise_value)
set_label = ttk.Label(main, text="Set: ")
set_input = ttk.Entry(main, width=10, textvariable=set_value)
rep_label = ttk.Label(main, text="Rep: ")
rep_input = ttk.Entry(main, width=10, textvariable=rep_value)
band_label = ttk.Label(main, text="Band: ")
band_input = ttk.Entry(main, width=10, textvariable=band_value)
submit_button = ttk.Button(main, text="Save", command=save_to_db)

# Display and align the labels and fields
exercise_label.grid(column=0, row=0, sticky="W", padx=10, pady=10)
exercise_input.grid(column=1, row=0, sticky="EW", padx=10, pady=10)
set_label.grid(column=0, row=1, sticky="W", padx=10, pady=10)
set_input.grid(column=1, row=1, sticky="EW", padx=10, pady=10)
rep_label.grid(column=0, row=2, sticky="W", padx=10, pady=10)
rep_input.grid(column=1, row=2, sticky="EW", padx=10, pady=10)
band_label.grid(column=0, row=3, sticky="W", padx=10, pady=10)
band_input.grid(column=1, row=3, sticky="EW", padx=10, pady=10)
submit_button.grid(column=0, row=4, columnspan=2, sticky="EW", pady=10)


root.mainloop()

Form

error

CodePudding user response:

You have to get the values using get() method.

params = exercise_value.get(), set_value.get(), rep_value.get(), band_value.get()
cur.execute("INSERT INTO workoutlog (name, set, rep, band) VALUES (%s,%s,%s,%s)", params)

Else you are just passing the instance of StringVar.

  • Related