Okay, so basically, I'm trying to create a sign-in system using python3 and the toolkit interface. I would like the data to be "sent on" after the user presses the enter key. Yet, for some reason, upon running the code (it's run from a separate main.py file which isn't included below, but it's a simple function call, and I highly doubt it's the source of the error), the enterPressed function is called, without any keys having been pressed. Because nothing is in the entry box at run, it causes an error to show, which is quite annoying. Why is the function being called, and how can I prevent it from being called until the enter/return keys are pressed? If it helps, I'm running the code on the latest version of Ubuntu (18.04) with python3.6.6
Here's the code:
#___________________________________________________________
#Last Updated by _________ on September 30, 2018
#V1.0.0
#mainWindow.py
#Import tkinter to create the GUI:
from tkinter import *
#For creating message boxes:
import tkinter.messagebox as popup
#Import any other necessary, non-specific modules:
import pickle , os
#Declare all of the global variables:
firstRun = True
#KEY: windowAttsList = [window title , window resolution , make window fullscreen , remove toolbar , stop alt + f4 from working , window background color , main heading text , main heading background color , main heading foreground color , main heading typeface , main heading font size , id prompt text , id prompt background color , id prompt foreground color , id prompt typeface , id prompt font size , entry box input length , entry box typeface entry box font size , entry box background color , entry box foreground color , hide entry , allow changing input hiding setting , button background color , button foreground color , button active background color , button active foreground color]
#Default Values:
windowAttsList = ['___ Sign In' , '1080x720' , False , False , False , '#000000' , 'BHS Library Sign In' , '#000000' , '#FF5000' , 'Times New Roman' , 70 , 'Please Enter Your Student I.D. Below:' , '#000000' , '#FFFFFF' , 'Times New Roman' , 25 , 4 , 'Times New Roman' , 250 , '#FFFFFF' , '#000000' , True , True , '#0061FF' , '#FFFFFF' , '#FF5000' , '#000000']
#Updates all of the design variables for the window:
def updateWindowAtts(save = False , newAttsToSave = []):
global windowAttsList
#Check for a custom design file; if none exists, create one with the default values:
if (not(os.path.exists('Data'))):
os.mkdir('Data')
if (save):
with open('Data/mainWindowAtts.dat' , 'wb') as windowAttsFile:
pickle.dump(newAttsToSave , windowAttsFile)
return 0
if (not(os.path.isfile('Data/mainWindowAtts.dat'))):
#Use a with statement to ensure that the file automatically gets closed, even if an exception is thrown:
with open('Data/mainWindowAtts.dat' , 'wb') as windowAttsFile:
pickle.dump(windowAttsList , windowAttsFile)
with open('Data/mainWindowAtts.dat' , 'rb') as windowAttsFile:
windowAttsList = pickle.load(windowAttsFile)
#Define a function to enable/disable the private mode feature, that obscures the input:
def enablePrivateMode(box):
if (windowAttsList[21]):
box.configure(show = '')
else:
box.configure(show = '*')
#Define a function to be called once the enter key is pressed:
This is the function that is getting called as soon as the startWindow() function is called, despite no keys having been pressed:
def enterPressed(enteredDataSource):
enteredData = enteredDataSource.get()
try:
enteredData = int(enteredData)
except:
This is the error message that is showing as soon as I run the code; it's triggered because enteredDataSource.get() returns an empty string, as no data had been entered yet.
popup.showerror('Invalid Input' , 'Error\nThe Input You Have Entered Is Not Valid.\nPlease Make Sure You Are Entering Only Integers.')
return [False]
if (len(enteredData) != (windowAttsList[16])):
popup.showerror('Invalid Input' , 'Error\nThe Input You Have Entered Is Not Valid.\nPlease Make Sure The Length Of Your Input Is ' + str(windowAttsList[16]) + ' Integers.')
return [False]
return [True , enteredData]
#Creates and launches the GUI:
def startWindow():
#If this is the first run, force an attributes check and update:
global firstRun
if (firstRun):
updateWindowAtts()
firstRun = False
#Create the GUI window object:
window = Tk()
#Title the window:
window.title(windowAttsList[0])
#Determine the specific size of the window:
window.geometry(windowAttsList[1])
#Make the window fullscreen:
if (windowAttsList[2]):
window.attributes('-fullscreen', True)
#Disable/Remove the toolbar, allowing for window closing, resizing, minimizing, etc.:
if (windowAttsList[3]):
window.overrideredirect(True)
#Stop alt + f4 from working to close the window:
if (windowAttsList[4]):
window.protocol('WM_DELETE_WINDOW', passRequest)
#Set the window background color:
window.configure(bg = windowAttsList[5])
#Create and pack a heading to display the title of the program:
heading1 = Label(window , text = windowAttsList[6] , bg = windowAttsList[7] , fg = windowAttsList[8] , font = (windowAttsList[9] , windowAttsList[10]))
heading1.pack(side = 'top' , pady = 10)
#Add a text label asking for input:
enterIDLabel = Label(window , text = windowAttsList[11] , bg = windowAttsList[12] , fg = windowAttsList[13] , font = (windowAttsList[14] , windowAttsList[15]))
enterIDLabel.pack(side = 'top' , pady = 10)
#Add an input box to enter the student I.D. number into:
studentIDBox = Entry(window , width = windowAttsList[16] , font = (windowAttsList[17] , windowAttsList[18]) , bg = windowAttsList[19] , fg = windowAttsList[20])
if (windowAttsList[21]):
studentIDBox.configure(show = '*')
else:
studentIDBox.configure(show = '')
studentIDBox.pack(side = 'top' , pady = 125)
#Make the box automatically selected, and ready for text entry:
studentIDBox.focus_set()
#Add a button allowing for switching in and out of private mode:
if (windowAttsList[22]):
if (windowAttsList[21]):
privateModeButtonText = 'Show Input'
else:
privateModeButtonText = 'Hide Input'
privateModeButton = Button(window , text = privateModeButtonText , bd = 0 , bg = windowAttsList[23] , fg = windowAttsList[24] , activebackground = windowAttsList[25] , activeforeground = windowAttsList[26] , command = lambda: enablePrivateMode(studentIDBox))
privateModeButton.place(x = 0 , y = 0)
#Bind the return key, so that when it is pressed, the appropriate function will be called:
This is where the keys are bound:
window.bind('<Return>' , enterPressed(studentIDBox))
window.bind('<KP_Enter>' , enterPressed(studentIDBox))
#Run the event listener main window loop:
window.mainloop()
P.S. Feel free to correct any terminology used in the comments if it's not technically correct or accurate. I know I may have used some terms incorrectly, and I always appreciate constructive criticism. :)