Python 对文本输入框使用验证
我正在尝试设置文本输入框的验证。其中三个框只需接受整数和一个文本作为邮政编码。我不确定是在以前定义的函数中还是在创建输入框时执行此操作。另外,如何使文本输入框中的值在函数QuoteCreation中可访问。我所有的代码都在下面Python 对文本输入框使用验证,python,validation,tkinter,tkinter-entry,Python,Validation,Tkinter,Tkinter Entry,我正在尝试设置文本输入框的验证。其中三个框只需接受整数和一个文本作为邮政编码。我不确定是在以前定义的函数中还是在创建输入框时执行此操作。另外,如何使文本输入框中的值在函数QuoteCreation中可访问。我所有的代码都在下面 from tkinter import * class quote(): def __init__(self, master): self.master=master self.master.title("Quote Screen"
from tkinter import *
class quote():
def __init__(self, master):
self.master=master
self.master.title("Quote Screen")
self.master.geometry("2100x1400")
self.master.configure(background = "white")
self.Borras = PhotoImage(file = "Borras.Logo.2.gif") #sets up image
self.Borras.image = self.Borras
self.BorrasLabel = Label(self.master, image = self.Borras, bg = "white")#puts image onto label
self.BorrasLabel.place(anchor=NW)
self.Title = Label(self.master, text = "New Quote", font = ("calibri", 20), bg = "White")
self.Title.place(x=650, y = 10)
self.SubmitButton = PhotoImage(file = "Submit.Button.gif") #sets up image
self.SubmitButton.image = self.SubmitButton
self.SubmitButtonLabel = Button(self.master, image = self.SubmitButton, bg = "white", command= self.QuoteCreation)#puts image onto a button
self.SubmitButtonLabel.place(x=900, y=290)
PostCodeVar = StringVar()
PostCodeEntry = Entry(master,width=50, font=20, textvariable=PostCodeVar)
PostCodeEntry.place(x = 20, y = 150)
PostCodeVar.set("Please enter the Post Code")
PostCodeValue = PostCodeVar.get()
HeightVar = StringVar()
HeightEntry = Entry(master, width=50, font=20, textvariable=HeightVar)
HeightEntry.place(x = 20, y = 220)
HeightVar.set("Please enter the Height")
HeightValue = HeightVar.get()
LengthVar = StringVar()
LengthEntry = Entry(master, width=50, font=20, textvariable=LengthVar)
LengthEntry.place(x = 20, y = 290)
LengthVar.set("Please enter the Length")
LengthValue = LengthVar.get()
PitchVar = StringVar()
PitchEntry = Entry(master, width=50, font=20, textvariable=PitchVar)
PitchEntry.place(x = 20, y = 360)
PitchVar.set("Please enter the Pitch")
PitchValue = PitchVar.get()
RiseVar = StringVar()
RiseEntry = Entry(master, width=50, font=20, textvariable=RiseVar)
RiseEntry.place(x = 20, y = 430)
RiseVar.set("Please enter the Rise")
RiseValue = RiseVar.get()
self.SubmitButton = PhotoImage(file = "Submit.Button.gif")
self.SubmitButton.image = self.SubmitButton
self.SubmitButtonLabel = Button(self.master, image = self.SubmitButton, bg = "white", command= self.QuoteCreation)#puts image onto a button
self.SubmitButtonLabel.place(x=900, y=290)
def on_button(self):
print(self.entry.get())
def QuoteCreation(self):
print(' ')
def quitWindow(self):
self.master.destroy()
def backToWelcome(self):
self.master.destroy()
当按下submit按钮时,您可以设置单独的函数来处理验证 例如,您的提交按钮可能看起来有点像:
submitButton = Button(master, text="Submit", command=validation)
在您的情况下,验证人员希望执行以下检查:
def validation():
postcode = PostCodeVar.get()
length = LengthVar.get()
pitch = PitchVar.get()
rise = RiseVar.get()
if postcodeCheck(postcode) == True and length.isdigit() == True and pitch.isdigit() == True and rise.isdigit() == True:
#carry out chosen process
在您的情况下,您可以尝试在调用函数之前设置邮政编码、长度、间距和高度变量,并将它们设置为全局变量。应创建邮政编码,如果可以,则功能应:
return True
…因此它与if语句的结果相匹配
我希望这就是您所寻找的,并且可以根据您的具体问题调整示例 您希望验证在用户键入时进行,还是仅在他们单击“提交”按钮后进行?我希望验证在“提交”按钮上进行,但如果可能,希望默认文本在用户单击框时消失。我注意到您从未接受任何问题的任何答案。如果你不想,那完全没关系。只是提醒你这个功能。