Tkinter中的异常处理

时间:2013-11-14 03:08:23

标签: python tkinter

所以,我在python中写了一个小的Tkinter程序。该程序执行良好,但如果在字段中输入非数字字符,则很容易发生异常。

所以,我试图解决它,但我的补救措施失败了:

以下是问题:

try:
            self.laborOne = float(self.trimmerOne_Entry.get()) * 8
            self.laborTwo = float(self.trimmerTwo_Entry.get()) * 8
            self.laborThree = float(self.operator_Entry.get()) * 8
            self.addedThem = self.laborOne + self.laborTwo + self.laborThree
            self.laborTotal.set(str(self.addedThem))
            self.perUnitLabor = self.addedThem / 125
            self.laborUnit.set(str(self.perUnitLabor))
except ValueError:
        tkinter.messagebox.showinfo('Error:', 'One or more of your values was not numeric. Please fix them.')
        self.performIt()
            self.performIt()

首先,我尝试在错误处理中显示消息框,但是当您单击“确定”时关闭程序。所以,我尝试了递归,将函数调用给自己。发生这种情况时,对话框就会停留在那里。因为self.performIt不需要传入arg,所以我将(self)传递给它只是为了尝试它。这允许我在框中修改我的值,这是我正在寻找的,但导致不同的异常

无论如何,如何在程序没有终止的情况下处理ValueError异常,以便用户可以输入更正的数据?

完整代码

import tkinter
import tkinter.messagebox

class MyGui:
    def __init__(self):
        #create the main window widget
        self.main_window = tkinter.Tk()

        #create 6 frames: 
        #one for each trimmers/operators pay,
        #one for buttons
        #one for outputs
        self.trimmerOne = tkinter.Frame(self.main_window)
        self.trimmerTwo = tkinter.Frame(self.main_window)
        self.operator = tkinter.Frame(self.main_window)
        self.rotaryLabor = tkinter.Frame(self.main_window)
        self.rotaryLaborUnit = tkinter.Frame(self.main_window)
        self.buttonFrame = tkinter.Frame(self.main_window)

        #create and pack widgets for Trimmer 1
        self.trimmerOne_Label = tkinter.Label(self.trimmerOne, text='Enter the payrate for trimmer 1: ')
        self.trimmerOne_Entry = tkinter.Entry(self.trimmerOne, width=10)
        self.trimmerOne_Label.pack(side='left')
        self.trimmerOne_Entry.pack(side='left')

        #create and pack widgets for Trimmer 2
        self.trimmerTwo_Label = tkinter.Label(self.trimmerTwo, text='Enter the payrate for trimmer 2: ')
        self.trimmerTwo_Entry = tkinter.Entry(self.trimmerTwo, width=10)
        self.trimmerTwo_Label.pack(side='left')
        self.trimmerTwo_Entry.pack(side='left')

        #create and pack widgets for Operator
        self.operator_Label = tkinter.Label(self.operator, text='Enter the payrate for operator: ')
        self.operator_Entry = tkinter.Entry(self.operator, width=10)
        self.operator_Label.pack(side='left')
        self.operator_Entry.pack(side='left')

        #create and pack widgets for rotaryLabor
        self.rotaryLabor_Label = tkinter.Label(self.rotaryLabor, text="This is what it cost's in trimmer labor: ")
        self.laborTotal = tkinter.StringVar() #to update with laborTotal_Label
        self.laborTotal_Label = tkinter.Label(self.rotaryLabor, textvariable=self.laborTotal)
        self.rotaryLabor_Label.pack(side='left')
        self.laborTotal_Label.pack(side='left')

        #create and pack widgets for labor Unit
        self.rotaryLaborUnit_Label = tkinter.Label(self.rotaryLaborUnit, text="This is the cost per part in trim labor: ")
        self.laborUnit = tkinter.StringVar() #to update with laborTotal_Label
        self.laborUnit_Label = tkinter.Label(self.rotaryLaborUnit, textvariable=self.laborUnit)
        self.rotaryLaborUnit_Label.pack(side='left')
        self.laborUnit_Label.pack(side='left')

        #create and pack the button widgets
        self.calcButton = tkinter.Button(self.buttonFrame, text = "Calculate", command=self.performIt)
        self.saveButton = tkinter.Button(self.buttonFrame, text = "Save", command=self.saveIt)
        self.quitButton = tkinter.Button(self.buttonFrame, text = "Quit", command=self.main_window.destroy)
        self.calcButton.pack(side="left")
        self.saveButton.pack(side="left")
        self.quitButton.pack(side="left")

        #pack the frames
        self.trimmerOne.pack()
        self.trimmerTwo.pack()
        self.operator.pack()
        self.rotaryLabor.pack()
        self.rotaryLaborUnit.pack()
        self.buttonFrame.pack()

        tkinter.mainloop()

    #define the function that will do the work:
    def performIt(self):
        try:
            self.laborOne = float(self.trimmerOne_Entry.get()) * 8
            self.laborTwo = float(self.trimmerTwo_Entry.get()) * 8
            self.laborThree = float(self.operator_Entry.get()) * 8
            self.addedThem = self.laborOne + self.laborTwo + self.laborThree
            self.laborTotal.set(str(self.addedThem))
            self.perUnitLabor = self.addedThem / 125
            self.laborUnit.set(str(self.perUnitLabor))
        except ValueError:
        tkinter.messagebox.showinfo('Error:', 'One or more of your values was not numeric. Please fix them.')
        self.performIt()
            self.performIt()

    def saveIt(self):
        self.laborOne = float(self.trimmerOne_Entry.get()) * 8
        self.laborTwo = float(self.trimmerTwo_Entry.get()) * 8
        self.laborThree = float(self.operator_Entry.get()) * 8
        self.addedThem = self.laborOne + self.laborTwo + self.laborThree
        self.laborTotal.set(str(self.addedThem))
        self.perUnitLabor = self.addedThem / 125
        self.laborUnit.set(str(self.perUnitLabor))
        file = open("log.txt", 'w')
        file.write("Trimmer One gets paid: " + str(self.laborOne))
        file.write("\n___________________________________________\n")
        file.write("Trimmer Two gets paid: " + str(self.laborTwo))
        file.write("\n___________________________________________\n")
        file.write("Operator gets paid: " + str(self.laborThree))
        file.write("\n___________________________________________\n")
        file.write("The sum of thier daily labor is: " + str(self.addedThem))
        file.write("\n___________________________________________\n")
        file.write("If production is reached, the labor cost is" + str(self.laborOne) + "per unit")
        file.write("\n___________________________________________\n")
        file.close()



testRun = MyGui()

2 个答案:

答案 0 :(得分:2)

这不是你如何捕捉错误。这样做:

except ValueError:
            tkinter.messagebox.showinfo('Error:', 'One or more of your values was not numeric. Please fix them.')

您无需再次调用该功能。

答案 1 :(得分:0)

在ValueError异常处理程序中,只需要显示错误。在performIt返回后,GUI仍将存在。