我的出勤管理程序基本上将缺席人数保存在一个简单的.txt文件中,并允许用户增加缺席人数。 问题在于,尽管用户选择了,但代码仅更新了一个主题。
我相信问题出在函数“菜单按钮”的第10行,而在编译时,最后一个主题卡在了参数中。
from tkinter import *
window = Tk()
window.geometry("400x550")
window.configure(background = "black")
window.title("My Attendance Program")
dic = {}
files = open("filehandling.txt", 'w+')
files.close()
############declaration of functions:
def submit_subject():#function works fine
files = open("filehandling.txt", 'r+')
entered_subject = subject_val.get()
output1.delete(0.0, END)
for line in files:
splitline = line.split()
dic["{}".format(splitline[0])] = int(splitline[1])
if entered_subject == "":
output1.insert(END, "don't enter null string")
elif entered_subject in dic.keys():
output1.insert(END, "subject already exists")
else:
files.writelines(entered_subject+" "+"0"+"\n")
output1.insert(END, "subject "+ entered_subject+ " added")
files.close()
def view_subject():
files = open("filehandling.txt", 'r+')
output2.delete(0.0, END)
for line in files:
splitline = line.split()
dic["{}".format(splitline[0])] = int(splitline[1])
output2.insert(END, ', '.join(dic.keys()))
files.close()
def donothing():
print("hello")
def increment_in_file(strings):
print(strings +" has been updated")
files = open("filehandling.txt", 'r+')
for line in files:
splitline = line.split()
dic["{}".format(splitline[0])] = int(splitline[1])
files.close()
files = open("filehandling.txt", 'w')
for keys in dic:
if keys == strings:
files.writelines(keys + " " + "{}\n".format(dic[keys] + 1))
else:
files.writelines(keys + " " + "{}\n".format(dic[keys]))
files.close()
#####################################function that is causing problem
def menubutton():
but = Menubutton(window, text = "choose subject")
but.grid(row = 7, column = 0, sticky = W)
but.menu = Menu(but)
but["menu"] = but.menu
files = open("filehandling.txt", 'r+')
output2.delete(0.0, END)
for line in files:
splitline = line.split()
dic["{}".format(splitline[0])] = int(splitline[1])
but.menu.add_command(label = "{}".format(splitline[0]), command = lambda: increment_in_file("{}".format(splitline[0])))
files.close()
# print(dic)
###############increment_in_file("{}".format(splitline[0]))
#ask for entry of subject row 0 and 1
Label(window, bg = "black", fg = "yellow", text = "Enter subject",anchor = W, width = 40).grid(row = 0, column = 0, sticky = W)
#entry window
subject_val = Entry(window, width = 40)
subject_val.grid(row = 1, column = 0, sticky = W)
#button for submission of subject along with result row 2 and 3
Button(window, text = "Submit", width = 8, command = submit_subject).grid(row = 2, column = 0, sticky = W)
output1 = Text(window, wrap = WORD, width = 40, height = 1)
output1.grid(row = 3, column = 0, sticky = W)
#button and output for the entered subjects row 4 and 5
Button(window, text = "View Subject", width = 15, height = 1, command = view_subject).grid(row = 4, column = 0, sticky = W)
output2 = Text(window, wrap = WORD, width = 40, height = 3)
output2.grid(row = 5, column = 0, sticky = W)
#button to mark attendance row 6 and 7
Button(window, text = "Mark attendance", width = 15, height = 1, command = menubutton).grid(row = 6, column = 0, sticky = W)
window.mainloop()
答案 0 :(得分:0)
是的,问题出在线上(menubutton()
内部):
but.menu.add_command(label = "{}".format(splitline[0]), command = lambda: increment_in_file("{}".format(splitline[0])))
执行函数时,传递给increment_in_file()
的值将始终是for循环的splitline
的最后一个赋值。
您需要通过参数的默认值传递所需的值:
but.menu.add_command(label=splitline[0], command=lambda subj=splitline[0]: increment_in_file(subj))
因为默认值将在创建lambda时根据给定值构造。