简体   繁体   English

在tkinter上验证文本输入

[英]validating a text entry on tkinter

I'm creating a program that allows the user to select a category and enter a value to calculate a charge. 我正在创建一个程序,该程序允许用户选择类别并输入值以计算费用。 I would like to validate the text entry using my own validation file. 我想使用自己的验证文件来验证文本输入。 However, when I run the program and enter nothing in the text entry, the error window keeps popping up over and over again. 但是,当我运行该程序并且在文本输入中未输入任何内容时,错误窗口不断反复弹出。 In addition, when I run the program and enter a valid number in the entry field, the charge comes out to 0.0, even though I have defined the calculation for the total charge. 另外,当我运行程序并在输入字段中输入有效数字时,即使我已经定义了总费用的计算方式,费用仍为0.0。

Here is the program: 这是程序:

import tkinter
import tkinter.messagebox
import ValidationFile

validationObject = ValidationFile.ValidationClass ()

class MyGUI:
    def __init__ (self):
        self.main_window = tkinter.Tk ()

        self.top_frame = tkinter.Frame (self.main_window)
        self.middle_frame = tkinter.Frame (self.main_window)
        self.bottom_frame = tkinter.Frame (self.main_window)

        self.phone_var = tkinter.IntVar ()

        self.phone_var.set (1)

        self.pb1 = tkinter.Radiobutton (self.top_frame, \
                    text = 'Daytime (6:00 AM - 5:59 PM)', variable = self.phone_var, \
                    value = 0.12)
        self.pb2 = tkinter.Radiobutton (self.top_frame, \
                    text = 'Evening (6:00 PM - 11:59 PM)', variable = self.phone_var, \
                    value = 0.07)
        self.pb3 = tkinter.Radiobutton (self.top_frame, \
                    text = 'Off-Peak (Midnight - 5:50 AM)', variable = self.phone_var, \
                    value = 0.05)

        #pack phone buttons
        self.pb1.pack ()
        self.pb2.pack ()
        self.pb3.pack ()

        #create input and output buttons
        self.txtInput = tkinter.Entry (self.middle_frame, \
                                       width = 10)
        self.value = tkinter.StringVar ()

        self.lblOutput = tkinter.Label (self.middle_frame, \
                                        width = 10, textvariable = self.value)

        self.txtInput.pack()
        self.lblOutput.pack ()

        #create OK buton and QUIT button
        self.ok_button = tkinter.Button (self.bottom_frame, \
                        text = 'OK', command = self.show_choice)
        self.quit_button = tkinter.Button (self.bottom_frame, \
                        text = 'QUIT', command = self.main_window.destroy)

        #pack the buttons
        self.ok_button.pack (side = 'left')
        self.quit_button.pack (side = 'left')

        #pack the frames
        self.top_frame.pack ()
        self.middle_frame.pack ()
        self.bottom_frame.pack ()

        #start the mainloop
        tkinter.mainloop ()


    def show_choice (self):
        choice = self.phone_var.get ()
        value = -1
        while value == -1:
            valueEntry = self.txtInput.get()
            if valueEntry == '':
                value = -1
                tkinter.messagebox.showinfo (title = 'ERROR', \
                                             message = 'Please enter a valid number.')
            else:
                value = validationObject.checkFloat (valueEntry)

        total = choice * value
        self.value.set (total)

#create instance of MyGUI class
my_GUI = MyGUI ()

Here is the validation file: 这是验证文件:

#create validation class
class ValidationClass:

    def checkFloat (self, inputString):

        try:
            result = float (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

    def checkInteger (self, inputString):

        try:
            result = int (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

You made an infinite loop with while value == -1: . 您使用while value == -1:进行了无限循环。 Nowhere in that loop do you pause to allow the user to try again. 您不会在该循环中的任何地方暂停以允许用户再次尝试。 You don't need the loop at all: 您根本不需要循环:

def show_choice (self):
    valueEntry = self.txtInput.get()
    value = validationObject.checkFloat(valueEntry)
    if value == -1:
        tkinter.messagebox.showinfo (title = 'ERROR', \
                                         message = 'Please enter a valid number.')
    else:
        choice = self.phone_var.get()
        total = choice * value
        self.value.set (total)

Once you fix that you will have another problem: you use float values in your options but the variable is an IntVar, which can only handle integers. 修复后,您将遇到另一个问题:您在选项中使用了浮点值,但变量是一个IntVar,它只能处理整数。 So "choice" will always be 0. You need to use DoubleVar instead. 因此,“选择”将始终为0。您需要改用DoubleVar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM