所以基本上我需要将用户输入的数据输入到Tkinter输入框并将其存储到我的sqlite3数据库中。我正在使用Python 3.6

时间:2017-01-07 23:26:46

标签: python python-3.x tkinter sqlite

conn = sqlite3.connect('School.db')
c = conn.cursor()
def account_Table(): #Creates the table for user accounts.
    c.execute('CREATE TABLE IF NOT EXISTS accounts(datestamp TEXT, u_id INT, username TEXT, password TEXT, classy TEXT, subject TEXT, forename TEXT, surname TEXT)')

def LoginFrame(Frame): #Builds the login/home screen for the program

        username_holder = Label(text="Username", bg="#b4c4a1", fg="#000000")
        password_holder = Label(text="Password", bg="#b4c4a1", fg="#000000")

        username = Entry()
        password = Entry(show="*")

        username_holder.grid(row=0, column=20)
        password_holder.grid(row=1, column=20)
        username.grid(row=0, column=40)
        password.grid(row=1, column=40)

        checkbox = Checkbutton(text="Keep me logged in", bg="#b4c4a1")
        checkbox.grid(row=3,column=40)

        logbtn = Button(text="Login", bg="#b4c4a1", command = Login_Check)
        logbtn.grid(row=3,column=30)

        sigup = Button(text="New User", bg="#b4c4a1", command = DataEntry)
        sigup.grid(row=3,column=20)

def DataEntry():
    datestamp = datetime.datetime.now() #Shows when the account was made
    u_id = (random.randrange(0,100)) #Gives the user a unique identifier, I don't really need it as the username is a pretty good identifier for this job.

    label_1 = Label(text='Enter A Username', bg="#b4c4a1")
    username = Entry()
    username.grid(row=4, column=40)
    label_1.grid(row=4, column=39)

    label_2 = Label(text='Enter A Password', bg="#b4c4a1")
    password = Entry(show='*')
    label_2.grid(row=5, column=39)
    password.grid(row=5, column=40)

    label_3 = Label(text='Enter Your Class', bg="#b4c4a1")
    classy = Entry()
    classy.grid(row=6, column=40)
    label_3.grid(row=6, column=39)

    subject = Entry()
    label_4=Label(text='Enter Your Subject', bg="#b4c4a1")
    subject.grid(row=7, column=40)
    label_4.grid(row=7, column=39)

    label_5=Label(text='Enter Your First Name', bg="#b4c4a1")
    forename = Entry()
    forename.grid(row=8, column=40)
    label_5.grid(row=8, column=39)

    surname = Entry()
    label_6 = Label(text='Enter Your Second Name', bg="#b4c4a1")
    surname.grid(row=9, column=40)
    label_6.grid(row=9, column=39)

'DataEntry():'中的所有条目都将作为新记录进入帐户表。

def Login_Check():
    print(".... Doing This Later")



#account_Table() 
root = Tk()
root.geometry('630x350')
root.config(bg="#b4c4a1")
root.title("Student Manager")
root.wm_iconbitmap('Icon.png')
LoginFrame(root)
Tk.mainloop

我不知道如何将来自Tkinter登录框的条目作为使用sqlite3的SQL'INSERT INTO'语句的值传递,它在使用控制台作为输入值的方法之前确实有效,但是失败了为用户提供GUI的重点。

1 个答案:

答案 0 :(得分:-1)

您可以使用get()窗口小部件的Entry功能获取输入的内容,然后使用字符串格式生成SQL命令。

# Python 2.7

import sqlite3 as sql
import tkinter as tk
import datetime

connection = sql.connect('School.db')
cursor = connecton.cursor()
# Sidenote: your database does not have a PRIMARY KEY
# I made u_id the PRIMARY KEY
cursor.execute('CREATE TABLE IF NOT EXISTS accounts(datestamp TEXT, ' \ 
    'u_id INT PRIMARY KEY, username TEXT, password TEXT, classy TEXT, ' \
    'subject TEXT, forename TEXT, surname TEXT)')
insert_command = """INSERT OR IGNORE INTO accounts(datestamp, username, """ \
                 """password, classy, subject, forename, surname) VALUES(""" \   
                 """'%s', '%s', '%s', '%s', '%s', '%s', '%s');"""

root = tk.Tk()
username_entry = tk.Entry(root)
password_entry = tk.Entry(root)
classy_entry = tk.Entry(root)
subject_entry = tk.Entry(root)
forename_entry = tk.Entry(root)
surname_entry = tk.Entry(root)

def insert_user():
    datestamp = datetime.datetime.now()
    username = username_entry.get()
    password = password_entry.get()
    classy = classy_entry.get()
    subject = subject_entry.get()
    forename = forename_entry.get()
    surname = surname_entry.get()
    cursor.execute(insert_command % (datestamp, username, password,
                                     classy, subject, forename, surname))

insert_button = tk.Button(root, text="Insert my account", command=insert_user)


username_entry.pack()
password_entry.pack()
classy_entry.pack()
subject_entry.pack()
forename_entry.pack()
surname_entry.pack()
insert_button.pack()

root.mainloop()