Skip to main content
 首页 » 编程设计

python之在文本输入框上使用验证

2024年10月24日10kuangbin

我正在尝试在文本输入框上设置验证。其中三个框只需要接受整数和一个文本作为邮政编码。我不确定是在先前定义的函数中还是在创建输入框时执行此操作。另外,我将如何使文本输入框中的值在函数 QuoteCreation 中可访问。我所有的代码都在下面。

     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()  

请您参考如下方法:

当按下提交按钮时,您可以设置单独的函数来处理验证。

因此,举个例子,您的提交按钮可能看起来像这样:

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 语句的结果。

我希望这就是您要找的,并且可以根据您的具体问题调整示例!