Sorry if that isn't a very helpful title, but it's the best way I describe my problem. When I intentionally put the password in wrong 3 times, what should happen is a popup should appear I got the password wrong too many times then the window should quit and the program should end. What actually happens is the popup appears just fine, but the tkinter window doesn't quit and stays there instead. For some reason I think the quit() function doesn't like my while loops. I put "scope problems" because I'm not sure "authorised" always equals what it's meant to or what I think it should. Any suggestions?
import tkinter as tk
from tkinter import messagebox
import random
username1 = "player1"
password1 = "player1pass"
username2 = "player2"
password2 = "player2pass"
authorised = False
failed = False
attempts = 0
playerslogin = tk.Tk()
playerslogin.title("Authentication")
playerslogin.geometry("325x110")
labeluser1 = tk.Label(playerslogin, text="Enter the first player's username:")
labeluser1.grid(row=0)
labelpass1 = tk.Label(playerslogin, text="Enter the first player's password:")
labelpass1.grid(row=1)
labeluser2 = tk.Label(playerslogin, text="Enter the second player's username:")
labeluser2.grid(row=2)
labelpass2 = tk.Label(playerslogin, text="Enter the second player's password:")
labelpass2.grid(row=3)
authfail = tk.Label(playerslogin, text="")
authfail.grid(row=4)
while(authorised == False and failed == False):
def checkLogin():
user1 = e1.get()==username1
pass1 = e2.get()==password1
user2 = e3.get()==username2
pass2 = e4.get()==password2
if (user1 and user2 and pass1 and pass2):
global authorised
authorised = True
playerslogin.quit()
else:
global attempts
attempts = attempts + 1
if attempts == 3:
playerslogin.quit()
messagebox.showerror(title="Attempts exceeded", message="You have used all 3 attempts.")
failed == True
else:
authfail.config(text = f"Login failed. Attempts left: {3-attempts}")
e1 = tk.Entry(playerslogin)
e2 = tk.Entry(playerslogin, show = "*")
e3 = tk.Entry(playerslogin)
e4 = tk.Entry(playerslogin, show = "*")
e5 = tk.Button(playerslogin, text = "Enter", command = checkLogin)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
e5.grid(row=4, column=1)
playerslogin.mainloop()
#Login/authorisation code ends here, this is where the game begins
#Creating a deck and preparing the game
class Card:
def __init__(self, colour, number):
self.colour = colour
self.number = number
def getColour(self):
return self.colour
def getNumber(self):
return self.number
#Cards are of colours Red, Yellow and Black, and go from 1 to 10
r1 = Card("Red", 1)
r2 = Card("Red", 2)
r3 = Card("Red", 3)
r4 = Card("Red", 4)
r5 = Card("Red", 5)
r6 = Card("Red", 6)
r7 = Card("Red", 7)
r8 = Card("Red", 8)
r9 = Card("Red", 9)
r10 = Card("Red", 10)
y1 = Card("Yellow", 1)
y2 = Card("Yellow", 2)
y3 = Card("Yellow", 3)
y4 = Card("Yellow", 4)
y5 = Card("Yellow", 5)
y6 = Card("Yellow", 6)
y7 = Card("Yellow", 7)
y8 = Card("Yellow", 8)
y9 = Card("Yellow", 9)
y10 = Card("Yellow", 10)
b1 = Card("Black", 1)
b2 = Card("Black", 2)
b3 = Card("Black", 3)
b4 = Card("Black", 4)
b5 = Card("Black", 5)
b6 = Card("Black", 6)
b7 = Card("Black", 7)
b8 = Card("Black", 8)
b9 = Card("Black", 9)
b10 = Card("Black", 10)
deck = [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10]
#function to check who wins
def checkwinner(card1, card2):
colour1 = card1.getColour()
number1 = card1.getNumber()
colour2 = card2.getColour()
number2 = card2.getNumber()
if (colour1 == colour2):
if (number1 > number2):
return "player1"
else:
return "player2"
else:
if (colour1 == "Red"):
if (colour2 == "Black"):
return "player1"
else:
return "player2"
elif (colour1 == "Yellow"):
if (colour2 == "Red"):
return "player1"
else:
return "player2"
elif (colour1 == "Black"):
if (colour2 == "Yellow"):
return "player1"
else:
return "player2"
player1deck = []
player2deck = []
#game GUI goes here
while (authorised):
shuffleddeck = []
shuffleddeck = deck
random.shuffle(shuffleddeck)
maingame = tk.Tk()
maingame.title("Louise's Card Game")
maingame.geometry("1000x950")
maingame.mainloop()