Neil18
Badges: 2
Rep:
?
#1
Report Thread starter 1 year ago
#1
#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()


#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.pass word.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.quizScore = 0
self.correctAnswer = '' # <-- create it at start (and use better name)
self.Qn = 1
self.update_question()# <-- get new question
self.update_question_number()
def update_question_number(self):
# Get question's number

query = "SELECT MAX(qnumber) FROM questions"
c.execute(query)
row = c.fetchone()

self.recordNum['text'] = row[0]

def update_question(self):
# Get new question

query = "SELECT * FROM questions WHERE qnumber=?"
c.execute(query, (self.Qn,))
row = c.fetchone()

self.question['text'] = row[1]

self.answer1['text'] = row[2]
self.answer2['text'] = row[3]
self.answer3['text'] = row[4]
self.answer4['text'] = row[5]

self.correctAnswer = row[6]

def confirmAnswer(self):
self.rightOrWrong = self.enterAnswer
if self.enterAnswer == self.correctAnswer:
self.rightOrWrong['text'] = "Correct"
self.quizScore += 1
self.update_question()
else:
self.rightOrWrong['text'] = "Incorrect"

if self.Qn < self.recordNum:
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(stic ky = 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(stic ky = 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=se lf.login).grid()
Button(self.logf,text = ' Create Account ',bd = 3 ,font = ('',15),padx=5,pady=5,command=se lf.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(stic ky = 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(stic ky = 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=se lf.new_user).grid()
Button(self.crf,text = 'Go to Login',bd = 3 ,font = ('',15),padx=5,pady=5,command=se lf.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=se lf.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(self.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()

self.enterAnswer = Entry(self.quizf, textvariable = self.confirmAnswer)
self.Submit = Button(self.quizf,text = "submit", command = self.confirmAnswer)
self.enterAnswer.pack()
self.Submit.pack()


if __name__ == '__main__':
#Create Object
#and setup window
root = Tk()
root.title('Login Form')
root.geometry('400x350+300+300')
main(root)
root.mainloop()

On line 126 it comes up with this error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init_ _.py", line 1533, in __call__
return self.func(*args)
File "S:\year 13\computing project\project\MAIN PROJECT CODE.py", line 126, in confirmAnswer
if self.Qn < self.recordNum:
TypeError: unorderable types: int() < Label()
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
Unfortunately, TSR has lost all of the formatting of your code, which is a problem with Python because indentation is important!

TSR forums aren't very good at handling code which is pasted into the forum, whenever you want to paste some code here, then it's easier to paste into somewhere like https://repl.it and post the link instead

As for your problem, the exception provides a pretty good clue:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__i nit_ _.py", line 1533, in __call__
return self.func(*args)
File "S:\year 13\computing project\project\MAIN PROJECT CODE.py", line 126, in confirmAnswer
if self.Qn < self.recordNum:
TypeError: unorderable types: int() < Label()
It's saying that your problem is on Line 126, furthermore, it's saying that the problem is this line:
Code:
if self.Qn < self.recordNum:
And it gives you a description of the error:
Code:
TypeError: unorderable types: int() < Label()
The documentation for TypeError is found at this link: https://docs.python.org/3/library/exceptions.html
Also, you can find a lot of other people who have had this error before to get a good idea what it's telling you: https://www.google.co.uk/search?q=py...rderable+types

Your expression compares self.Qn (which it has identified as an int) using the less-than (<) operator, against self.recordNum (which it has identified as a Label - which seems to be the Label from the tkinter library). Python doesn't know how to compare an int with a Label.

Here's the documentation for the tkinter label:
https://docs.python.org/3/library/tk...#label-options

If you want the text contained in the label, then you need to use the 'text' option to read the text as a str. i.e.
Code:
self.recordNum['text']
Although, you can't compare a str to an int either, but you can convert them. e.g.
Code:
int(self.recordNum['text'])
Of course, that will fail if recordNum['text'] doesn't contain an int. So you might also want to check that the label text really does contain an integer as well

If you only care about positive integers, then you can use isdigit - https://docs.python.org/3/library/st...ml#str.isdigit
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • The University of Law
    The Bar Series: Applications and Interviews - London Bloomsbury campus Postgraduate
    Thu, 17 Oct '19
  • Cardiff Metropolitan University
    Undergraduate Open Day - Llandaff Campus Undergraduate
    Sat, 19 Oct '19
  • Coventry University
    Undergraduate Open Day Undergraduate
    Sat, 19 Oct '19

Why wouldn't you turn to teachers if you were being bullied?

They might tell my parents (8)
7.08%
They might tell the bully (15)
13.27%
I don't think they'd understand (19)
16.81%
It might lead to more bullying (39)
34.51%
There's nothing they could do (32)
28.32%

Watched Threads

View All