0

I am relatively new to coding (this is only the second thing I have ever written) and have hit a roadblock. Any advice, even if it is just a fundamental idea that has evaded me would be helpful.

The actual form I am creating is fairly large (could have up to 400 entry widgets at a future date) so I will place a smaller version of what I am working on here.

from tkinter import *
import sqlite3
import time
import datetime

master = Tk()
master.wm_title("Form")

Label(master, text="Cage ID", width=25).grid(column=1, row=1)
d1 = Entry(master)
d1.grid(column=2, row=1)
Label(master, text="Status", width=25).grid(column=1, row=2)
d2 = Entry(master)
d2.grid(column=2, row=2)

def commit_to_database():
    conn = sqlite3.connect("SGAF_Application_Database.db")
    c = conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS StatusTable(ID BLOB, Status BLOB, Timestamp TEXT)")#table format

    unix = time.time()
    time_data = str(datetime.datetime.fromtimestamp(unix).strftime("%m-%d-%Y %H:%M:%S"))#timestamp generation

    c.execute("INSERT INTO StatusTable(ID, Status, Timestamp) VALUES (?, ?, ?)",
          (d1.get(), d2.get(), time_data))

    conn.commit()

b1 = Button(master, text="Quit", font="bold", fg="black", bg="white", width=15, command=master.quit)
b1.grid(column=3, row=22)

b2 = Button(master, text="Commit", font="bold", fg="black", bg="white", width=15, command=commit_to_database())
b2.grid(column=6, row=22)

mainloop()

Basically this is an attempt to build in the commit into a button widget so that I can access filled in entry widgets to fill in the table. As this exists right now the program enters the variables into the table as soon as I press run for the program. Since the fields are empty to begin with it puts an empty space in the table for the '.get()' variables. In my last program I built a 'def' function into a button that also used many '.get()' commands and it works perfectly so I have no idea why this is happening. Pressing the "Commit" button doesn't seem to do anything at the moment.

Thank you all for the help!

Ryan B
  • 3
  • 2

1 Answers1

0
b2 = Button(master, text="Commit", font="bold", fg="black", bg="white", width=15, command=commit_to_database())

This calls commit_to_database() RIGHT NOW, and uses its return value (which is None) as the value of the command option of the button. You want to leave off the (), to pass the function itself as the command (just like the other button you created).

jasonharper
  • 9,450
  • 2
  • 18
  • 42
  • Thank you so much! It is a little embarrassing to miss such a detail but I guess it is part of being a beginner. – Ryan B Mar 13 '17 at 00:08