【发布时间】:2020-06-14 04:33:20
【问题描述】:
我一直在尝试制作一个考勤卡计算器,供我姐姐在她的工作中使用。
它有 16 个不同的“时间进入”槽和 16 个不同的“超时”槽。我遇到的问题是,当我尝试计算它时,我必须每次进出都运行我的代码,从而导致一堆重复的代码。我觉得使用while 循环有一种更简单的方法,我只是想不通。有没有人有任何提示或想法可以帮助我解决这个问题?
在我只粘贴第一部分的代码中,在我的程序中它实际上有我发布的内容,只是多了 15 次。
编辑:我添加了整个函数
另外,我将总计和总计,并使用textvariables 将它们放入tkinter 标签中,您可以在底部看到。
def calculate():
fields = time_in1.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin1 = float(hours) + (float(minutes) / 60.0)
fields = time_out1.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout1 = float(hours) + (float(minutes) / 60.0)
grandtotal1 = totalout1 - totalin1
grandtotal1 = round(grandtotal1, 3)
fields = time_in2.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin2 = float(hours) + (float(minutes) / 60.0)
fields = time_out2.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout2 = float(hours) + (float(minutes) / 60.0)
grandtotal2 = totalout2 - totalin2
grandtotal2 = round(grandtotal2, 3)
fields = time_in3.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin3 = float(hours) + (float(minutes) / 60.0)
fields = time_out3.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout3 = float(hours) + (float(minutes) / 60.0)
grandtotal3 = totalout3 - totalin3
grandtotal3 = round(grandtotal3, 3)
fields = time_in4.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin4 = float(hours) + (float(minutes) / 60.0)
fields = time_out4.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout4 = float(hours) + (float(minutes) / 60.0)
grandtotal4 = totalout4 - totalin4
grandtotal4 = round(grandtotal4, 3)
fields = time_in5.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin5 = float(hours) + (float(minutes) / 60.0)
fields = time_out5.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout5 = float(hours) + (float(minutes) / 60.0)
grandtotal5 = totalout5 - totalin5
grandtotal5 = round(grandtotal5, 3)
fields = time_in6.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin6 = float(hours) + (float(minutes) / 60.0)
fields = time_out6.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout6 = float(hours) + (float(minutes) / 60.0)
grandtotal6 = totalout6 - totalin6
grandtotal6 = round(grandtotal6, 3)
fields = time_in7.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin7 = float(hours) + (float(minutes) / 60.0)
fields = time_out7.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout7 = float(hours) + (float(minutes) / 60.0)
grandtotal7 = totalout7 - totalin7
grandtotal7 = round(grandtotal7, 3)
fields = time_in8.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin8 = float(hours) + (float(minutes) / 60.0)
fields = time_out8.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout8 = float(hours) + (float(minutes) / 60.0)
grandtotal8 = totalout8 - totalin8
grandtotal8 = round(grandtotal8, 3)
fields = time_in9.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin9 = float(hours) + (float(minutes) / 60.0)
fields = time_out9.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout9 = float(hours) + (float(minutes) / 60.0)
grandtotal9 = totalout9 - totalin9
grandtotal9 = round(grandtotal9, 3)
fields = time_in10.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin10 = float(hours) + (float(minutes) / 60.0)
fields = time_out10.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout10 = float(hours) + (float(minutes) / 60.0)
grandtotal10 = totalout10 - totalin10
grandtotal10 = round(grandtotal10, 3)
fields = time_in11.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin11 = float(hours) + (float(minutes) / 60.0)
fields = time_out11.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout11 = float(hours) + (float(minutes) / 60.0)
grandtotal11 = totalout11 - totalin11
grandtotal11 = round(grandtotal11, 3)
fields = time_in12.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin12 = float(hours) + (float(minutes) / 60.0)
fields = time_out12.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout12 = float(hours) + (float(minutes) / 60.0)
grandtotal12 = totalout12 - totalin12
grandtotal12 = round(grandtotal12, 3)
fields = time_in13.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin13 = float(hours) + (float(minutes) / 60.0)
fields = time_out13.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout13 = float(hours) + (float(minutes) / 60.0)
grandtotal13 = totalout13 - totalin13
grandtotal13 = round(grandtotal13, 3)
fields = time_in14.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin14 = float(hours) + (float(minutes) / 60.0)
fields = time_out14.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout14 = float(hours) + (float(minutes) / 60.0)
grandtotal14 = totalout14 - totalin14
grandtotal14 = round(grandtotal14, 3)
fields = time_in15.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin15 = float(hours) + (float(minutes) / 60.0)
fields = time_out15.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout15 = float(hours) + (float(minutes) / 60.0)
grandtotal15 = totalout15 - totalin15
grandtotal15 = round(grandtotal15, 3)
fields = time_in16.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalin16 = float(hours) + (float(minutes) / 60.0)
fields = time_out16.get().split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
totalout16 = float(hours) + (float(minutes) / 60.0)
grandtotal16 = totalout16 - totalin16
grandtotal16 = round(grandtotal16, 3)
time_1_var.set(grandtotal1)
time_2_var.set(grandtotal2)
time_3_var.set(grandtotal3)
time_4_var.set(grandtotal4)
time_5_var.set(grandtotal5)
time_6_var.set(grandtotal6)
time_7_var.set(grandtotal7)
time_8_var.set(grandtotal8)
time_9_var.set(grandtotal9)
time_10_var.set(grandtotal10)
time_11_var.set(grandtotal11)
time_12_var.set(grandtotal12)
time_13_var.set(grandtotal13)
time_14_var.set(grandtotal14)
time_15_var.set(grandtotal15)
time_16_var.set(grandtotal16)
grandtotalall = (grandtotal1 + grandtotal2 + grandtotal3 + grandtotal4 + grandtotal5 + grandtotal6 + grandtotal7 +
grandtotal8 + grandtotal9 + grandtotal10 + grandtotal11 + grandtotal12 + grandtotal13 +
grandtotal14 + grandtotal15 + grandtotal16)
grand_total_var.set(grandtotalall)
【问题讨论】:
-
将重复的代码提取到自己的函数中,根据需要传入数据。还将返回值分配给特定的变量。
-
你可以稍微改变你的代码然后使用循环!
-
这个问题属于代码审查网站。
-
你应该edit你的问题并至少显示一个重复,这样模式就更明显了。
-
@martineau 我为函数添加了其余代码
标签: python python-3.x tkinter