首页 > 解决方案 > 从循环生成的 python tk.entry 获取输入时出现问题?

问题描述

从昨晚开始,我一直在重写我所有的代码,作为我一直在研究的考勤卡计算器,试图缩短它并提高效率。

所以现在我得到了它,以便time_in输入框和time_out输入框由for loop. 我现在唯一的问题是我无法弄清楚如何在计算函数中从它们那里获取输入。

在我写之前,我variable给每个盒子起了一个名字,并列出了所有盒子,比如times = [time_in1.get(), time_in2.get(), ect...]

但由于我没有这样做,我无法确切地弄清楚如何获得输入。

import tkinter as tkr

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='grey14')

entries_in = []
entries_out = []
totals = []

labelfont = ('calibri', 35)
smallfont = ('calibri', 20)
grandtotalfont = ('calibri', 30)

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

first_name = tkr.Label(root, text="Employee First Name", background="grey14")
first_name.config(font=smallfont, fg="snow")
first_name.grid(row=1, column=0)

first_name_input = tkr.Entry(root)
first_name_input.config(highlightbackground="grey14")
first_name_input.grid(row=2, column=0, padx=15)

last_name = tkr.Label(root, text="Employee Last Name", background="grey14")
last_name.config(font=smallfont, fg="snow")
last_name.grid(row=1, column=2)

last_name_input = tkr.Entry(root)
last_name_input.config(highlightbackground="grey14")
last_name_input.grid(row=2, column=2, padx=15)

pay_start_label = tkr.Label(root, text="Pay Start", background="grey14")
pay_start_label.config(font=smallfont, fg="snow")
pay_start_label.grid(row=3, column=0)

pay_start_input = tkr.Entry(root)
pay_start_input.config(highlightbackground="grey14")
pay_start_input.grid(row=4, column=0)

pay_end_label = tkr.Label(root, text="Pay End", background="grey14")
pay_end_label.config(font=smallfont, fg="snow")
pay_end_label.grid(row=3, column=1)

pay_end_input = tkr.Entry(root)
pay_end_input.config(highlightbackground="grey14")
pay_end_input.grid(row=4, column=1)

pay_date_label = tkr.Label(root, text="Pay Date", background="grey14")
pay_date_label.config(font=smallfont, fg="snow")
pay_date_label.grid(row=3, column=2)

pay_date_input = tkr.Entry(root)
pay_date_input.config(highlightbackground="grey14")
pay_date_input.grid(row=4, column=2)

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

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

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

#=====Creates entry boxes 
for i in range(6, 24):
    entryin = tkr.Entry(root)
    entryin.grid(row=i+1, column=0, padx=15, pady=2)
    entryin.config(highlightbackground="grey14")
    entries_in.append(entryin)

for i in range(6,24):
    entryout = tkr.Entry(root)
    entryout.grid(row=i+1, column=1, padx=15, pady=2)
    entryout.config(highlightbackground="grey14")
    entries_out.append(entries_out)

#=== Creates label to put the totals of the entries into

for i in range(6,24):
    total_label = tkr.Label(root, textvariable="")
    total_label.grid(row=i+1, column=2)
    total_label.config(background="grey14", fg="snow")
    totals.append(total_label)


def clear():
    pass

def calculate():

    totals_in = []

    for time in entries_in:
        if time.get() == 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))

        print(totals_in)

def printer():
    pass

button_clear = tkr.Button(root, width=15, height=2, background="grey14", fg="black",
                          highlightbackground="dodgerblue3", text="Clear", command=clear)
button_clear.grid(row=25, column=0, padx=15, pady=5)

button_calculate = tkr.Button(root, width=15, height=2, background="grey14", highlightbackground="dodgerblue3",
                              text="Calculate", command=calculate)
button_calculate.grid(row=25, column=1, padx=15, pady=5)

button_print = tkr.Button(root, width=15, height=2, background="grey14", highlightbackground="dodgerblue3",
                          text="Print", command=printer)
button_print.grid(row=25, column=2, padx=15, pady=5)

root.mainloop()

标签: pythonpython-3.xtkinter

解决方案


我想到了。这是一个非常简单的解决方案。我只需要.get()if声明之前for loop

def calculate():

    totals_in = []

    for time in entries_in:
        inn = time.get()
        if inn == str(""):
            inn = str('0:0')
        fields = inn.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))

推荐阅读