-1

Working on a group program that has users and passwords. We are trying to insert our users and passwords via python to sqlite. But atm we are only sending blank data before our graphic window pops up with a button that is supposed to send the data. We have tried different variations found on multiple websites but nothing seems to work.

from tkinter import *
import sqlite3
import sys




conn = sqlite3.connect('indexCards.db')
cur = conn.cursor()
users ={}

def addUser(entUsername, entPassword):

    #global entUsername, entPassword
    NAME = entUsername.get()
    PASSWORD = entPassword.get()
    print("add User") #this is testing only
    print(NAME, PASSWORD) # this is testing to see the passed variable
    //
   //This is where we are having the issues
    conn.executemany('INSERT INTO USER(NAME,PASSWORD)\
          VALUES (? ,?);', [(NAME,PASSWORD)])
    conn.commit();

    return (NAME, PASSWORD)


def makeWindow():
    global entUsername, entPassword
    window = Tk()
    window.title('Add New User')

    lblInst = Label(window, text = "Create your account here:", font=("Helvetica",16))
    lblInst.pack()
    lblUsername = Label(window, text="Please enter a username: ")
    entUsername = Entry(window)
    lblUsername.pack()
    entUsername.pack()


    lblPassword = Label(window, text="Please enter a password: ")
    entPassword = Entry(window)

    lblPassword.pack()
    entPassword.pack()


    btn = Button(window, text="Create", command=addUser(entUsername, entPassword))

    btn.pack()
    print (addUser(entUsername, entPassword))
    print('entUsername', 'entPassword')

    #window.mainloop()

    return window



#if __name__ == '__main__':
 #   main():

window = makeWindow()
window.mainloop()
Aaron
  • 3
  • 1
  • 2
  • `conn.executemany('INSERT INTO USER(NAME,PASSWORD)\ VALUES (? ,?);', [(NAME,PASSWORD)])` -> `conn.execute('INSERT INTO USER(NAME,PASSWORD) VALUES (? ,?);', (NAME,PASSWORD))`. But are you seriously going to store plain-text password into a database? – 301_Moved_Permanently Oct 28 '15 at 14:55
  • This program doesn't really need security since it's just for a class. We really don't even need the user and password, I just need the ability to be able to send info to the sql database without it being blank. – Aaron Oct 30 '15 at 14:05

2 Answers2

0

The problem is that you're calling the function immediately, before the user has a chance to enter any data.

See Why is Button parameter “command” executed when declared?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Great thank you for the link we were able to fix our program! – Aaron Oct 30 '15 at 14:15
  • @Aaron: if you found this helpful, please read [What should I do when someone answers my question](http://stackoverflow.com/help/someone-answers) – Bryan Oakley Oct 30 '15 at 14:17
0

The reason it invokes the method immediately and pressing the button does nothing is that addUser(entUsername, entPassword) is evaluated and its return value is attributed as the command for the button. So if addUser prints something to tell you it has run and returns None, you just run addUser to evaluate its return value and given None as the command for the button.
To have buttons to call functions with different arguments you can use global variables, although I can't recommend it:

import sqlite3
from tkinter import *

conn = sqlite3.connect('indexCards.db')
cur = conn.cursor()
users = {}
cur = conn.cursor()
cur.execute('''
Create table if not exists USER(NAME varchar(100), PASSWORD varchar(100))
''')


def addUser():
    NAME = entUsername.get()
    PASSWORD = entPassword.get()
    print("add User")  # this is testing only
    print(NAME, PASSWORD)  # this is testing to see the passed variable
    # This is where we are having the issues
    conn.executemany('INSERT INTO USER(NAME,PASSWORD) VALUES (? ,?);', [(NAME, PASSWORD)])
    conn.commit()

    return (NAME, PASSWORD)


def makeWindow():
    global entUsername
    global entPassword
    window = Tk()
    window.title('Add New User')

    lblInst = Label(window, text="Create your account here:", font=("Helvetica", 16))
    lblInst.pack()
    lblUsername = Label(window, text="Please enter a username: ")
    entUsername = Entry(window)
    lblUsername.pack()
    entUsername.pack()

    lblPassword = Label(window, text="Please enter a password: ")
    entPassword = Entry(window)

    lblPassword.pack()
    entPassword.pack()

    btn = Button(window, text="Create", command=addUser)

    btn.pack()
    print(addUser())

    window.mainloop()

    return window

window = makeWindow()


If you don't want to create global then you can pass the function with multiple arguments to an anonymous function i.e lambda function

btn = Button(window, text="Create", command= lambda : addUser(entUsername, entPassword))
DaVinci
  • 868
  • 1
  • 7
  • 25
  • Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. – ChrisGPT was on strike May 19 '20 at 17:41