曾试图寻找其他类似的问题,但不能这样做,我们开始吧。
我正在使用名为tkinter的python上的gui库来构建dektop应用程序。以下是代码内的函数之一,该函数在按下按钮后加载一个完整的帧(类似于应用程序的页面)。它只需要一个称为pom的元组,并(带有for循环)在框架中显示其中的信息,同时它允许用户通过输入条目在其上编辑一些值。
我想制作一个保存按钮,该按钮将调用外部函数,为其提供新的已编辑元组,因此我可以继续执行,但由于某些原因,在for循环工作后我无法编写任何代码。
我想知道为什么。我已经有一个小时的时间了,会一直努力直到找出这件事到底是什么。预先谢谢你。
def edit_pom(pom):
#Edit frame set up.
editFrame.tkraise()
IdLabel = tk.Label(editFrame, text=str("Editing "+str(pom[-1])), bg=FRAME_BG_COLOR, font=(FONT, 28), fg="white")
IdLabel.place(relx=1/2, rely=1/45, relwidth=1, anchor="n")
dataFrame = tk.Frame(editFrame, bg=FRAME_BG_COLOR)
dataFrame.place(relx=1/2, rely=(1/20)+(1/6), width=WIDTH, relheight=1/2, anchor="n")
titles = ["dummy","Time","Date","Description","Code","Minutes"]
entries = []
index = 1
for i in pom:
label1 = tk.Label(dataFrame, text=titles[index]+":", bg=FRAME_BG_COLOR, font=(FONT, 18), fg="white")
label1.grid(row=index, column=0, sticky="W")
if titles[index] == "Code" or titles[index] == "Description":
Entry1 = tk.Entry(dataFrame)
Entry1.grid(row=index, column=1, sticky="E")
Entry1.insert(0, i)
entries.append(Entry1)
else:
label2 = tk.Label(dataFrame, text=i, bg=FRAME_BG_COLOR, font=(FONT, 18), fg="white")
label2.grid(row=index, column=1, sticky="E")
label1.grid_columnconfigure(0, weight=10)
label2.grid_columnconfigure(1, weight=10)
index += 1
print("This part is not being printed.")
我不知道为什么for循环会以这种方式运行。我最好的猜测是,它与tkinter的mainloop的内部功能有关。
无论如何,解决方法是手动中断 for cicle:
index = 0
for i in pom:
if index == 5:
break
label1 = tk.Label(dataFrame, text=titles[index]+":", bg=FRAME_BG_COLOR, font=(FONT, 18), fg="white")
label1.grid(row=index, column=0, sticky="W")
if titles[index] == "Code" or titles[index] == "Description":
print("on the if")
pass
else:
print("on the else")
label2 = tk.Label(dataFrame, text=i, bg=FRAME_BG_COLOR, font=(FONT, 18), fg="white")
label2.grid(row=index, column=1, sticky="E")
index += 1