Python .insert()函数不适用于我的输入框
我正在编写一个函数,它将输入框小部件作为参数,配置输入框(更改文本的颜色),然后将一些文本插入输入框。除了输入框中没有显示文本外,Evrythig工作正常 功能如下:Python .insert()函数不适用于我的输入框,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在编写一个函数,它将输入框小部件作为参数,配置输入框(更改文本的颜色),然后将一些文本插入输入框。除了输入框中没有显示文本外,Evrythig工作正常 功能如下: def entry_error(self, entry): entry.delete(0, tk.END) entry.config(fg = "red") entry.insert(0, "Data Invalid!") 对该函数的调用是: if len(firstName) =
def entry_error(self, entry):
entry.delete(0, tk.END)
entry.config(fg = "red")
entry.insert(0, "Data Invalid!")
对该函数的调用是:
if len(firstName) == 0: # If no data is entered for the first name
dataValid = False # The data is invalid
self.entry_error(self.firstNameEntry) ### <-- CALL TO FUNCTION ###
如果len(firstName)==0:#如果没有为名字输入数据
dataValid=False#数据无效
self.entry#u错误(self.firstNameEntry)###请尝试将您的代码缩减为一个错误。我们真的只需要一个条目,您遇到问题的函数,以及足够的代码来运行它。我已经尝试过简化它,但是这样做之后,问题就解决了。我无法确定这种情况是如何发生的,因为我只在出现上述代码时才发现问题。插入文本后,我尝试使用.get()函数打印条目的内容,并打印数据,因此插入工作正常,但文本未显示在条目中,请重试。我不敢相信,复制这一点的唯一方法需要十几个条目小部件、标签和intvars,再加上几十行验证代码。
'''
Variable Name = variableName
Function Name = function_name
'''
import string # Importing the string module
import random # Importing the random module
import sqlite3 # Importing sqlite3
import tkinter as tk # Importing the tkinter module to create a GUI
from tkinter import messagebox # Importing the messagebox class from the tkinter module
from tkinter import filedialog # Importing the filedialog class from the tkinter module
from shutil import copyfile # Importing the copyfile class from the shutil module
# Class To Create The Main Window #
class window(tk.Tk): # Defining the class (window) which inherits the properties of the tkinter class Tk
def __init__(self): # Defining the initial function that runs every time an instance of this class is made
tk.Tk.__init__(self) # Running the __init__() function of the Tk class to create the new window
self.screenWidth = self.winfo_screenwidth() # Sets the variable screenWidth to the width of the users screen
self.screenHeight = self.winfo_screenheight() # Sets the variable screenHeight to the height of the users screen
self.geometry("%sx%s" % (self.screenWidth, self.screenHeight)) # Sets the width and height of the main window to the width and height of the users screen
self.overrideredirect(1) # Removes the border from the window
self.existingFrames = {} # Creating a dictionay to store the frames that currently exist
''' All New Frame classes need to be added to the list below '''
self.frameNames = [loginPage, registerPage, termsOfServicePage] # Creating a lsit of names of all the frames linked to this class
for frameName in self.frameNames: # Iterating through the list of names of frames
self.existingFrames[frameName] = frameName(self) # Creating a new element in the dictionary where the frame name is the key and the frame pointer is the value
self.existingFrames[frameName].grid(row = 0, column = 0) # Adding the frame to the windows grid
self.existingFrames[loginPage].lift() # Bringing the login screen to the top so that it is visible by the user
def show_frame(self, frameToShow): # Creating a function in the class that takes the name of the frame to display
frame = self.existingFrames[frameToShow] # Gets the value from the dictionary of the frame that is passed through as frameToShow
frame.lift() # Raises the frame to the top so that it is visible by the user
def close_window(self): # Function to close the window when called
closeWindow = messagebox.askyesno("Close Window?", "Are you sure you want to close the window?\nAll unsaved data will be lost!") # Asks the user if they definitely want to close the window and stores the result under the variable closeWindow
if closeWindow == True: # If the user answeres yes
self.destroy() # The window gets destroyed
exit() # The program is exited
# Class To Create The Login Page #
class loginPage(tk.Frame): # Defining the class (loginPage) which inherits the properties of the tkinter Frame class
def __init__(self, parentClass): # Defining the initial function that runs every time an instance of this class is made
self.parentClass = parentClass # Creating a variable to store the pointer to the parent class that can be used by all function in this class
tk.Frame.__init__(self, self.parentClass, width = self.parentClass.screenWidth, height = self.parentClass.screenHeight) # Creates a Frame inside the main window passed through as rootWidnow
self.exitButton = tk.Button(self, text = "X", fg = "red", font = ("", 10), command = self.parentClass.close_window) # Creates an exit button in the Frame with the main window's close_windwo() command
self.exitButton.place(anchor = tk.NE, x = self.parentClass.screenWidth, y = 0) # Places the exit button in the top right corner of the frame
self.usernamelabel = tk.Label(self, text = "Username:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their username
self.usernamelabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 5) * 2, y = (self.parentClass.screenHeight / 12) * 5) # Placing the label in the frame
self.passwordlabel = tk.Label(self, text = "Password:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their password
self.passwordlabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 5) * 2, y = (self.parentClass.screenHeight / 12) * 6) # Placing the label in the frame
self.usernameEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their username
self.usernameEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 5) * 3, y = (self.parentClass.screenHeight / 12) * 5) # Placing the entry next to usernamelabel in the frame
self.passwordEntry = tk.Entry(self, font = ("", 12), show = "*") # Creating an entry for the user to enter their password (A * will be shown instead of plain text for security)
self.passwordEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 5) * 3, y = (self.parentClass.screenHeight / 12) * 6) # Placing the entry next to passowrdlabel in the frame
self.loginButton = tk.Button(self, text = "Login", font = ("Cooper Black", 17), command = self.submit_data) # Creating a button for the user to submit their login details
self.loginButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 2), y = (self.parentClass.screenHeight / 12) * 7) # Placing the button in the frame
self.newAccountButton = tk.Button(self, text = "Dont Have An Account!", font = ("Cooper Black", 13), command = self.no_account) # Creating a button for the user to go to the screen to register a new account
self.newAccountButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 2), y = (self.parentClass.screenHeight / 12) * 8) # Placing the button in the frame
def submit_data(self): # Creating a function to submit the users login details (parameters: the entries used for the username and password)
username = self.usernameEntry.get() # geting the username from the username entry
password = self.passwordEntry.get() # getting the password from the passoword entry
self.usernameEntry.delete(0, tk.END) # Deleting the text in the entry
self.passwordEntry.delete(0, tk.END) # Deleting the text in the entry
# VALIDATE DATA #
def no_account(self): # Creating a function to take the user to the register screen if they do not have an account
self.usernameEntry.delete(0, tk.END) # Deleting the text in the entry
self.passwordEntry.delete(0, tk.END) # Deleting the text in the entry
self.parentClass.show_frame(registerPage) # Switches the screen visible by the user to the register screen
# Class To Create The Register Page #
class registerPage(tk.Frame): # Defining the class (registerPage) which inherits the properties of the tkinter Frame class
def __init__(self, parentClass): # Defining the initial function that runs every time an instance of this class is made
self.parentClass = parentClass # Creating a variable to store the pointer to the parent class that can be used by all function in this class
tk.Frame.__init__(self, self.parentClass, width = self.parentClass.screenWidth, height = self.parentClass.screenHeight) # Creates a Frame inside the main window passed through as rootWidnow
self.exitButton = tk.Button(self, text = "X", fg = "red", font = ("", 10), command = self.parentClass.close_window) # Creates an exit button in the Frame with the main window's close_windwo() command
self.exitButton.place(anchor = tk.NE, x = self.parentClass.screenWidth, y = 0) # Places the exit button in the top right corner of the frame
self.firstNameLabel = tk.Label(self, text = "First Name:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their first name
self.firstNameLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 3, y = (self.parentClass.screenHeight / 9) * 1) # Placing the label in the frame
self.firstNameEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their first name
self.firstNameEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 7, y = (self.parentClass.screenHeight / 9) * 1) # Placing the entry in the frame
self.surnameLabel = tk.Label(self, text = "Surname:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their surname
self.surnameLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 3, y = (self.parentClass.screenHeight / 9) * 2) # Placing the label in the frame
self.surnameEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their surname
self.surnameEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 7, y = (self.parentClass.screenHeight / 9) * 2) # Placing the entry in the frame
self.emailLabel = tk.Label(self, text = "Email:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their email
self.emailLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 3, y = (self.parentClass.screenHeight / 9) * 3) # Placing the label in the frame#
self.emailEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their email
self.emailEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 7, y = (self.parentClass.screenHeight / 9) * 3) # Placing the entry in the frame
self.confirmEmailLabel = tk.Label(self, text = "Confirm Email:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their email
self.confirmEmailLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 3, y = (self.parentClass.screenHeight / 9) * 4) # Placing the label in the frame
self.confirmEmailEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their confirm email
self.confirmEmailEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 7, y = (self.parentClass.screenHeight / 9) * 4) # Placing the entry in the frame
self.usernameLabel = tk.Label(self, text = "Username:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their first name
self.usernameLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 13, y = (self.parentClass.screenHeight / 9) * 1) # Placing the label in the frame
self.usernameEntry = tk.Entry(self, font = ("", 12)) # Creating an entry for the user to enter their first name
self.usernameEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 17, y = (self.parentClass.screenHeight / 9) * 1) # Placing the entry in the frame
self.passwordLabel = tk.Label(self, text = "Password:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their surname
self.passwordLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 13, y = (self.parentClass.screenHeight / 9) * 2) # Placing the label in the frame
self.passwordEntry = tk.Entry(self, font = ("", 12), show = "*") # Creating an entry for the user to enter their surname
self.passwordEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 17, y = (self.parentClass.screenHeight / 9) * 2) # Placing the entry in the frame
self.confirmPasswordLabel = tk.Label(self, text = "Confirm Password:", font = ("Cooper Black", 15)) # Creating a label to instruct the user to enter their email
self.confirmPasswordLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 13, y = (self.parentClass.screenHeight / 9) * 3) # Placing the label in the frame#
self.confirmPasswordEntry = tk.Entry(self, font = ("", 12), show = "*") # Creating an entry for the user to enter their email
self.confirmPasswordEntry.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 17, y = (self.parentClass.screenHeight / 9) * 3) # Placing the entry in the frame
self.termsOfServiceVar = tk.IntVar() # Creating an Integer variable to store the status of the terms of service check button
self.termsOfServiceCheckButton = tk.Checkbutton(self, text = "I agree to the terms of service", variable = self.termsOfServiceVar) # Creating a check button for the user to accept the terms of service
self.termsOfServiceCheckButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 13, y = (self.parentClass.screenHeight / 9) * 4) # Places the check button in the frame
self.termsOfServiceCheckButton.bind("<Button 1>", self.show_terms_of_service) # Runs the show_terms_of_service function when the check button is clicked
self.recieveEmailsVar = tk.IntVar() # Creating an Integer variable to store the status of the email preference check button
self.recieveEmailsCheckButton = tk.Checkbutton(self, text = "I agree to recieve promotional emails", variable = self.recieveEmailsVar) # Creates a check button for the user to agree to recieve promotinal emails
self.recieveEmailsCheckButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 17.5, y = (self.parentClass.screenHeight / 9) * 4) # Places the check button in the frame
self.registerButton = tk.Button(self, text = "Register", font = ("Cooper Black", 17), command = self.submit_data) # Creating a button for the user to submit their data
self.registerButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 10, y = (self.parentClass.screenHeight / 9) * 5.25)
self.hasAccountButton = tk.Button(self, text = "Already Have An Account!", font = ("Cooper Black", 13), command = self.has_account) # Creating a button for the user to go to the login screen
self.hasAccountButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 10, y = (self.parentClass.screenHeight / 9) * 6) # Placing the button in the frame
self.dividerLine = tk.Label(self, text = "_" * self.parentClass.screenWidth, font = ("Cooper Black", 15)) # Creating a line to divide the screen into sections
self.dividerLine.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 10, y = (self.parentClass.screenHeight / 9) * 7) # Placing the label in the frame
self.loginAsGuestButton = tk.Button(self, text = "Continue As Guest", font = ("Cooper Black", 13), command = self.login_as_guest) # Creating a button for the user to go to sign in as a guest
self.loginAsGuestButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 20) * 10, y = (self.parentClass.screenHeight / 9) * 8) # Placing the button in the frame
def empty_entries(self): # Creating a function to empty the data from the etnry boxes
entries = [self.firstNameEntry, self.surnameEntry, self.emailEntry, self.confirmEmailEntry, self.usernameEntry, self.passwordEntry, self.confirmPasswordEntry] # List of entries in the register screen
for entry in entries: # Iterates through the list of entries
entry.delete(0, tk.END) # Deletes the text in the entry
self.termsOfServiceVar.set(0) # Resetting the check button
self.recieveEmailsVar.set(0) # Resetting the check button
def entry_error(self, entry):
entry.delete(0, tk.END)
entry.config(fg = "red")
entry.insert(0, "Data Invalid!")
entry.bind("<Button 1>")#, lambda event: self.remove_entry_error(entry))
def remove_entry_error(self, entry):
entry.delete(0, tk.END)
entry.config(fg = "black")
entry.unbind("<Button 1>")
def has_account(self):
self.empty_entries()
self.parentClass.show_frame(loginPage) # Switches the screen visible by the user to the login screen
def submit_data(self):
firstName = self.firstNameEntry.get() # getting the data from the entry box and storing it under a variable
surname = self.surnameEntry.get() # getting the data from the entry box and storing it under a variable
email = self.emailEntry.get() # getting the data from the entry box and storing it under a variable
confirmEmail = self.confirmEmailEntry.get() # getting the data from the entry box and storing it under a variable
username = self.usernameEntry.get() # getting the data from the entry box and storing it under a variable
password = self.passwordEntry.get() # getting the data from the entry box and storing it under a variable
confirmPassword = self.confirmPasswordEntry.get() # getting the data from the entry box and storing it under a variable
termsOfServiceStatus = self.termsOfServiceVar.get() # getting the data from the check button and storing it under a variable
recieveEmailsStatus = self.recieveEmailsVar.get() # getting the data from the check button and storing it under a variable
# Validating Data #
dataValid = True # Variable to store whether or not the data is valid. Data stays valid untill found invalid
if len(firstName) == 0: # If no data is entered for the first name
dataValid = False # The data is invalid
self.entry_error(self.firstNameEntry) # Runs the function to display an error in the data to the user
if len(surname) == 0: # If no data is entered for the surname
dataValid = False # The data is invalid
if "@" not in email or "." not in email: # If the email does not contain both an '@' and a '.'
dataValid = False # The data is invalid
if confirmEmail != email: # If both emails do not match
dataValid = False # The data is invalid
if " " in username: # If the username contains a space
dataValid = False # the data is invalid
if len(password) < 8: # If the password is less than 8 characters
dataValid = False # The data is invalid
else:
uppercase = False # Variable to store if the password contains an uppercase letter
lowercase = False # Variable to store if the password contains an lowercase letter
punctuation = False # Variable to store if the password contains a special character
digit = False # Variable to store if the password contains a digit
for char in password: # Iterates through each character in the password
if char in string.ascii_uppercase: # If the character is an uppercase letter
uppercase = True # The uppercase variable is true
elif char in string.ascii_lowercase: # If the character is a lowercase letter
lowercase = True # The lowercase variable is true
elif char in string.punctuation: # If the character is a special character
punctuation = True # The punctuation variable is true
elif char in string.digits: # If the character is a digit
digit = True # The digit variable is true
if uppercase == False or lowercase == False or punctuation == False or digit == False: # If any of the 4 variables are False
dataValid = False # The data is invalid
if confirmPassword != password: # If the two passwords dont match
dataValid = False # The data is invalid
self.empty_entries()
def login_as_guest(self):
pass
def show_terms_of_service(self, *args): # Function to show the terms of service screen
self.termsOfServiceCheckButton.unbind("<Button 1>") # Unbinds Button 1 from the widget
self.parentClass.show_frame(termsOfServicePage) # Calls the function to display the terms of service screen
# Class To Create The Terms Of Service Page #
class termsOfServicePage(tk.Frame):
def __init__(self, parentClass):
self.parentClass = parentClass
tk.Frame.__init__(self, self.parentClass, width = self.parentClass.screenWidth, height = self.parentClass.screenHeight)
self.exitButton = tk.Button(self, text = "X", fg = "red", font = ("", 10), command = lambda: self.exit_screen(False))
self.exitButton.place(anchor = tk.NE, x = self.parentClass.screenWidth, y = 0)
self.titleLabel = tk.Label(self, text = "Terms Of Service", font = ("Cooper Black", 20))
self.titleLabel.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 2), y = (self.parentClass.screenHeight / 11) * 1)
self.acceptButton = tk.Button(self, text = "Accept Terms Of Service", font = ("Cooper Black", 17), command = lambda: self.exit_screen(True))
self.acceptButton.place(anchor = tk.CENTER, x = (self.parentClass.screenWidth / 2), y = (self.parentClass.screenHeight / 11) * 10)
def exit_screen(self, status):
if status == True:
self.parentClass.existingFrames[registerPage].termsOfServiceVar.set(1)
else:
self.parentClass.existingFrames[registerPage].termsOfServiceVar.set(0)
self.parentClass.show_frame(registerPage)
mainWindow = window() # Creating an instance of the window class
mainWindow.mainloop() # Calling the mainloop function on mainWindow to allow the user to interact with the window and to allow events to be responded to