0

所以,我用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()
4

2 回答 2

2

That's not how you catch errors. Do it like this:

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

You don't need to call the function again.

于 2013-11-14T03:17:01.623 回答
0

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

于 2013-11-14T03:44:32.283 回答