跳过tk。如果Null python,则跳过条目?

问题描述 投票:0回答:2

我一直在研究时间卡计算器,在发布我遇到的一些问题时,人们提到我应该重做所有工作,以使其更短,更高效。

[在计算器中,我有16个tk.Entry框用于time_in,有16个time_out框,所以总共32个。在我使用一个函数时,将每个time_intime_out框分别转换为十进制,然后从time_out中减去time_in以得到total小时,然后将每个班次的总和放入tk.Label,然后将所有总计相加,然后将grand_total放入tk.Label

为了简化这一点,我一直试图写一个loop来代替。

我还没有写完其余部分,因为我在这部分上遇到了麻烦。

在添加if语句之前,它已经起作用。我添加它是因为我试图提出一种方法来基本上跳过所有没有任何输入的time_intime_out框。跳过它,或将其设置为0.0。

因此,基本上,如果有输入,它将运行下面的代码,将所有内容转换为小数并将其存储在正确的列表中。

[此外,如果您注意到的话,我只上到time_in3time_out3列表中的time_intime_out。我只是现在才这样做,直到一切都可以工作,然后把其余的都放在那里。

但是自从我将if语句放到那里以来,我一直收到此错误。

in calculate totals_in.append(round(float(hours) + (float(minutes) / 60.0), 3)) ValueError: could not convert string to float:

我不太清楚为什么,因为它以前工作正常。我知道可能有一些简单的解决方案,但是我知道这是我学习过程的一部分。我必须要学习一些问题。

def calculate():

    time_in =[time_in1.get(), time_in2.get(), time_in3.get()]
    time_out = [time_out1.get(), time_out2.get(), time_out3.get()]
    totals_in = []
    totals_out = []
    grand_totals = []

    for time in time_in:
        if time == None:
            continue
        fields = time.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_in.append(round(float(hours) + (float(minutes) / 60.0), 3))

    for times in time_out:
        if times == None:
            continue
        fields = times.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_out.append(round(float(hours) + (float(minutes) / 60.0), 3))

这是所有代码,如果有帮助的话。现在,我将其设置为将所有Entry都设置为0。但是我觉得这样做会更明智,就像我上面说的那样,要么全部跳过该框,要么将其设置为0.0(如果为null)。

import tkinter as tkr
import tempfile
import os
import numpy
import wx
from wx.html import HtmlEasyPrinting
root = tkr.Tk()

windowWidth = root.winfo_reqwidth()
windowHeight = root.winfo_reqheight()
positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)

root.title("Time Card Calculator")
root.geometry( "+{}+{}".format(positionRight, positionDown))
root.resizable(0,0)
root.configure(background='grey33')

labelfont = ('calibri', 35)

header = tkr.Label(root, text="Time Card Calculator", background="grey33")
header.config(font=labelfont)
header.config(fg="snow")
header.grid(row=0, columnspan=3)

smallfont = ('calibri', 20)

firstlabel = tkr.Label(root, text="Employee first name", background="gray33")
firstlabel.config(font=smallfont)
firstlabel.config(fg="snow")
firstlabel.grid(row=1, column=0)

lastlabel = tkr.Label(root, text="Employee last name", background="gray33")
lastlabel.config(font=smallfont)
lastlabel.config(fg="snow")
lastlabel.grid(row=1, column=2)

startlabel = tkr.Label(root, text="Pay start", background="grey33")
startlabel.config(font=smallfont)
startlabel.config(fg="snow")
startlabel.grid(row=3, column=0)

endlabel = tkr.Label(root, text="Pay end", background="grey33")
endlabel.config(font=smallfont)
endlabel.config(fg="snow")
endlabel.grid(row=3, column=1)

datelabel = tkr.Label(root, text="Pay date", background="grey33")
datelabel.config(font=smallfont)
datelabel.config(fg="snow")
datelabel.grid(row=3, column=2)

inlabel = tkr.Label(root, text="Time In", background="grey33")
inlabel.config(font=smallfont)
inlabel.config(fg="snow")
inlabel.grid(row=5, column=0)

outlabel = tkr.Label(root, text="Time Out", background="grey33")
outlabel.config(font=smallfont)
outlabel.config(fg="snow")
outlabel.grid(row=5, column=1)

totallabel = tkr.Label(root, text="Total", background="grey33")
totallabel.config(font=smallfont)
totallabel.config(fg="snow")
totallabel.grid(row=5, column=2)

time_1_var = tkr.StringVar(root)
time_2_var = tkr.StringVar(root)
time_3_var = tkr.StringVar(root)
time_4_var = tkr.StringVar(root)
time_5_var = tkr.StringVar(root)
time_6_var = tkr.StringVar(root)
time_7_var = tkr.StringVar(root)
time_8_var = tkr.StringVar(root)
time_9_var = tkr.StringVar(root)
time_10_var = tkr.StringVar(root)
time_11_var = tkr.StringVar(root)
time_12_var = tkr.StringVar(root)
time_13_var = tkr.StringVar(root)
time_14_var = tkr.StringVar(root)
time_15_var = tkr.StringVar(root)
time_16_var = tkr.StringVar(root)
grand_total_var = tkr.StringVar(root)

zero = str(0)

first_name = tkr.Entry(root, highlightbackground="grey33")
last_name = tkr.Entry(root, highlightbackground="grey33")
pay_start = tkr.Entry(root, highlightbackground="grey33")
pay_end = tkr.Entry(root, highlightbackground="grey33")
pay_date = tkr.Entry(root, highlightbackground="grey33")
time_in1 = tkr.Entry(root, highlightbackground="grey33")
time_in1.insert(0, zero)
time_out1 = tkr.Entry(root, highlightbackground="grey33")
time_out1.insert(0, zero)
time_1_total = tkr.Label(root, textvariable = time_1_var, background="grey33")
time_1_var.set('')
time_in2 = tkr.Entry(root, highlightbackground="grey33")
time_in2.insert(0, zero)
time_out2 = tkr.Entry(root, highlightbackground="grey33")
time_out2.insert(0, zero)
time_2_total = tkr.Label(root, textvariable = time_2_var, background="grey33")
time_2_var.set('')
time_in3 = tkr.Entry(root, highlightbackground="grey33")
time_in3.insert(0, zero)
time_out3 = tkr.Entry(root, highlightbackground="grey33")
time_out3.insert(0, zero)
time_3_total = tkr.Label(root, textvariable=time_3_var, background="grey33")
time_3_var.set('')
time_in4 = tkr.Entry(root, highlightbackground="grey33")
time_in4.insert(0, zero)
time_out4 = tkr.Entry(root, highlightbackground="grey33")
time_out4.insert(0, zero)
time_4_total = tkr.Label(root, textvariable=time_4_var, background="grey33")
time_4_var.set('')
time_in5 = tkr.Entry(root, highlightbackground="grey33")
time_in5.insert(0, zero)
time_out5 = tkr.Entry(root, highlightbackground="grey33")
time_out5.insert(0, zero)
time_5_total = tkr.Label(root, textvariable=time_5_var, background="grey33")
time_5_var.set('')
time_in6 = tkr.Entry(root, highlightbackground="grey33")
time_in6.insert(0, zero)
time_out6 = tkr.Entry(root, highlightbackground="grey33")
time_out6.insert(0, zero)
time_6_total = tkr.Label(root, textvariable=time_6_var, background="grey33")
time_6_var.set('')
time_in7 = tkr.Entry(root, highlightbackground="grey33")
time_in7.insert(0, zero)
time_out7 = tkr.Entry(root, highlightbackground="grey33")
time_out7.insert(0, zero)
time_7_total = tkr.Label(root, textvariable=time_7_var, background="grey33")
time_7_var.set('')
time_in8 = tkr.Entry(root, highlightbackground="grey33")
time_in8.insert(0, zero)
time_out8 = tkr.Entry(root, highlightbackground="grey33")
time_out8.insert(0, zero)
time_8_total = tkr.Label(root, textvariable=time_8_var, background="grey33")
time_8_var.set('')
time_in9 = tkr.Entry(root, highlightbackground="grey33")
time_in9.insert(0, zero)
time_out9 = tkr.Entry(root, highlightbackground="grey33")
time_out9.insert(0, zero)
time_9_total = tkr.Label(root, textvariable=time_9_var, background="grey33")
time_9_var.set('')
time_in10 = tkr.Entry(root, highlightbackground="grey33")
time_in10.insert(0, zero)
time_out10 = tkr.Entry(root, highlightbackground="grey33")
time_out10.insert(0, zero)
time_10_total = tkr.Label(root, textvariable=time_10_var, background="grey33")
time_10_var.set('')
time_in11 = tkr.Entry(root, highlightbackground="grey33")
time_in11.insert(0, zero)
time_out11 = tkr.Entry(root, highlightbackground="grey33")
time_out11.insert(0, zero)
time_11_total = tkr.Label(root, textvariable=time_11_var, background="grey33")
time_11_var.set('')
time_in12 = tkr.Entry(root, highlightbackground="grey33")
time_in12.insert(0, zero)
time_out12 = tkr.Entry(root, highlightbackground="grey33")
time_out12.insert(0, zero)
time_12_total = tkr.Label(root, textvariable=time_12_var, background="grey33")
time_12_var.set('')
time_in13 = tkr.Entry(root, highlightbackground="grey33")
time_in13.insert(0, zero)
time_out13 = tkr.Entry(root, highlightbackground="grey33")
time_out13.insert(0, zero)
time_13_total = tkr.Label(root, textvariable=time_13_var, background="grey33")
time_13_var.set('')
time_in14 = tkr.Entry(root, highlightbackground="grey33")
time_in14.insert(0, zero)
time_out14 = tkr.Entry(root, highlightbackground="grey33")
time_out14.insert(0, zero)
time_14_total = tkr.Label(root, textvariable=time_14_var, background="grey33")
time_14_var.set('')
time_in15 = tkr.Entry(root, highlightbackground="grey33")
time_in15.insert(0, zero)
time_out15 = tkr.Entry(root, highlightbackground="grey33")
time_out15.insert(0, zero)
time_15_total = tkr.Label(root, textvariable=time_15_var, background="grey33")
time_15_var.set('')
time_in16 = tkr.Entry(root, highlightbackground="grey33")
time_in16.insert(0, zero)
time_out16 = tkr.Entry(root, highlightbackground="grey33")
time_out16.insert(0, zero)
time_16_total = tkr.Label(root, textvariable=time_16_var, background="grey33")
time_16_var.set('')
grand_total = tkr.Label(root, textvariable=grand_total_var, background="grey33")
grand_total_var.set('')

medfont = ('calibri', 30)

first_name.grid(row=2, column=0, padx=15)
last_name.grid(row=2, column=2, padx=15)
pay_start.grid(row=4, column=0, padx=15)
pay_end.grid(row=4, column=1, padx=15)
pay_date.grid(row=4, column=2, padx=15)
time_in1.grid(row=6, column=0, padx=15)
time_out1.grid(row=6, column=1, padx=15)
time_1_total.config(font=smallfont)
time_1_total.config(fg="snow")
time_1_total.grid(row=6, column=2, padx=15)
time_in2.grid(row=7, column=0, padx=15)
time_out2.grid(row=7, column=1, padx=15)
time_2_total.grid(row=7, column=2, padx=15)
time_2_total.config(font=smallfont)
time_2_total.config(fg="snow")
time_in3.grid(row=8, column=0, padx=15)
time_out3.grid(row=8, column=1, padx=15)
time_3_total.grid(row=8, column=2, padx=15)
time_3_total.config(font=smallfont)
time_3_total.config(fg="snow")
time_in4.grid(row=9, column=0, padx=15)
time_out4.grid(row=9, column=1, padx=15)
time_4_total.grid(row=9, column=2, padx=15)
time_4_total.config(font=smallfont)
time_4_total.config(fg="snow")
time_in5.grid(row=10, column=0, padx=15)
time_out5.grid(row=10, column=1, padx=15)
time_5_total.grid(row=10, column=2, padx=15)
time_5_total.config(font=smallfont)
time_5_total.config(fg="snow")
time_in6.grid(row=11, column=0, padx=15)
time_out6.grid(row=11, column=1, padx=15)
time_6_total.grid(row=11, column=2, padx=15)
time_6_total.config(font=smallfont)
time_6_total.config(fg="snow")
time_in7.grid(row=12, column=0, padx=15)
time_out7.grid(row=12, column=1, padx=15)
time_7_total.grid(row=12, column=2, padx=15)
time_7_total.config(font=smallfont)
time_7_total.config(fg="snow")
time_in8.grid(row=13, column=0, padx=15)
time_out8.grid(row=13, column=1, padx=15)
time_8_total.grid(row=13, column=2, padx=15)
time_8_total.config(font=smallfont)
time_8_total.config(fg="snow")
time_in9.grid(row=14, column=0, padx=15)
time_out9.grid(row=14, column=1, padx=15)
time_9_total.grid(row=14, column=2, padx=15)
time_9_total.config(font=smallfont)
time_9_total.config(fg="snow")
time_in10.grid(row=15, column=0, padx=15)
time_out10.grid(row=15, column=1, padx=15)
time_10_total.grid(row=15, column=2, padx=15)
time_10_total.config(font=smallfont)
time_10_total.config(fg="snow")
time_in11.grid(row=16, column=0, padx=15)
time_out11.grid(row=16, column=1, padx=15)
time_11_total.grid(row=16, column=2, padx=15)
time_11_total.config(font=smallfont)
time_11_total.config(fg="snow")
time_in12.grid(row=17, column=0, padx=15)
time_out12.grid(row=17, column=1, padx=15)
time_12_total.grid(row=17, column=2, padx=15)
time_12_total.config(font=smallfont)
time_12_total.config(fg="snow")
time_in13.grid(row=18, column=0, padx=15)
time_out13.grid(row=18, column=1, padx=15)
time_13_total.grid(row=18, column=2, padx=15)
time_13_total.config(font=smallfont)
time_13_total.config(fg="snow")
time_in14.grid(row=19, column=0, padx=15)
time_out14.grid(row=19, column=1, padx=15)
time_14_total.grid(row=19, column=2, padx=15)
time_14_total.config(font=smallfont)
time_14_total.config(fg="snow")
time_in15.grid(row=20, column=0, padx=15)
time_out15.grid(row=20, column=1, padx=15)
time_15_total.grid(row=20, column=2, padx=15)
time_15_total.config(font=smallfont)
time_15_total.config(fg="snow")
time_in16.grid(row=21, column=0, padx=15)
time_out16.grid(row=21, column=1, padx=15)
time_16_total.grid(row=21, column=2, padx=15)
time_16_total.config(font=smallfont)
time_16_total.config(fg="snow")
grand_total.grid(row=22, columnspan=3, padx=15)
grand_total.config(font=medfont)
grand_total.config(fg="snow")

def clear():
    first_name.delete(0, 'end')
    last_name.delete(0, 'end')
    time_in1.delete(0, 'end')
    time_in1.insert(0, zero)
    time_out1.delete(0, 'end')
    time_out1.insert(0, zero)
    time_1_var.set('')
    time_in2.delete(0, 'end')
    time_in2.insert(0, zero)
    time_out2.delete(0, 'end')
    time_out2.insert(0, zero)
    time_2_var.set('')
    time_in3.delete(0, 'end')
    time_in3.insert(0, zero)
    time_out3.delete(0, 'end')
    time_out3.insert(0, zero)
    time_3_var.set('')
    time_in4.delete(0, 'end')
    time_in4.insert(0, zero)
    time_out4.delete(0, 'end')
    time_out4.insert(0, zero)
    time_4_var.set('')
    time_in5.delete(0, 'end')
    time_in5.insert(0, zero)
    time_out5.delete(0, 'end')
    time_out5.insert(0, zero)
    time_5_var.set('')
    time_in6.delete(0, 'end')
    time_in6.insert(0, zero)
    time_out6.delete(0, 'end')
    time_out6.insert(0, zero)
    time_6_var.set('')
    time_in7.delete(0, 'end')
    time_in7.insert(0, zero)
    time_out7.delete(0, 'end')
    time_out7.insert(0, zero)
    time_7_var.set('')
    time_in8.delete(0, 'end')
    time_in8.insert(0, zero)
    time_out8.delete(0, 'end')
    time_out8.insert(0, zero)
    time_8_var.set('')
    time_in9.delete(0, 'end')
    time_in9.insert(0, zero)
    time_out9.delete(0, 'end')
    time_out9.insert(0, zero)
    time_9_var.set('')
    time_in10.delete(0, 'end')
    time_in10.insert(0, zero)
    time_out10.delete(0, 'end')
    time_out10.insert(0, zero)
    time_10_var.set('')
    time_in11.delete(0, 'end')
    time_in11.insert(0, zero)
    time_out11.delete(0, 'end')
    time_out11.insert(0, zero)
    time_11_var.set('')
    time_in12.delete(0, 'end')
    time_in12.insert(0, zero)
    time_out12.delete(0, 'end')
    time_out12.insert(0, zero)
    time_12_var.set('')
    time_in13.delete(0, 'end')
    time_in13.insert(0, zero)
    time_out13.delete(0, 'end')
    time_out13.insert(0, zero)
    time_13_var.set('')
    time_in14.delete(0, 'end')
    time_in14.insert(0, zero)
    time_out14.delete(0, 'end')
    time_out14.insert(0, zero)
    time_14_var.set('')
    time_in15.delete(0, 'end')
    time_in15.insert(0, zero)
    time_out15.delete(0, 'end')
    time_out15.insert(0, zero)
    time_15_var.set('')
    time_in16.delete(0, 'end')
    time_in16.insert(0, zero)
    time_out16.delete(0, 'end')
    time_out16.insert(0, zero)
    time_16_var.set('')
    grand_total_var.set('')


def calculate():

    time_in =[time_in1.get(), time_in2.get(), time_in3.get()]
    time_out = [time_out1.get(), time_out2.get(), time_out3.get()]
    totals_in = []
    totals_out = []
    grand_totals = []

    for time in time_in:
        if time == None:
            continue
        fields = time.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_in.append(round(float(hours) + (float(minutes) / 60.0), 3))

    for times in time_out:
        if times == None:
            continue
        fields = times.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_out.append(round(float(hours) + (float(minutes) / 60.0), 3))



def printer():
    print_var = (first_name.get(), last_name.get(), pay_start.get(), pay_end.get(), pay_date.get(), time_1_var.get(),
                 time_2_var.get(), time_3_var.get(), time_4_var.get(), time_5_var.get(), time_6_var.get(),
                 time_7_var.get(),
                 time_8_var.get(), time_9_var.get(), time_10_var.get(), time_11_var.get(), time_12_var.get(),
                 time_13_var.get(), time_14_var.get(), time_15_var.get(), time_16_var.get(), grand_total_var.get())
    printer_var = grand_total_var.get()
    print_file = tempfile.mktemp(".txt")
    open (print_file, "w"). write(str(print_var))
    os.st(print_file, "print")


button_clear = tkr.Button(root, width=15, height=2, background="deepskyblue", fg="black",
                          highlightbackground="dodgerblue2", text="Clear", command=clear)
button_clear.grid(row=23, column=0, padx=15, pady=5)
button_calculate = tkr.Button(root, width=15, height=2, background="grey33", highlightbackground="dodgerblue2",
                              text="Calculate", command=calculate)
button_calculate.grid(row=23, column=1, padx=15, pady=5)
button_print = tkr.Button(root, width=15, height=2, background="grey33", highlightbackground="dodgerblue2",
                          text="Print", command=printer)
button_print.grid(row=23, column=2, padx=15, pady=5)

tkr.mainloop()
python python-3.x tkinter
2个回答
1
投票

有人可以在Hello World!中放入空字符串或类似Entry的文本,然后float()无法将其转换,并且可能会出现类似ValueError: could not convert string to float: "Hello World!"的错误

您可以使用try/except捕获ValueError并跳过此值

for time in time_in:
    if time: # check if text is not empty string
        try:
            fields = time.split(":")
            hours = fields[0] if len(fields) > 0 else 0.0
            minutes = fields[1] if len(fields) > 1 else 0.0
            totals_in.append(round(float(hours) + (float(minutes) / 60.0), 3))
        except ValueError as ex:
            print(time, "[ERROR]", ex)  # useful for debug and to see if there was problem with value 
            #pass

0
投票

感谢大家的帮助。我终于让它工作了。我知道我仍然需要处理某些事情,例如tk.entry框的循环和其他内容,因此我不需要所有这些变量。但是我仍然认为这是一个巨大的成就。谢谢@furas,您的帮助很大。我知道我还有很长的路要走,但这就是我想出的解决方案。

def calculate():

    time_in =[time_in1.get(), time_in2.get(), time_in3.get(), time_in4.get(), time_in5.get(), time_in6.get(),
              time_in7.get(), time_in8.get(), time_in9.get(), time_in10.get(), time_in11.get(), time_in12.get(),
              time_in13.get(), time_in14.get(), time_in15.get(), time_in16.get()]

    time_out = [time_out1.get(), time_out2.get(), time_out3.get(), time_out4.get(), time_out5.get(), time_out6.get(),
                time_out7.get(), time_out8.get(), time_out9.get(), time_out10.get(), time_out11.get(),
                time_out12.get(), time_out13.get(), time_out14.get(), time_out15.get(), time_out16.get()]
    totals_in = []
    totals_out = []
    grand_totals = []
    total_write = [time_1_var, time_2_var, time_3_var, time_4_var, time_5_var,
                   time_6_var, time_7_var, time_8_var, time_9_var, time_10_var,
                   time_11_var, time_12_var, time_13_var, time_14_var, time_15_var,
                   time_16_var]

    for time in time_in:
        if time == str(""):
            time = str('0:0')
        fields = time.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_in.append(round(float(hours) + (float(minutes) / 60.0), 3))

    for times in time_out:
        if times == str(""):
            times = str('0:0')
        fields = times.split(":")
        hours = fields[0] if len(fields) > 0 else 0.0
        minutes = fields[1] if len(fields) > 1 else 0.0
        totals_out.append(round(float(hours) + (float(minutes) / 60.0), 3))

    for (totalsout, totalsin) in zip(totals_out, totals_in):
        grand_totals.append(round(float(totalsout) - float(totalsin), 3))

    for (totals, label) in zip(grand_totals, total_write):
        label.set(float(totals))
        grand_total_var.set(sum(grand_totals))
© www.soinside.com 2019 - 2024. All rights reserved.