NameError: free variable 'mywidget' referenced before assignment in enclosing scope
NameError: free variable 'mywidget' referenced before assignment in enclosing scope
当我从 openproject() 导出文本文件时,它工作正常并导出文本文件,但是当我从其他 fn(例如 openuser() 导出时,它显示 NameError: free variable 'mywidget' referenced before assignment in封闭范围。
为什么它对一个功能有效但对其他功能无效?如果我将每个定义的函数分成单独的项目,它工作正常,但是当我将三个函数合并到一个 GUI 中时,第一个只能工作。对于我的愚蠢代码,我很抱歉,因为我是 Python 和这个论坛的新手。谢谢。
下面是我的代码>
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)
这是因为你在下面一行中使用了command=save1()
:
button4 = Button(user, text="Export", command=save1())
立即执行save1()
,此时mywidget
尚未定义。
您应该改用 command=save1
:
button4 = Button(user, text="Export", command=save1)
当我从 openproject() 导出文本文件时,它工作正常并导出文本文件,但是当我从其他 fn(例如 openuser() 导出时,它显示 NameError: free variable 'mywidget' referenced before assignment in封闭范围。 为什么它对一个功能有效但对其他功能无效?如果我将每个定义的函数分成单独的项目,它工作正常,但是当我将三个函数合并到一个 GUI 中时,第一个只能工作。对于我的愚蠢代码,我很抱歉,因为我是 Python 和这个论坛的新手。谢谢。
下面是我的代码>
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)
这是因为你在下面一行中使用了command=save1()
:
button4 = Button(user, text="Export", command=save1())
立即执行save1()
,此时mywidget
尚未定义。
您应该改用 command=save1
:
button4 = Button(user, text="Export", command=save1)