可变数据未添加到TKinter SQLite3数据库

时间:2017-03-25 18:33:16

标签: python tkinter sqlite

我正在开发一个应用程序,允许用户回答音乐,基于视觉阅读的问题。我试图将他们的分数添加到SQLite数据库。所有代码当前都在工作,我没有收到有关我的数据库的任何错误消息。我的问题是变量,时间戳,LevelDatabase,CorrectDatabase和Incorrect数据库没有写入数据库。 这是我的代码:

import tkinter as tk

root=tk.Tk()
from tkinter import font
import time
import random
import datetime
import sqlite3

conn = sqlite3.connect('Results.db') # DATABASE
c = conn.cursor() # DATABASE
#Main Menu


helv12= font.Font(family='Helvetica', size=40, weight=font.BOLD)
helv13=font.Font(family='Helvetica', size=20, weight=font.BOLD)
root.configure(background="pink1")
root.title("MySight: Main Menu")
root.geometry("1368x766")
#root.iconbitmap('Final.ico')
photo=tk.PhotoImage(file="FinalFinalLogo.gif")
w=tk.Label(root, image=photo)
w.pack()


#Level1
class Levels:
        def __init__(self,master):
                frame = tk.Frame(master)
                frame.pack()
                self.Levels=tk.Button(frame, text="Levels", fg="white" ,bg="pink1", font=helv12, height=1, width=8, highlightthickness = 0, bd = 0, command=self.NewWindow)
                self.Levels.pack()
                global QuestionCounter
                QuestionCounter=0
                global Correct
                Correct = 0
                global Incorrect
                Incorrect = 0


        # DEFINES WHAT THE NEW WINDOW SHOULD LOOK LIKE
        def NewWindow(self):

                Window =tk.Toplevel(root)
                Window.geometry("800x1000")
                Window.configure(background="pink1")
                Window.title("Level 1")
                c.execute('CREATE TABLE IF NOT EXISTS ResultsStorage(timestamp TEXT, LevelDatabase TEXT, CorrectDatabase REAL, IncorrectDatabase REAL)') # DATABASE



                # LIST OF QUESTIONS
                Question1=("Question1")
                Question2=("Question2")
                Question3=("Question3")
                Question4=("Question4")
                Question5=("Question5")
                Question6=("Question6")
                Question7=("Question7")
                Question8=("Question8")
                Question9=("Question9")
                Question10=("Question10")
                # LIST OF ANSWERS
                Answer1=("A")
                Answer2=("B")
                Answer3=("C")
                Answer4=("D")
                Answer5=("E")
                Answer6=("F")
                Answer7=("G")
                Answer8=("C#")
                Answer9=("A#")
                Answer10=("B#")
                # DEFINITIONS OF VARIABLES
                Questions = [Question1,Question2,Question3,Question4,Question5,Question6,Question7,Question8,Question9, Question10]
                Answers = [Answer1, Answer2, Answer3, Answer4, Answer5, Answer6, Answer7, Answer8, Answer9, Answer10]
                RandomQuestion=(random.choice(Questions))
                LevelDatabase = ("1")





                # DEFINES LAYOUT OF NEW WINDOW
                Name = tk.Label(Window, text = "Level 1", fg="white", bg="pink1", font=helv13, height=1, width=20, highlightthickness=0, bd=0)
                Name.pack()
                label = tk.Label(Window, text=RandomQuestion, fg="white", bg="pink1", font=helv13, height=1, width=20, highlightthickness=0, bd=0)
                label.pack()
                Label2 = tk.Label(Window,fg="white", bg="pink1", font=helv13, height=1, width=70, highlightthickness=0, bd=0)
                Label2.pack()
                entry = tk.Entry(Window, width=70)
                entry.configure(background="pink1")
                entry.pack()




               # DEFINES FIRST LEVEL
                def Level1():
                        global QuestionCounter

                        UserInput = entry.get()# RANDOMLY PRINTS QUESTION


                        QuestionCounter= QuestionCounter+ 1
                        print ("You are on Question",QuestionCounter,)
                        if str(UserInput) in str(Answers) and len(UserInput): # AND IF THE ANSWER OR CAPITAL ANSWER IS THE SAME AS THE CORRECT ANSWER
                                global Correct                                               
                                Correct = Correct+ 1 # ADD ONE TO QUESTION COUNTER
                                print ("Correct")
                                entry.delete(0, tk.END)
                                label.config(text = RandomQuestion)



                        elif str(UserInput) not in str(Answers) and len (UserInput):
                                global Incorrect                                            
                                Incorrect = Incorrect+1
                                print ("Wrong")
                                print (QuestionCounter)
                                entry.delete(0,tk.END)

                        if QuestionCounter == 9:
                                        entry.delete(0, tk.END)
                                        label.config(text = "Correct: "+str(Correct) + " Incorrect:   "+str(Incorrect))

                                        if Correct>7:

                                                GoodJob=("You did a good job on Level 1.")
                                                Label2.config (text = str(GoodJob))
                                                file=open ("Results.txt","w")
                                                file.write (str(GoodJob))
                                                file.write("\n")
                                                file.write("You got"+str(Correct)+"correct")
                                                file.write("\n")
                                                file.write("You got"+str(Incorrect)+"incorrect")

                                        if Correct < 5:

                                                BadJob=("You did a bad job on Level 1")
                                                Label2.config (text = str(BadJob))
                                                file=open ("Results.txt","w")
                                                file.write(str(BadJob))
                                                file.write("\n")
                                                file.write("You got"+str(Correct)+"correct")
                                                file.write("\n")
                                                file.write("You got"+str(Incorrect)+"incorrect")


                def dynamic_data_entry(self):
                        global dynamic_data_entry
                        #this is what adds the data to the database. 
                        timestamp = str(datetime.datetime.now().date())
                        LevelDatabase = Window.Level
                        CorrectDatabase = Window.Correct
                        IncorrectDatabase = Window.Incorrect
                        c.execute("INSERT INTO ResultsStorage (timestamp, LevelDatabase, CorrectDatabase, IncorrectDatabase) VALUES (?, ?, ?, ?)",(timestamp, LevelDatabase, CorrectDatabase, IncorrectDatabase))
                        conn.commit()
                        self.writetodatabase()

                def writetodatabase(self):
                        for i in range(1):
                                time.sleep(1)
                        c.close()
                        conn.close()




                #NextLevel = tk.Button (Window, text = "Next Level", command= Level2, fg="white", bg="pink1",font=helv13, height=1, width=20, highlightthickness=0, bd=0)


                NextQuestion = tk.Button(Window,text = "Next Question",command = Level1, fg="white", bg="pink1", font=helv13, height=1, width=20, highlightthickness=0, bd=0)
                NextQuestion.pack()











class Results():
        def __init__(self,master):
                frame = tk.Frame(master)
                frame.pack()
                self.Levels=tk.Button(frame, text="Results", fg="white" ,bg="pink1", font=helv12, height=1, width=8, highlightthickness = 0, bd = 0, command=self.NewWindow)
                self.Levels.pack()
        def NewWindow(self):

                Window =tk.Toplevel(root)
                Window.geometry("1500x1500")
                Window.configure(background="pink1")
                self.connection = sqlite3.connect('Results.db')
                self.cur = self.connection.cursor()
                self.dateLabel = tk.Label(Window, text="Date", width=10, fg="white" ,bg="pink1", font=helv12, height=1,highlightthickness = 0, bd = 0,)
                self.dateLabel.grid(row=0, column=0)
                self.LevelLabel = tk.Label(Window, text = "Level", width=10,fg="white" ,bg="pink1", font=helv12, height=1, highlightthickness = 0, bd = 0,)
                self.LevelLabel.grid(row=0, column=1)
                self.CorrectLabel = tk.Label(Window, text="Correct", width=10,fg="white" ,bg="pink1", font=helv12, height=1, highlightthickness = 0, bd = 0,)
                self.CorrectLabel.grid(row=0, column=2)
                self.IncorrectLabel = tk.Label(Window, text="Incorrect", width=10,fg="white" ,bg="pink1", font=helv12, height=1, highlightthickness = 0, bd = 0,)
                self.IncorrectLabel.grid(row=0, column=3)
                self.showallrecords()
        def showallrecords(self):
                data = self.readfromdatabase()
                for index, dat in enumerate(data):
                    Label(self.master, text=dat[0]).grid(row=index+1, column=0)
                    Label(self.master, text=dat[1]).grid(row=index+1, column=1)
                    Label(self.master, text=dat[2]).grid(row=index+1, column=2)
                    Label(self.master, text=dat[3]).grid(row=index+1, column=3)

        def readfromdatabase(self):
                self.cur.execute("SELECT * FROM ResultsStorage")
                return self.cur.fetchall()





Quit = tk.Button(root, text="Exit",fg="white" ,bg="pink1", font=helv12, height=1, width=40, highlightthickness = 0, bd = 0, command=root.destroy)


#pack (add) the widget into the window
Levels(root)
#Practice(root)
Results(root)
Quit.pack()
root.mainloop #Run the thing

0 个答案:

没有答案