pythontkinterexport-to-text

NameError: free variable 'mywidget' referenced before assignment in enclosing scope


When I export the text file from openproject() it works fine and exported the text file but when I export from other fn such as openuser() its show NameError: free variable 'mywidget' referenced before assignment in enclosing scope. How come it does work on one function but not on others? If I separate each defined function into separate projects, its working fine but when I merge three functions into one GUI, the first one is only working. I'm sorry for my stupid codes as I'm new to Python and also to this forum. Thank you.

Below is my code>

from tkinter import *

def openproject():
def save():
    with open('project.txt', 'w') as out:
        for label, entry in mywidget:
            out.write(label['text'] + " " + entry.get() + '\n')
    out.close()

project = Tk()
myLabel1 = Label(project, text="FIS Projects")

myLabel2 = Label(project, text="FIS_ID")
FIS_ID = StringVar()
entry_box1 = Entry(project, textvariable=FIS_ID, width=80)
entry_box1.insert(0, "PRA")
entry_box1.get()

myLabel3 = Label(project, text="FIS_Owner_Name")
FIS_Owner_Name = StringVar()
entry_box2 = Entry(project, textvariable=FIS_Owner_Name, width=80)
entry_box2.insert(0, "PR1MA Communications")

myLabel4 = Label(project, text="FIS_Contact_Details")
FIS_Contact_Details = StringVar()
entry_box3 = Entry(project, textvariable=FIS_Contact_Details, width=80)
entry_box3.insert(0, "Support phone:019-6304240")
entry_box34 = Entry(project, textvariable=StringVar(), width=80)
entry_box34.insert(0, "Email: enquiry@pr1macomm.my")
entry_box35 = Entry(project, textvariable=StringVar(), width=80)
entry_box35.insert(0, "Help-desk : 03-76289898")
entry_box36 = Entry(project, textvariable=StringVar(()), width=80)
entry_box36.insert(0, " ")

myLabel5 = Label(project, text="FIS_Element_ID")
FIS_Element_ID = StringVar()
entry_box4 = Entry(project, textvariable=FIS_Element_ID, width=80)
entry_box4.insert(0, "PRA123456")

myLabel6 = Label(project, text="FIS_Element_Type")
etype = ["Cable","ODF","FWS","FDC"]
clicked1 = StringVar(project)
clicked1.set("Cable")
drop1 = OptionMenu(project, clicked1, *etype)


myLabel7 = Label(project, text="FIS_Installation_Date")
FIS_Installation_Date = StringVar()
entry_box6 = Entry(project, textvariable=FIS_Installation_Date, width=80)
entry_box6.insert(0, "YYYY/MM/DD")
button1 = Button(project, text="Save")
button2 = Button(project, text="Edit")
button3 = Button(project, text="Delete")
button4 = Button(project, text="Export", command=save)
# tkinter display setting
myLabel1.grid(row=0, column=2)
myLabel2.grid(row=2, column=0)
entry_box1.grid(row=2, column=2)
myLabel3.grid(row=3, column=0)
entry_box2.grid(row=3, column=2)
myLabel4.grid(row=4, column=0)
entry_box3.grid(row=4, column=2)
entry_box34.grid(row=5, column=2)
entry_box35.grid(row=6, column=2)
entry_box36.grid(row=7, column=2)
myLabel5.grid(row=9, column=0)
entry_box4.grid(row=9, column=2)
myLabel6.grid(row=10, column=0)
drop1.grid(row=10, column=2)
myLabel7.grid(row=11, column=0)
entry_box6.grid(row=11, column=2)
button1.grid(row=12, column=3)
button2.grid(row=12, column=4)
button3.grid(row=12, column=5)
button4.grid(row=12, column=6)
project.title("FIS_Projects")
mywidget = [(myLabel2, entry_box1), (myLabel3, entry_box2), (myLabel4, entry_box3), (myLabel5, entry_box4), (myLabel6,clicked1), (myLabel7, entry_box6)]
project.mainloop()

  def openuser():

def save1():
    with open('user.txt', 'w') as out:
        for label,entry in mywidget :
            out.write(label['text']+" " +entry.get() + '\n')
    out.close()


if __name__ == '__main__':
    user = Tk()
    myLabel1 = Label(user, text="FIS_PRA_USERS")

    myLabel2 = Label(user, text="FIS_ID")
    FIS_ID = StringVar()
    entry_box1 = Entry(user, textvariable=FIS_ID, width=70)
    entry_box1.insert(0, "PRA")

    myLabel3 = Label(user, text="FIS_User_Name")
    FIS_User_Name = StringVar()
    entry_box2 = Entry(user, textvariable=FIS_User_Name, width=70)
    entry_box2.insert(0, "User Full Name")

    myLabel4 = Label(user, text="FIS_User_Email")
    FIS_User_Email = StringVar()
    entry_box3 = Entry(user, textvariable=FIS_User_Email, width=70)
    entry_box3.insert(0, "boss@allnetworks.com")

    myLabel5 = Label(user, text="FIS_User_Password")
    FIS_User_Password = StringVar()
    entry_box4 = Entry(user, textvariable=FIS_User_Password, width=70)
    entry_box4.insert(0, "xxxxxxxx")

    myLabel6 = Label(user, text="FIS_User_Type")
    utype = ["Admin", "Supervisor", "User", "ReadOnly"]
    clicked1 = StringVar(user)
    clicked1.set("Admin")
    drop1 = OptionMenu(user, clicked1, *utype)
    

    myLabel7 = Label(user, text="FIS_User_Fist_Seen")
    FIS_User_First_Seen = StringVar()
    entry_box5 = Entry(user, textvariable=FIS_User_First_Seen, state='normal', width=70)
    entry_box5.insert(0, "YYYY/MM/DD-HH-SS")

    myLabel8 = Label(user, text="FIS_User_Last_Seen")
    FIS_User_Last_Seen = StringVar()
    entry_box6 = Entry(user, textvariable=FIS_User_Last_Seen, width=70)
    entry_box6.insert(0, "YYYY/MM/DD-HH-SS")
    button1 = Button(user, text="Save")
    button2 = Button(user, text="Edit")
    button3 = Button(user, text="Delete")
    button4 = Button(user, text="Export", command=save1())

    # tkinter display setting
    myLabel1.grid(row=0, column=2)
    myLabel2.grid(row=2, column=0)
    entry_box1.grid(row=2, column=2)
    myLabel3.grid(row=3, column=0)
    entry_box2.grid(row=3, column=2)
    myLabel4.grid(row=4, column=0)
    entry_box3.grid(row=4, column=2)
    myLabel5.grid(row=5, column=0)
    entry_box4.grid(row=5, column=2)
    myLabel6.grid(row=6, column=0)
    drop1.grid(row=6, column=2)
    myLabel7.grid(row=7, column=0)
    entry_box5.grid(row=7, column=2)
    myLabel8.grid(row=8, column=0)
    entry_box6.grid(row=8, column=2)
    button1.grid(row=9, column=3)
    button2.grid(row=9, column=4)
    button3.grid(row=9, column=5)
    button4.grid(row=9, column=6)
    
    mywidget = [(myLabel2, entry_box1), (myLabel3, entry_box2), (myLabel4, entry_box3), (myLabel5,         entry_box4),(myLabel6, clicked1), (myLabel7, entry_box5), (myLabel8, entry_box6)]
    user.title("FIS_PRA_USERS")
user.mainloop()

def opendept():
def save2():
    mywidget = mywidget2
    with open('Department.txt', 'w') as out:
        for label, entry in mywidget:
            out.write(label['text'] + " " + entry.get() +'\n')
dept = Tk()
myLabel1 = Label(dept, text="FIS_ALL_Departments")

myLabel2 = Label(dept, text="FIS_PRA_Departments")
dtype = ["FIS_PRA_Dept_S1: [S1 = Yangon to Bago] ", "FIS_PRA_Dept_S2: [S2 = Bago to Panwegon]", "FIS_PRA_Dept_S3: [S3 = Panwegon to Taungoo]", "FIS_PRA_Dept_S4: [S4 = Taungoo to Naypyidaw]", "FIS_PRA_Dept_S5: [S5 = Naypyidaw to Pyawbwe]", "FIS_PRA_Dept_S6: [S6 = Pyawbwe to Thazi]", "FIS_PRA_Dept_S7: [S7 = Thazi to Kyaukse]", "FIS_PRA_Dept_S8: [S8 = Kyaukse to Mandalay]"]
clicked1 = StringVar(dept)
clicked1.set("FIS_PRA_Dept_S1: [S1 = Yangon to Bago]")
drop1 = OptionMenu(dept, clicked1, *dtype)

button1 = Button(dept, text="Save")
button2 = Button(dept, text="Edit")
button3 = Button(dept, text="Delete")
button4 = Button(dept, text="Export", command=save2())
# tkiner display setting
myLabel1.grid(row=0, column=2)
myLabel2.grid(row=1, column=0)
drop1.grid(row=1, column=2)
button1.grid(row=4, column=3)
button2.grid(row=4, column=4)
button3.grid(row=4, column=5)
button4.grid(row=4, column=6)
mywidget2 = [(myLabel2, clicked1)]



dept.mainloop()

root = Tk()
btn1 = Button(root, text="FILE1: FIS_Projects", command=openproject)
btn1.grid(row=3, column=2)
btn2 = Button(root, text="FILE2: FIS_PRA_USERS", command=openuser)
btn2.grid(row=4, column=2)
btn3 = Button(root, text="FILE3: FIS_ALL_Departments", command=opendept)
btn3.grid(row=5, column=2)

Solution

  • It is because you used command=save1() in the following line:

    button4 = Button(user, text="Export", command=save1())
    

    which execute save1() immediately and at that time mywidget is not defined yet.

    You should use command=save1 instead:

    button4 = Button(user, text="Export", command=save1)