Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 名称错误:未定义全局名称';定义错误后创建错误_Python_Python 3.x_Tkinter_Listbox - Fatal编程技术网

Python 名称错误:未定义全局名称';定义错误后创建错误

Python 名称错误:未定义全局名称';定义错误后创建错误,python,python-3.x,tkinter,listbox,Python,Python 3.x,Tkinter,Listbox,我正在尝试创建一个程序,它将加载一个文件(称为lines.txt),向其中添加行并删除行,我使用tkinter作为gui。但是,我无法完成添加行部分,因为它总是返回“NameError:global name not defined'creating error when is defined when is defined.”,它在第29行定义为listbox=listbox(root),失败的部分位于第95行def add条形码(self):中列表框。插入(结束,术语) 从tkinter导入

我正在尝试创建一个程序,它将加载一个文件(称为lines.txt),向其中添加行并删除行,我使用tkinter作为gui。但是,我无法完成添加行部分,因为它总是返回“NameError:global name not defined'creating error when is defined when is defined.”,它在第29行定义为
listbox=listbox(root)
,失败的部分位于第95行
def add条形码(self):
中<代码>列表框。插入(结束,术语)

从tkinter导入Tk、帧、菜单
导入系统、时间、数学
从tkinter进口*
尝试:
root=Tk()
root.wm_标题(“条形码管理器”)
根几何(“250x150+300+300”)
类示例(框架):
定义初始化(自身,父级):
帧。\uuuu初始化(自,父)
self.parent=parent
self.initUI()
def initUI(self):
self.parent.title(“注册条形码”)
菜单栏=菜单(self.parent)
self.parent.config(menu=menubar)
文件菜单=菜单(菜单栏)
fileMenu.add_命令(label=“add Barcode”,command=self.add_Barcode)
fileMenu.add_命令(label=“Save”,command=self.Save)
fileMenu.add_命令(label=“Exit”,command=self.onExit)
menubar.add_级联(label=“File”,menu=fileMenu)
列表框=列表框(根)
pack(fill=BOTH,expand=1)
def弹出菜单()
菜单=菜单(根,tearoff=0)
菜单.添加命令(label=“Edit”,command=lambda:print(“hello”))
菜单.add_命令(label=“Delete”,command=lambda:print(DeleteSelection(self)))
def弹出窗口(事件):
menu.post(event.x_root,event.y_root)
listbox.bind(“,弹出窗口)
弹出菜单()
def get_行():
lines=open('lines.txt','r')
对于行中的行:
条形码,文本=行。拆分(',')
text.strip(“\n”)
行=':'。连接(str(x)表示x in(“GTIN#”,条形码,文本))
列表框.插入(结束,行)
获取_行()
def删除选择(自):
items=listbox.cursection()
pos=0
对于项目中的i:
idx=整数(i)-位置
listbox.delete(idx,idx)
位置=位置+1
def添加_条形码(自身):
将tkinter作为tk导入
nroot=Toplevel()
nroot.wm_标题(“添加条形码”)
textbox\u lbl=标签(nroot,text=“text”)
文本框包装(侧面=左侧,填充=Y)
文本框=输入(nroot,bd=2)
textbox.pack(侧=左,填充=Y)
textbox.pack(锚定=中心)
条形码\u lbl=标签(nroot,text=“条形码”)
条形码包装(侧面=左侧,填充=Y)
条形码=输入(nroot,bd=2)
条形码.包装(侧面=左侧,填充=Y)
条形码包(锚定=中心)
def添加(自我):
非本地文本框
非本地条码
text=textbox.get()
条形码=条形码。获取()
nroot.destroy()
a=0
对于条形码中的项目:
a+=1
如果a==7:
尝试:
条形码=获取检查数字()
条形码=整数(条形码)
打印(条形码)
term=':'。连接(str(x)表示x in(“GTIN#”,条形码,文本))
印刷品(学期)
列表框.插入(结束,术语)
除了():
错误=顶级()
错误。wm_标题(“错误”)
错误\标签=标签(错误,text=“请只输入数字。”)
标签包装错误(侧面=顶部,填充=X)
其他:
错误=顶级()
错误。wm_标题(“错误”)
错误\标签=标签(错误,text=“错误”)
标签包装错误(侧面=顶部,填充=X)
错误\u label1=标签(错误,text=“请输入7位”)
错误标签1.pack(侧面=顶部,填充=X)
时间。睡眠(5)
error.destroy()
def Get_Check_Digit():
检查表=[]
数字=1
非本地条码
对于条形码中的项目:
检查项目=整数(项目)*整数(编号)
检查表。附加(检查项目)
如果数字==3:
数字=1
elif编号==1:
数字=3
检查表=总和(检查表)
def综述(x):
返回int(math.ceil(x/10.0))*10
检查数字=汇总(检查表)
检查数字-=检查表
num=”“.连接(str(x)表示x英寸(条形码,校验位))
返回数
btn=按钮(nroot,text=“Add”,command=lambda:Add(self))
btn.包装(侧面=底部,填充=Y)
def保存(自我):
全局列表框
对于列表框中的项目:
打印(项目)
def onExit(self):
root.destroy()
sys.exit()
app=示例(根)
root.mainloop()
除了():
通过

如果您想稍后在示例类中使用listbox变量,则需要使用self声明它,例如:

self.listbox = Listbox(root)
然后在以后使用它,如:

self.listbox.insert(END, term)

如果希望稍后在示例类中使用listbox变量,则需要使用self声明它,例如:

self.listbox = Listbox(root)
然后在以后使用它,如:

self.listbox.insert(END, term)

把例外情况隐藏起来是不好的做法