如果你运行这个小GUI,并插入几个例子(q1,a1,h1)点击“保存/下一个”(q2,a2,h1,)保存/下一个(q3,a3,h3)保存/下一个。然后单击编辑/确认保存按钮。它将带来第一个(q1,a1,h1)将一些内容更改为ex :(问题1,答案1,提示1)点击“保存条目#0”按钮。它将它保存在[[0] []]的正确位置,即列表[item0]。现在进行下一次编辑,将内容更改为(问题2,答案2,提示2)然后再次点击“保存条目#1”。这是我需要帮助的地方。我用于调试的打印输出显示列表项[[0] [1]]为零和一个都已使用应该在列表1中的项目进行了更改。然后我丢失了清单0中的东西。为什么会这样?
from tkinter import *
root= Tk()
n=0
x = 10
y = 10
single_questions = []
all_questions = []
# clear all the entries or Text
def clear(event=None):
l = [Quest_Entry, Ans_Entry, Hint_Entry]
for i in l:
i.delete(1.0, 'end')
# add single questions to all questions
def add_to_all_questions(new_list):
print ("appending : " + str(list(new_list)))
all_questions.append(list(new_list))
# saves single questions, call add_to_all_question and clears screen.
def save_next(event=None):
l = [Quest_Entry, Ans_Entry, Hint_Entry]
for i in l:
single_questions.append(i.get(1.0,'end').strip())
clear()
Quest_Entry.focus()
add_to_all_questions(single_questions)
single_questions.clear()
# first method of button check confirm
def check_confirm(event=None):
''' should disappear after initiation and set back to n=0'''
global n
print ("Confirm btn pressed")
save_next.configure(state="disabled")
confirm_btn.grid_forget()
confirm.grid(column=2, row=4, padx=x, pady=y)
clear()
n=0
Incrementer()
# Increment then display the following questions
def Incrementer(event=None):
global n
root.update_idletasks()
if n < len(all_questions):
Quest_Entry.insert(1.0,all_questions[n][0])
Ans_Entry.insert(1.0,all_questions[n][1])
Hint_Entry.insert(1.0,all_questions[n][2])
confirm.configure(text="Save Entry #{}".format(n))
else:
Quest_Entry.insert(1.0, "No more Entries Click \
Main to close course or back to return to options.")
save_next.configure(state="normal")
confirm.configure(state="disabled")
return
# confirm what's being displayed and save new changes
def save_new_confirm(event=None):
global n
l = [Quest_Entry, Ans_Entry, Hint_Entry]
print ("currently editing list index {} ".format(n))
print ("before list")
for i in range(len(all_questions)):
print ("list # {}".format(i))
for x in all_questions[i]:
print ("\titem: {}".format(x))
print("clearing {} from single question".format(single_questions))
single_questions.clear()
for i in l:
single_questions.append(i.get(1.0,'end').strip())
print("singles now has {}".format(single_questions))
print("Need to add: {} at list {}".format(single_questions, n))
print ("popping: {} from list {}".format(all_questions[n], n))
all_questions[n] = single_questions
all_questions.pop(n)
print ("now list has {}".format(all_questions))
all_questions.insert(n, single_questions)
print ("\nafter list")
for i in range(len(all_questions)):
print ("list # {}".format(i))
for x in all_questions[i]:
print ("\titem: {}".format(x))
n+=1
clear()
Incrementer()
# Labels
Quest_Label = Label(root, text='Question', font=('Helvetica',15))
Quest_Label.grid(column=0, row=1, padx=x, pady=y)
Ans_Label = Label(root, text='Answer', font=('Helvetica',15))
Ans_Label.grid(column=0, row=2, padx=x, pady=y)
Hint_Label = Label(root, text='Hint', font=('Helvetica',15))
Hint_Label.grid(column=0, row=3, padx=x, pady=y)
# All the Entries or Texts
Quest_Entry = Text(root, font=('Helvetica',12),height=3,bg='light blue',
fg='black')
Quest_Entry.grid(column=1, columnspan=4, row=1, padx=x, pady=y)
Ans_Entry = Text(root, font=('Helvetica',12),height=3,bg='light blue',
fg='black')
Ans_Entry.grid(column=1, columnspan=4, row=2, padx=x, pady=y)
Hint_Entry = Text(root, font=('Helvetica',12),height=3,bg='light blue',
fg='black')
Hint_Entry.grid(column=1, columnspan=4, row=3, padx=x, pady=y)
# Buttons
save_next = Button(root, text='Save/Next',bg="light blue",font=('Helvetica',
12),command=save_next)
save_next.grid(column=1, row=4, padx=x, pady=y)
confirm_btn = Button(root, text='edit/confirm saved'
,bg="light blue", font=('Helvetica', 12),command=check_confirm)
confirm_btn.grid(column=2, row=4, padx=x, pady=y)
confirm = Button(root, text='edit/confirm saved'
,bg="light blue", font=('Helvetica', 12),command=save_new_confirm)
# call main loop
root.configure(bg="gray")
root.mainloop()
答案可能是我忘了做的小事。但任何帮助将不胜感激。我只是问,因为我已经在这里待了几个小时了。
问题是single_questions.clear()
。它删除元素,但它保留对内存中相同位置的引用,因此两个问题都使用相同的位置。
我使用single_questions = []
创建新的空列表(并且它不必是全局列表)
import tkinter as tk
# clear all the entries or Text
def clear_entries(event=None):
for entry in all_entries:
entry.delete(1.0, 'end')
# saves single questions, call add_to_all_question and clears screen.
def save_next(event=None):
# create new empty list
new_questions = []
for entry in all_entries:
new_questions.append(entry.get(1.0,'end').strip())
print("appending:", new_questions)
all_questions.append(new_questions)
# clear entries
clear_entries()
quest_entry.focus()
# first method of button check confirm
def check_confirm(event=None):
''' should disappear after initiation and set back to n=0'''
global n
print("Confirm btn pressed")
save_next.configure(state="disabled")
confirm_btn.grid_forget()
confirm.grid(column=2, row=4, padx=x, pady=y)
n = 0
clear_entries()
incrementer()
# Increment then display the following questions
def incrementer(event=None):
global n
root.update_idletasks()
if n < len(all_questions):
quest_entry.insert(1.0, all_questions[n][0])
ans_entry.insert(1.0, all_questions[n][1])
hint_entry.insert(1.0, all_questions[n][2])
confirm.configure(text="Save Entry #{}".format(n))
else:
quest_entry.insert(1.0, "No more Entries Click \
Main to close course or back to return to options.")
save_next.configure(state="normal")
confirm.configure(state="disabled")
# confirm what's being displayed and save new changes
def save_new_confirm(event=None):
global n
print("currently editing list index:", n)
display_list("before list")
# create empty list for new values
new_single = []
for entry in all_entries:
new_single.append(entry.get(1.0,'end').strip())
print("new single:", new_single)
all_questions[n] = new_single
print("now list has:", all_questions)
display_list("\nafter list")
n += 1
clear_entries()
incrementer()
def display_list(text=None):
if text:
print(text)
for data in all_questions:
print("list #", data)
for item in data:
print("\titem:", item)
# --- main ---
n = 0
x = 10
y = 10
all_questions = []
root = tk.Tk()
# Labels
quest_label = tk.Label(root, text='Question', font=('Helvetica',15))
quest_label.grid(column=0, row=1, padx=x, pady=y)
ans_label = tk.Label(root, text='Answer', font=('Helvetica',15))
ans_label.grid(column=0, row=2, padx=x, pady=y)
hint_label = tk.Label(root, text='Hint', font=('Helvetica',15))
hint_label.grid(column=0, row=3, padx=x, pady=y)
# All the Entries or Texts
quest_entry = tk.Text(root, font=('Helvetica',12), height=3, bg='light blue', fg='black')
quest_entry.grid(column=1, columnspan=4, row=1, padx=x, pady=y)
ans_entry = tk.Text(root, font=('Helvetica',12), height=3, bg='light blue', fg='black')
ans_entry.grid(column=1, columnspan=4, row=2, padx=x, pady=y)
hint_entry = tk.Text(root, font=('Helvetica',12), height=3, bg='light blue', fg='black')
hint_entry.grid(column=1, columnspan=4, row=3, padx=x, pady=y)
all_entries = [quest_entry, ans_entry, hint_entry]
# Buttons
save_next = tk.Button(root, text='Save/Next', bg="light blue", font=('Helvetica',12), command=save_next)
save_next.grid(column=1, row=4, padx=x, pady=y)
confirm_btn = tk.Button(root, text='edit/confirm saved', bg="light blue", font=('Helvetica',12), command=check_confirm)
confirm_btn.grid(column=2, row=4, padx=x, pady=y)
confirm = tk.Button(root, text='edit/confirm saved', bg="light blue", font=('Helvetica',12), command=save_new_confirm)
# call main loop
root.configure(bg="gray")
root.mainloop()