ValueErrors в рабочем приложении tkinter gui

Я получил свое первое приложение графического интерфейса до функциональной точки, где я могу начать использовать его. Все работает отлично, за исключением того, что я получаю трассировку ValueErrors не может преобразовать строку в плавающее, когда я вводить данные в записи. Я получаю и другие ошибки, которые я не понимаю. Любая идея о том, как я могу сделать это приложение без ошибок, будет принята с благодарностью.

Вот копия консоли после ввода некоторых чисел в записи и изменения комбинированного списка

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\timewarnerIP-server\Desktop\Hillside_production_Calculator.py",
 line 370, in <lambda>
    self.cycletimes[num].trace('w', lambda x,y,z: proj(num))
  File "C:\Users\timewarnerIP-server\Desktop\Hillside_production_Calculator.py",
 line 346, in proj
    case = float(self.current_case_vars[tab].get())
ValueError: could not convert string to float:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\timewarnerIP-server\Desktop\Hillside_production_Calculator.py",
 line 377, in <lambda>
    self.moldvars[num].trace("w", lambda x, y, z: cyclecnt(num))
  File "C:\Users\timewarnerIP-server\Desktop\Hillside_production_Calculator.py",
 line 301, in cyclecnt
    cyclecount = int(self.cycle_count_vars[tab].get())
ValueError: invalid literal for int() with base 10: ''

#

from Tkinter import *
from ttk import *
import time


class supervisorcalc:
    def __init__(self, master):

        #####notebook tabs#####
        self.notebook = Notebook(master)
        self.lines = {}
        for i in range(10, 20):
            self.lines[i] = Frame(self.notebook)
            self.notebook.add(self.lines[i], text="Line" +  str(i))
        self.notebook.grid(row=0,column=0)
        ###end notebook tabs###

        moldoptions = [1, 1, 2, 3, 4]

###widget dictionaries###
    ### cycle time ###

        ###cycle time labels###
        self.ctlabels = {}
        for i in range(10, 20):
            self.ctlabels[i] = Label(self.lines[i], text = "Cycle Time:")

        ###cycle time variables###
        self.cycletimes = {}
        for i in range(10,20):
            self.cycletimes[i] = StringVar()

        ###cycle time entries###
        self.cycle_time_entries = {}
        for i in range(10,20):
            self.cycle_time_entries[i] = Entry(self.lines[i], textvariable=self.cycletimes[i])
    ###molds###

        ###mold combo box variables###
        self.moldvars = {}
        for i in range(10,20):
            self.moldvars[i] = IntVar()

        ###molds labels###
        self.heads_labels = {}
        for i in range(10,20):
            self.heads_labels[i] = Label(self.lines[i], text = "# of Molds:")

        ###molds combo box###
        self.head_combo_boxes = {}
        for i in range(10,20):
            self.head_combo_boxes[i] = OptionMenu(self.lines[i], self.moldvars[i], *moldoptions)

    ###cycle count###

        ###cycle count labels###
        self.cycle_count_labels = {}
        for i in range(10,20):
            self.cycle_count_labels[i] = Label(self.lines[i], text = "Cycle Count:")

        ###cycle count variables###
        self.cycle_count_vars = {}
        for i in range(10,20):
            self.cycle_count_vars[i] = StringVar()

        ###cycle count entries###
        self.cycle_count_entries = {}
        for i in range(10,20):
            self.cycle_count_entries[i] = Entry(self.lines[i], textvariable=self.cycle_count_vars[i])

    ### units per case###

        ### units per case labels###
        self.units_per_case_labels = {}
        for i in range(10,20):
            self.units_per_case_labels[i] = Label(self.lines[i], text = "Units/Case:")

        ### units per case variables#
        self.units_per_case_vars = {}
        for i in range(10,20):
            self.units_per_case_vars[i] = StringVar()

        ### units per case entries###
        self.units_per_case_entries = {}
        for i in range(10,20):
            self.units_per_case_entries[i] = Entry(self.lines[i], textvariable=self.units_per_case_vars[i])

    ### current case ###

        ### current case labels ###
        self.current_case_labels = {}
        for i in range(10, 20):
            self.current_case_labels[i] = Label(self.lines[i], text = "Current Case #:")

        ### current case variables ###
        self.current_case_vars = {}
        for i in range(10,20):
            self.current_case_vars[i] = StringVar()

        ### current case entries ###
        self.current_case_entries = {}
        for i in range(10, 20):
            self.current_case_entries[i]  = Entry(self.lines[i], textvariable=self.current_case_vars [i])

    ### current time ###

        ### current time labels###
        self.current_time_labels = {}
        for i in range (10, 20):
            self.current_time_labels[i] = Label(self.lines[i], text = "Current Time:")

        ### clock labels ###
        self.clocks = {}
        for i in range (10, 20):
            self.clocks[i] = Label(self.lines[i])

    ### future time ###

        ### future time extra frame###
        self.future_frames = {}
        for i in range (10, 20):
            self.future_frames[i] =  Frame(self.lines[i])

        ### future labels ###
        self.future_labels = {}
        for i in range (10, 20):
            self.future_labels[i] =  Label(self.lines[i], text = "Future Projections time:")

        ### future hour variables ###
        self.future_hours_vars = {}
        for i in range (10, 20):
            self.future_hours_vars[i] = StringVar()

        ### future hour entries ###
        self.future_hour_entries = {}
        for i in range (10, 20):
            self.future_hour_entries[i] = Entry(self.future_frames[i], width=2, textvariable=self.future_hours_vars[i])

        ### future time colon###
        self.future_time_colon = {}
        for i in range(10, 20):
            self.future_time_colon[i] = Label(self.future_frames[i], text = ":")

        ### future minutes variables###
        self.future_minutes_vars = {}
        for i in range(10,20):
            self.future_minutes_vars[i] = StringVar()

        ### future minutes entries###
        self.future_minutes = {}
        for i in range(10,20):
            self.future_minutes[i] = Entry(self.future_frames[i], width=2, textvariable=self.future_minutes_vars[i])

    ### cycle total ###

        ### cycle total labels ###
        self.total_cycles_labels = {}
        for i in range(10, 20):
            self.total_cycles_labels[i] = Label(self.lines[i], text = 'Cycle Total:')

        ### total cycles variables ###
        self.total_cycles_vars = {}
        for i in range(10,20):
            self.total_cycles_vars[i] = StringVar()

        ### total cycles display###
        self.total_cycles_disp = {}
        for i in range(10, 20):
            self.total_cycles_disp[i] = Label(self.lines[i], foreground = 'green', background = 'black', text = "00000", textvariable = self.total_cycles_vars[i])

    ### current production ###

        ### current production labels ###
        self.current_production_labels = {}
        for i in range(10, 20):
            self.current_production_labels[i] = Label(self.lines[i], text = 'Current Production:')

        ### current production variables ###
        self.current_production_vars = {}
        for i in range(10, 20):
            self.current_production_vars[i] = StringVar()

        ### current production ###
        self.current_production_disp = {}
        for i in range(10, 20):
            self.current_production_disp[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.current_production_vars[i])

    ### projected production ###

        ### projected production labels###
        self.projected_production_labels = {}
        for i in range(10, 20):
            self.projected_production_labels[i] = Label(self.lines[i], text = 'Projected Production:')

        ### projected production variables ###
        self.projected_production_vars = {}
        for i in range(10, 20):
            self.projected_production_vars[i] = StringVar()

        ### projected production display ###
        self.projected_production_disp = {}
        for i in range(10,20):
            self.projected_production_disp[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.projected_production_vars[i])

    ### lost units ###

        ### lost units labels ###
        self.lost_units_labels = {}
        for i in range(10, 20):
            self.lost_units_labels[i] = Label(self.lines[i], text = 'Lost units:')

        ### lost units variables ###
        self.lost_units_vars = {}
        for i in range(10, 20):
            self.lost_units_vars[i] = StringVar()

        ### lost units display ###
        self.lost_units_displays = {}
        for i in range(10, 20):
            self.lost_units_displays[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.lost_units_vars[i])

    ### projected lost units ###

        ### projectd lost units labels ###
        self.projected_lost_units_labels = {}
        for i in range(10, 20 ):
            self.projected_lost_units_labels[i] = Label(self.lines[i], text = 'Projected Lost units:')

        ### projected lost unit variables ###
        self.projected_lost_units_vars = {}
        for i in range(10, 20):
            self.projected_lost_units_vars[i] = StringVar()

        ### projected lost ###
        self.projected_lost_units_displays = {}
        for i in range(10, 20):
            self.projected_lost_units_displays[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.projected_lost_units_vars[i])

    ### efficiency ###

        ### efficiency labels ###
        self.current_efficiency_labels = {}
        for i in range(10,20):
            self.current_efficiency_labels[i] = Label(self.lines[i], text = 'Current efficiency %:')

        ### efficiency variables ###
        self.current_efficiency_vars = {}
        for i in range(10, 20):
            self.current_efficiency_vars[i] = StringVar()

        ### efficiency displays ###
        self.current_efficiency_displays = {}
        for i in range(10, 20):
            self.current_efficiency_displays[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.current_efficiency_vars[i])

    ### future case ###

        ### future case labels ###
        self.future_case_projection_labels = {}
        for i in range(10, 20):
            self.future_case_projection_labels[i] = Label(self.lines[i], text = 'Future case # projection:')

        ### future case variables ###
        self.future_case_projection_vars = {}
        for i in range(10, 20):
            self.future_case_projection_vars[i] = StringVar()

        ### future case displays ###
        self.future_case_projection_disp = {}
        for i in range(10, 20):
            self.future_case_projection_disp[i] = Label(self.lines[i], foreground = 'green', background = 'black', textvariable=self.future_case_projection_vars[i])

    ### projected efficiency ###

        ### projected efficiency labels ###
        self.projected_efficiency_labels = {}
        for i in range(10, 20):
            self.projected_efficiency_labels[i] = Label(self.lines[i], text = "Efficiency Projection:")

        ### projected efficiency variables ###
        self.projected_efficiency_vars = {}
        for i in range(10, 20):
            self.projected_efficiency_vars[i] = StringVar()

        ### projected efficiency entries ###
        self.projected_efficiency_entries = {}
        for i in range(10, 20):
            self.projected_efficiency_entries[i] = Entry(self.lines[i], textvariable=self.projected_efficiency_vars[i])









        ###functions###

        def cyclecnt(tab, *args):
            cyclecount = int(self.cycle_count_vars[tab].get())
            molds = self.moldvars[tab].get()
            cyccount = cyclecount * molds
            self.total_cycles_vars[tab].set(cyccount)            
            return


        def currentproduction(tab, *args):
            item = int(self.units_per_case_vars[tab].get())
            case = int(self.current_case_vars [tab].get())
            currprod = item * case
            self.current_production_vars[tab].set(currprod)
            return

        def lostunits(tab, *args):
            cycle = int(self.total_cycles_vars[tab].get())
            prod = int(self.current_production_vars[tab].get())
            self.lost_units_vars[tab].set(cycle - prod)
            return

        def efficiency(tab, *args):
            lost = float(self.lost_units_vars[tab].get())
            prod = float(self.current_production_vars[tab].get())
            self.current_efficiency_vars[tab].set((lost/prod)*100)
            return

        def getSec(x):
            l = x.split(':')
            return int(l[0]) * 3600 + int(l[1]) * 60 + int(l[2])

        def future_time_seconds(tab, *args):
            hrs = self.future_hours_vars[tab].get()
            mins = self.future_minutes_vars[tab].get()
            return (int(hrs) * 3600) + (int(mins) * 60)

        def time_difference_seconds(tab, *args):
            fseconds = future_time_seconds(tab)
            s = time.strftime('%I:%M:%S')
            cursecs = getSec(s)
            return fseconds - cursecs

        def proj(tab, *args):
            ctime = float(self.cycletimes[tab].get())
            prod = float(self.current_production_vars[tab].get())
            loss = float(self.lost_units_vars[tab].get())
            case = float(self.current_case_vars[tab].get())
            molds = float(self.moldvars[tab].get())
            item = float(self.units_per_case_vars[tab].get())

            seconds = time_difference_seconds(tab)

            pcycle = ((molds / ctime) * seconds)
            projeff = float(self.projected_efficiency_vars[tab].get()) / float(100)

            pproduction = pcycle - (pcycle * projeff)

            self.projected_production_vars[tab].set(prod + pproduction)                     
            projloss = loss + pcycle * projeff
            self.projected_lost_units_vars[tab].set(projloss)

            fcase = case + (pproduction / float(item))
            self.future_case_projection_vars[tab].set(fcase)

### widget fuction ###

        def widg_on_panes(self, num):

            ### cycle time ###
            self.ctlabels[num].grid(row=2, column=0) 
            self.cycletimes[num].trace('w', lambda x,y,z: proj(num))
            self.cycle_time_entries[num].grid(row=2,column=1)

            ### molds ###
            self.moldvars[num].set(moldoptions[0])
            self.heads_labels[num].grid(row=3, column=0)
            self.head_combo_boxes[num].grid(row=3,column=1)
            self.moldvars[num].trace("w", lambda x, y, z: cyclecnt(num))

            ### cycle count ###
            self.cycle_count_labels[num].grid(row=4, column=0)
            self.cycle_count_vars[num].trace("w", lambda x, y, z: cyclecnt(num))
            self.cycle_count_entries[num].grid(row=4,column=1)

            ### units per case ###
            self.units_per_case_labels[num].grid(row=5, column=0)
            self.units_per_case_entries[num].grid(row=5,column=1)
            self.units_per_case_vars[num].trace("w", lambda x, y, z: currentproduction(num))

            ### current case ###      
            self.current_case_labels[num].grid(row=6, column=0)
            self.current_case_entries[num].grid(row=6,column=1)
            self.current_case_vars[num].trace("w", lambda x, y, z: currentproduction(num))

            ### clock ###
            self.current_time_labels[num].grid(row=7, column=0, sticky='W')
            self.clocks[num].grid(row=7,column=1, sticky='w')

            #### future time ###
            self.future_frames[num].grid(row=8, column=1)           
            self.future_labels[num].grid(row=8, column=0, sticky='w')
            self.future_hours_vars[num].trace('w', lambda x, y, z: proj(num))
            self.future_hour_entries[num].grid(row=0, column=0) 
            self.future_time_colon[num].grid(row=0, column=1)
            self.future_minutes_vars[num].trace('w', lambda x, y, z: proj(num))
            self.future_minutes[num].grid(row=0, column=2)


            ### total cycle display ### 
            self.total_cycles_labels[num].grid(row=2, column=2)
            self.total_cycles_vars[num].set("00000")
            self.total_cycles_vars[num].trace('w', lambda x, y, z: lostunits(num))
            self.total_cycles_disp[num].grid(row=2, column=3)

            ### current production ###
            self.current_production_labels[num].grid(row=3, column=2)
            self.current_production_vars[num].set('00000')           
            self.current_production_vars[num].trace('w', lambda x, y, z: lostunits(num))
            self.current_production_vars[num].trace('w', lambda x, y, z: efficiency(num))
            self.current_production_disp[num].grid(row=3, column=3)

            ### projected production ###    
            self.projected_production_labels[num].grid(row=4, column=2)
            self.projected_production_vars[num].set('00000')
            self.projected_production_disp[num].grid(row=4, column=3)

            ### lost units ###
            self.lost_units_labels[num].grid(row=5, column=2)
            self.lost_units_vars[num].set("0000")
            self.lost_units_vars[num].trace('w', lambda x, y, z: efficiency(num))
            self.lost_units_displays[num].grid(row=5, column=3)

            ### projected lost uniots ###c            
            self.projected_lost_units_labels[num].grid(row=6, column=2)
            self.projected_lost_units_vars[num].set("0000")
            self.projected_lost_units_displays[num].grid(row=6, column=3)

            ### current efficiency ###
            self.current_efficiency_labels[num].grid(row=7, column=2)
            self.current_efficiency_vars[num].set("00.00") 
            self.current_efficiency_displays[num].grid(row=7, column=3)

            ### future case ###
            self.future_case_projection_labels[num].grid(row=8, column=2)
            self.future_case_projection_vars[num].set("000.0")
            self.future_case_projection_disp[num].grid(row=8, column=3)

            ### projected efficiency ###
            self.projected_efficiency_labels[num].grid(row=9, column=2)
            self.projected_efficiency_vars[num].set(0.00)
            self.projected_efficiency_vars[num].trace('w', lambda x, y, z: proj(num))
            self.projected_efficiency_entries[num].grid(row=9,column=3)



        for num in range(10,20):
            widg_on_panes(self, num)


        def tick():
            s = time.strftime('%I:%M:%S')
            if s != self.clocks[10]:
                for i in range(10, 20):
                    self.clocks[i].configure(text=s)
            self.notebook.after(200, tick)

        tick()

root = Tk()
root.wm_title("Hillside Plastics Production Calculator")
calc = supervisorcalc(root)

mainloop()

1 ответ

Ваш tracebak содержит две ошибки.
В этом случае

line 301, in cyclecnt
    cyclecount = int(self.cycle_count_vars[tab].get())
ValueError: invalid literal for int() with base 10: ''

Вы пытаетесь преобразовать пустую строку в целое число. В этом случае вы можете использовать следующий подход

s = self.cycle_count_vars[tab].get()
cyclecount = int(s) if s else 0

или более общий подход

try:
    cyclecount = int(self.cycle_count_vars[tab].get())
except ValueError as e:
    cyclecount = 0 # or some other default value

Эта ошибка

line 346, in proj
    case = float(self.current_case_vars[tab].get())
ValueError: could not convert string to float:

означает, что строковая переменная self.current_case_vars[tab].get() не содержит действительных данных с плавающей точкой. Эта ситуация также может быть обработана с try/except блок.

Другие вопросы по тегам