循环跳过 tk.Entry 如果 Null python?

Gadawg099

我一直在研究一个时间卡计算器,当我发布一些我遇到的问题时,人们提到我应该重新设计所有东西以使其更短、更高效。

在计算器中,我有 16 个tk.Entry盒子time_in和 16盒子time_out,总共 32 个。在我使用一个函数之前,该函数将每个time_intime_out框单独转换为十进制,然后time_out从 the 中减去 thetime_in以获得total工作时间,然后将每个班次的总数放入 a tk.Label,然后将所有总数加在一起并放入grand_totala tk.Label.

为了缩短这个时间,我一直在尝试编写一个loop来使用。

我还没有写剩下的部分,因为我在这部分遇到了麻烦。

在我添加if声明之前,我已经让它工作了我说,因为我试图想出一个办法基本上跳过任何time_in或者time_out说没有任何输入他们框。要么跳过它,要么将其设置为 0.0。

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

另外,如果你发现,我只是上去time_in3,并time_out3time_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))

如果有帮助的话,这就是所有代码。现在我有了它,以便将所有的Entrys 设置为 0。但我觉得像我上面所说的那样做会更聪明,要么一起跳过这个框,要么将它设置为 0.0 如果它为空。

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

有人可以把空字符串或类似的文字Hello World!Entryfloat()不能将它,你可能会得到这样的错误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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章