Hello,
I have created two classes (for now). One class is being used as the main (Testing), and the other is being used as a composition class (Login). When I attempted to run the main class, I had initially attempted to do it via the following lines:
if __name__ == "__main__":
app = Testing()
app.mainloop()
Doing it this way, it failed. After many iterations of different set ups, I finally tried the following way (adding the ‘tk’ in front of the mainloop()) and it appears to work … not sure why:
if __name__ == "__main__":
app = Testing()
tk.mainloop()
Not sure why it works this way. Can someone please give a valid explanation.
Another issue that I came across is that if I set the variable testing to ‘True’, the code appears to execute as expected. However, if I set it to ‘False’, then I get the following exception:
RuntimeError: Too early to run the main loop: no default root window
Here is my test code snippet:
import tkinter as tk
import datetime
# This list contains the authorized users to run tests on this test station.
authorizedUserList = {'James': 'CA456', 'Robert': 'CA841', 'Roger': 'CA341'}
class Login(tk.Tk):
def __init__(self):
super().__init__()
self.verified = False
tk.Tk.title(self,'PN: F4581-01 Test') # Window title (not real pn)
tk.Tk.geometry(self, '280x150')
login_info = tk.LabelFrame(self, padx = 15, pady = 10,
text = "Login Info")
tk.Label(login_info, text = "First name").grid(row = 0)
tk.Label(login_info, text = "Password").grid(row = 1)
self.username = tk.Entry(login_info)
self.username.grid(row = 0, column = 1, sticky = tk.W)
self.password = tk.Entry(login_info, show = '*')
self.password.grid(row = 1, column = 1, sticky = tk.W)
self.btn_start_test = tk.Button(self, text = "Start Test", command = self.verifyUser)
self.btn_start_test.bind("<Return>", self.verifyUser) # Add 'Enter' key as additional input
# to 'Start Test' button
login_info.pack(padx = 10, pady = 10)
self.btn_start_test.pack(padx = 10, pady = 10, side = tk.BOTTOM)
self.username.focus_set() # Put cursor in 'First Name' cell entry
def verifyUser(self, *args):
name = self.username.get().title()
password = self.password.get().upper()
if name in authorizedUserList:
if authorizedUserList[name] == password:
print('\nCredentials verified.') # for testing purposes
self.verified = True
else:
print('Unauthorized user! Try Again.')
self.clear_login()
#return self.verified
def clear_login(self):
self.username.delete(0, tk.END)
self.password.delete(0, tk.END)
class Testing:
def __init__(self):
#super().__init__()
testing = True
# This is the testing loop
while testing:
self.login = Login() # Instantiate class object 'Login' via composition
print(self.login)
#self.valid_user = self.login() #verifyUser().verified
print('Entered while loop and passed login')
#print(self.valid_user)
## if(self.login.verifyUser() == True):
##
## print('Test start time:', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
## print('\nBeging testing ...')
##
## else:
## print('Wrong credentials! Please try again.')
testing = False # Force a stop to the while loop
if __name__ == "__main__":
app = Testing()
tk.mainloop()
Any help / clarification would be appreciated.
By the way, to test the code snippet in the affirmative, you may type the following:
First name: james
Password: ca456
… this is from a combination as defined in the dictionary at the top.