我需要助手,一个按钮应该可以从具有特定行的组合框中工作 4 两种可能性,但只对选项 1 有效
i need assistants, a button should work 4 two possibilities from a combo box with specific lines but only really works for option 1
I setup a button to work with two possibilities from a combo box so that if Event 1 is chosen it will save to lines 1 to 4 which works fine and saves with no problems but when Event 2 is chosen it should save 5 到 8 但它不能正常工作 5 会把它自己放在更正的行中,但 6 会向下飞 10 行,我昨天花了很多时间添加和删除代码但没有 gold.any 想法?
def saveteam():
with open("data/tournamentdatae1.txt",'r') as f:
get_all=f.readlines()
event = combo_event.get()
if event == 'Event 1':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 1:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 2:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 3:
input3 = team_3.get()
f.writelines(input3)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 4:
input4 = team_4.get()
f.writelines(input4)
f.writelines("\n")
if event == 'Event 2':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 5:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
else:
f.writelines(line)
for i,line in enumerate(get_all,1):
if i == 6:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
else:
f.writelines(line)
这就是我所做的,但不确定如何使用它来制作节目
if event == 'Event 1':
if indivcombo == 'Individual 1':
get_all[20] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[21] = individual.get() + "\n"
if indivcombo == 'Individual 3':
get_all[22] = individual.get() + "\n"
if indivcombo == 'Individual 4':
get_all[23] = individual.get() + "\n"
if indivcombo == 'Individual 5':
get_all[24] = individual.get() + "\n"
if indivcombo == 'Individual 6':
get_all[25] = individual.get() + "\n"
if indivcombo == 'Individual 7':
get_all[26] = individual.get() + "\n"
if indivcombo == 'Individual 8':
get_all[27] = individual.get() + "\n"
if indivcombo == 'Individual 9':
get_all[28] = individual.get() + "\n"
if indivcombo == 'Individual 10':
get_all[29] = individual.get() + "\n"
if indivcombo == 'Individual 11':
get_all[30] = individual.get() + "\n"
if indivcombo == 'Individual 12':
get_all[31] = individual.get() + "\n"
if indivcombo == 'Individual 13':
get_all[32] = individual.get() + "\n"
if indivcombo == 'Individual 14':
get_all[33] = individual.get() + "\n"
if indivcombo == 'Individual 15':
get_all[34] = individual.get() + "\n"
if indivcombo == 'Individual 16':
get_all[35] = individual.get() + "\n"
if indivcombo == 'Individual 17':
get_all[36] = individual.get() + "\n"
if indivcombo == 'Individual 18':
get_all[37] = individual.get() + "\n"
if indivcombo == 'Individual 19':
get_all[38] = individual.get() + "\n"
if indivcombo == 'Individual 20':
get_all[39] = individual.get() + "\n"
if event == 'Event 2':
if indivcombo == 'Individual 1':
get_all[40] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[41] = individual.get() + "\n"
如果我对你的理解是正确的(大如果),那么你想用 team_X
变量的值替换文件中的某些行。你这样做的方式是,将整个文件读入 get_all
(不再连接到文件的列表),然后以 r+
模式重新打开文件,然后迭代该列表(但不是文件!)直到你得到你想要覆盖的行,然后写入新打开的文件。
这应该行不通,因为新打开的文件仍在第一行,即使您循环列表也是如此。
相反,我建议直接替换列表中的值,并用 'w'
覆盖整个文件。
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
if event == 'Event 1':
get_all[0] = team_1.get() + "\n"
get_all[1] = team_2.get() + "\n"
get_all[2] = team_3.get() + "\n"
get_all[3] = team_4.get() + "\n"
if event == 'Event 2':
get_all[4] = team_1.get() + "\n"
get_all[5] = team_2.get() + "\n"
get_all[6] = team_3.get() + "\n"
get_all[7] = team_4.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
您可以通过将要替换的行和相应的团队变量放入列表和列表字典中,然后压缩和循环来进一步减少代码重复(特别是如果有更多 "events" 这样的代码)这些列表:
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
get_all[line] = team.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
注意:我没有对此进行任何测试。
类似于从文件中读取团队。 (注意:我将 list 和 dict 移出了方法,因为它总是相同的,这样它就可以被阅读和写作团队重复使用。)
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
filename = "data/tournamentdatae1.txt"
def showteam():
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
team.set(get_all[line].strip())
I setup a button to work with two possibilities from a combo box so that if Event 1 is chosen it will save to lines 1 to 4 which works fine and saves with no problems but when Event 2 is chosen it should save 5 到 8 但它不能正常工作 5 会把它自己放在更正的行中,但 6 会向下飞 10 行,我昨天花了很多时间添加和删除代码但没有 gold.any 想法?
def saveteam():
with open("data/tournamentdatae1.txt",'r') as f:
get_all=f.readlines()
event = combo_event.get()
if event == 'Event 1':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 1:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 2:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 3:
input3 = team_3.get()
f.writelines(input3)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 4:
input4 = team_4.get()
f.writelines(input4)
f.writelines("\n")
if event == 'Event 2':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 5:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
else:
f.writelines(line)
for i,line in enumerate(get_all,1):
if i == 6:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
else:
f.writelines(line)
这就是我所做的,但不确定如何使用它来制作节目
if event == 'Event 1':
if indivcombo == 'Individual 1':
get_all[20] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[21] = individual.get() + "\n"
if indivcombo == 'Individual 3':
get_all[22] = individual.get() + "\n"
if indivcombo == 'Individual 4':
get_all[23] = individual.get() + "\n"
if indivcombo == 'Individual 5':
get_all[24] = individual.get() + "\n"
if indivcombo == 'Individual 6':
get_all[25] = individual.get() + "\n"
if indivcombo == 'Individual 7':
get_all[26] = individual.get() + "\n"
if indivcombo == 'Individual 8':
get_all[27] = individual.get() + "\n"
if indivcombo == 'Individual 9':
get_all[28] = individual.get() + "\n"
if indivcombo == 'Individual 10':
get_all[29] = individual.get() + "\n"
if indivcombo == 'Individual 11':
get_all[30] = individual.get() + "\n"
if indivcombo == 'Individual 12':
get_all[31] = individual.get() + "\n"
if indivcombo == 'Individual 13':
get_all[32] = individual.get() + "\n"
if indivcombo == 'Individual 14':
get_all[33] = individual.get() + "\n"
if indivcombo == 'Individual 15':
get_all[34] = individual.get() + "\n"
if indivcombo == 'Individual 16':
get_all[35] = individual.get() + "\n"
if indivcombo == 'Individual 17':
get_all[36] = individual.get() + "\n"
if indivcombo == 'Individual 18':
get_all[37] = individual.get() + "\n"
if indivcombo == 'Individual 19':
get_all[38] = individual.get() + "\n"
if indivcombo == 'Individual 20':
get_all[39] = individual.get() + "\n"
if event == 'Event 2':
if indivcombo == 'Individual 1':
get_all[40] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[41] = individual.get() + "\n"
如果我对你的理解是正确的(大如果),那么你想用 team_X
变量的值替换文件中的某些行。你这样做的方式是,将整个文件读入 get_all
(不再连接到文件的列表),然后以 r+
模式重新打开文件,然后迭代该列表(但不是文件!)直到你得到你想要覆盖的行,然后写入新打开的文件。
这应该行不通,因为新打开的文件仍在第一行,即使您循环列表也是如此。
相反,我建议直接替换列表中的值,并用 'w'
覆盖整个文件。
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
if event == 'Event 1':
get_all[0] = team_1.get() + "\n"
get_all[1] = team_2.get() + "\n"
get_all[2] = team_3.get() + "\n"
get_all[3] = team_4.get() + "\n"
if event == 'Event 2':
get_all[4] = team_1.get() + "\n"
get_all[5] = team_2.get() + "\n"
get_all[6] = team_3.get() + "\n"
get_all[7] = team_4.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
您可以通过将要替换的行和相应的团队变量放入列表和列表字典中,然后压缩和循环来进一步减少代码重复(特别是如果有更多 "events" 这样的代码)这些列表:
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
get_all[line] = team.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
注意:我没有对此进行任何测试。
类似于从文件中读取团队。 (注意:我将 list 和 dict 移出了方法,因为它总是相同的,这样它就可以被阅读和写作团队重复使用。)
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
filename = "data/tournamentdatae1.txt"
def showteam():
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
team.set(get_all[line].strip())