简体   繁体   中英

I am getting an error: “UnboundLocalError: local variable 'text_to_print' referenced before assignment”

def search():

        def back():
            searchwindow.destroy()

        def submit():
            inputID = ID.get()
            if inputID == "":
                messagebox.showerror("Error", "Please Enter An ID")
            elif len(inputID) != 6:
                messagebox.showerror("Error", "ID Must Be 6 Characters Long")
            else:
                cursor.execute(("SELECT * FROM tblRoomAllocation WHERE roomID = ?"), (inputID,))
                records = cursor.fetchall()
                for record in records:
                    text_to_print = str(record[0]) + " | " + str(record[1]) + " | " + str(record[2]) + " | " + str(record[3]) + " | " + str(record[4]) 
                messagebox.showinfo("Search Results", text_to_print)

I tried to make "text_to_print" global and it is still giving me the same error. I am only a beginner to this so please help out. Thank you for any help.

Put the last line in the else:

for record in records:
    text_to_print = str(record[0]) + " | " + str(record[1]) + " | " + str(record[2]) + " | " + str(record[3]) + " | " + str(record[4]) 
if records:
     messagebox.showinfo("Search Results", text_to_print)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM