簡體   English   中英

在tkinter上驗證文本輸入

[英]validating a text entry on tkinter

我正在創建一個程序,該程序允許用戶選擇類別並輸入值以計算費用。 我想使用自己的驗證文件來驗證文本輸入。 但是,當我運行該程序並且在文本輸入中未輸入任何內容時,錯誤窗口不斷反復彈出。 另外,當我運行程序並在輸入字段中輸入有效數字時,即使我已經定義了總費用的計算方式,費用仍為0.0。

這是程序:

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

這是驗證文件:

#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

您使用while value == -1:進行了無限循環。 您不會在該循環中的任何地方暫停以允許用戶再次嘗試。 您根本不需要循環:

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)

修復后,您將遇到另一個問題:您在選項中使用了浮點值,但變量是一個IntVar,它只能處理整數。 因此,“選擇”將始終為0。您需要改用DoubleVar。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM