#imports
from tkinter import *
from tkinter import messagebox as ms
import sqlite3
# make database and users (if not exists already) table at programme start up
with sqlite3.connect('logins.db') as db:
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEX NOT NULL);')
db.commit()
db.close()
#main Class
class main:
def __init__(self,master):
# Window
self.master = master
# Some Usefull variables
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
#Create Widgets
self.widgets()
#Login Function
def login(self):
with sqlite3.connect('logins.db') as db:
c = db.cursor()
#Find user If there is any take proper action
find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
c.execute(find_user,[(self.username.get()),(self.password.get())])
result = c.fetchall()
if result:
self.logf.pack_forget()
self.new()
else:
ms.showerror('Oops!','Username Not Found.')
def new_user(self):
#Establish Connection
with sqlite3.connect('logins.db') as db:
c = db.cursor()
#Find Existing username if any take proper action
find_user = ('SELECT * FROM user WHERE username = ?')
c.execute(find_user,[(self.username.get())])
if c.fetchall():
ms.showerror('Error!','Username Taken Try a Diffrent One.')
else:
ms.showinfo('Success!','Account Created!')
self.log()
#Create New Account
insert = 'INSERT INTO user(username,password) VALUES(?,?)'
c.execute(insert,[(self.n_username.get()),(self.n_password.get())])
db.commit()
#Frame Packing Methods
def log(self):
self.username.set('')
self.password.set('')
self.crf.pack_forget()
self.head['text'] = 'LOGIN'
self.logf.pack()
def cr(self):
self.n_username.set('')
self.n_password.set('')
self.logf.pack_forget()
self.head['text'] = 'Create Account'
self.crf.pack()
def new(self):
self.logf.pack_forget()
self.crf.pack_forget()
self.head['text'] = 'Welcome Back'
self.newf.pack()
def quiz(self):
self.newf.pack_forget()
self.head['text'] = 'Welcome to the psychology revision quiz'
self.quizf.pack()
self.Qn = 1
self.quizScore = 0
self.correctAnswer = '' # <-- create it at start (and use better name)
self.update_question_number() # <-- get question number
self.update_question() # <-- get new question
def update_question_number(self):
# Get question's number
query = "SELECT MAX(qnumber) FROM questions"
cursor.execute(query)
row = cursor.fetchone()
self.recordNum['text'] = row[0]
def update_question(self):
# Get new question
query = "SELECT * FROM questions WHERE qnumber=?"
cursor.execute(query, (self.Qn,))
row = cursor.fetchone()
self.question['text'] = row[0]
self.answer1['text'] = row[1]
self.answer2['text'] = row[2]
self.answer3['text'] = row[3]
self.answer4['text'] = row[4]
self.correctAnswer = row[5]
def confirmAnswer(self):
if self.enterAnswer == self.correctAnswer:
self.rightOrWrong['text'] = "Correct"
self.quizScore += 1
else:
self.rightOrWrong['text'] = "Incorrect"
if self.Qn < self.recNum:
self.Qn += 1 # <-- get new question
self.update_question() # <-- get new question
else:
self.rightOrWrong['text'] = "Quiz Complete! Your score was: {}".format(self.quizScore)
#Draw Widgets
def widgets(self):
self.head = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
self.head.pack()
self.logf = Frame(self.master,padx =10,pady = 10)
Label(self.logf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.logf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
Button(self.logf,text = ' Create Account ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.cr).grid(row=2,column=1)
self.logf.pack()
self.crf = Frame(self.master,padx =10,pady = 10)
Label(self.crf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.crf,textvariable = self.n_username,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.crf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
Button(self.crf,text = 'Create Account',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.new_user).grid()
Button(self.crf,text = 'Go to Login',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.log).grid(row=2,column=1)
self.newf = Frame(self.master,padx =12,pady = 12)
Button(self.newf,text = 'Start New Quiz',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.quiz).grid()
Button(self.newf,text = 'Progress',bd = 3 ,font = ('',15),padx=5,pady=5).grid()
self.quizf = Frame(self.master,padx =10,pady = 10)
# v-- create empty labels
self.recordNum = Label(quizf, text='')
self.recordNum.pack()
self.question = Label(self.quizf, text='')
self.question.pack()
self.answer1 = Label(self.quizf, text='')
self.answer1.pack()
self.answer2 = Label(self.quizf, text='')
self.answer2.pack()
self.answer3 = Label(self.quizf, text='')
self.answer3.pack()
self.answer4 = Label(self.quizf, text='')
self.answer4.pack()
if __name__ == '__main__':
#Create Object
#and setup window
root = Tk()
root.title('Login Form')
root.geometry('400x350+300+300')
main(root)
root.mainloop()
我正在创建一个tkinter / SQlite代码,该代码使用交互式GUI进行“心理学修订测验”。对于python中的tkinter,我是一个业余爱好者。
但无论如何,这是我得到的错误:
在处理上述异常期间,发生了另一个异常:
追踪(最近一次通话): 文件“C:\ Python34 \ lib \ tkinter__init __。py”,第1533行,调用 return self.func(* args) 文件“S:\ year 13 \ computing project \ project \ MAIN PROJECT CODE.py”,第91行,在测验中 self.update_question_number()#&lt; - 获取问题编号 AttributeError:'main'对象没有属性'update_question_number'
“update_question”也会发生此错误,该错误位于“update_question_number”之下。
我包含了整个代码,所以它可以由你们中的任何人进行测试,如果遇到困惑我很抱歉,但我试图让结构尽可能理解,并提供一些评论来帮助。我不知道如何解决这个错误,但我确定它可以轻松修复它的愚蠢。谢谢,伙计们:))