當我退出 tk 窗口時,我收到這個長庫錯誤,而且我不知道如何修復它。GUI 本身和錯誤消息位于代碼下方。def golf_records()->None: """ This function prints the information of golfer names and the score they have """ with open("golf.txt", "r") as myfile: myfile_content = myfile.read() GUI_print(myfile_content)def GUI_print(data)->None: """ The function takes in a string found in golf.txt file and prints it out through tkinter GUI """ my_label = Label(root,text = str(data),font=("ariel",15)).place(x=250,y=120) root = Tk()root.geometry("600x600")#Lablesheader = Label(root,text = "Current Golf Records",font=("ariel",15,"bold")).place(x=150,y=20)header = Label(root,text = "-----------------------------",font=("ariel",15)).place(x=150,y=50)header = Label(root,text = "Press enter to list players data: ",font=("ariel",15)).place(x=150,y=80)#Buttonsenter = Button(root, text="Enter", activebackground = "green", command=golf_records).place(x=440,y=80)root.mainloop()if __name__ == "__main__": golf_records()
1 回答

慕萊塢森
TA貢獻1810條經驗 獲得超4個贊
基本上想象一下,您的代碼執行到mainloop()
并在那里暫停,直到您跳出循環,即退出應用程序。然后執行后的代碼mainloop()
,即為true 的語句,因此運行調用激活標簽的if
函數。golf_record()
GUI_print()
如何解決這個問題?我不確定您在這里嘗試使用此代碼做什么,但如果您可以將其移至語句root.mainloop()
末尾if
,它將在代碼最初執行時執行該函數。如果您還有更多疑問,請告訴我。或者更好的方法是擺脫golf_records()
內部的函數調用,if
因為無論如何都有一個調用該函數的按鈕。
添加回答
舉報
0/150
提交
取消