2016-01-21 49 views
0

即時嘗試創建一個程序,將加載一個文件(稱爲lines.txt),添加行到它並將刪除它們,即時使用tkinter作爲GUI。然而,我不能完成add_line部分,因爲它總是返回「NameError:全局名稱未定義」,當它被定義時會產生錯誤。「,它在第29行定義爲listbox = Listbox(root),它失敗的部分在def Add_Barcode(self):def Add(self):上線95 listbox.insert(END, term)NameError:全局名稱未定義'創建錯誤時,它已被定義

from tkinter import Tk, Frame, Menu 
import sys, time, math 
from tkinter import * 

try: 
    root = Tk() 
    root.wm_title("barcode manager") 
    root.geometry("250x150+300+300") 
    class Example(Frame): 
     def __init__(self, parent): 
      Frame.__init__(self, parent) 

      self.parent = parent   
      self.initUI() 

     def initUI(self): 

      self.parent.title("Registered Barcodes.") 

      menubar = Menu(self.parent) 
      self.parent.config(menu=menubar) 

      fileMenu = Menu(menubar) 
      fileMenu.add_command(label="Add Barcode", command=self.Add_Barcode) 
      fileMenu.add_command(label="Save", command=self.Save) 
      fileMenu.add_command(label="Exit", command=self.onExit) 
      menubar.add_cascade(label="File", menu=fileMenu) 

      listbox = Listbox(root) 
      listbox.pack(fill=BOTH, expand=1) 

      def Popup_Menu(): 
       menu = Menu(root, tearoff=0) 
       menu.add_command(label="Edit", command=lambda:print("hello")) 
       menu.add_command(label="Delete", command=lambda:print(DeleteSelection(self))) 

       def popup(event): 
        menu.post(event.x_root, event.y_root) 

       listbox.bind("<Button-3>", popup) 

      Popup_Menu() 

      def get_lines(): 
       lines = open('lines.txt', 'r') 
       for line in lines: 
        barcode, text = line.split(',') 
        text.strip('\n') 
        line = ': '.join(str(x) for x in ("GTIN #", barcode, text)) 
        listbox.insert(END, line) 
      get_lines() 

      def DeleteSelection(self) : 
       items = listbox.curselection() 
       pos = 0 
       for i in items : 
        idx = int(i) - pos 
        listbox.delete(idx,idx) 
        pos = pos + 1 

      def Add_Barcode(self): 
       import tkinter as tk 
       nroot = Toplevel() 
       nroot.wm_title("Add a barcode") 

       textbox_lbl = Label(nroot, text="text") 
       textbox_lbl.pack(side=LEFT, fill=Y) 
       textbox = Entry(nroot, bd=2) 
       textbox.pack(side=LEFT, fill=Y) 
       textbox.pack(anchor=CENTER) 

       barcode_lbl = Label(nroot, text="barcode") 
       barcode_lbl.pack(side=LEFT, fill=Y) 
       barcode = Entry(nroot, bd=2) 
       barcode.pack(side=LEFT, fill=Y) 
       barcode.pack(anchor=CENTER) 

      def Add(self): 
       nonlocal textbox 
       nonlocal barcode 
       text = textbox.get() 
       barcode = barcode.get() 
       nroot.destroy() 
       a = 0 
       for item in barcode: 
        a += 1 

       if a == 7: 
        try: 
         barcode = Get_Check_Digit() 
         barcode = int(barcode) 
         print(barcode) 
         term = ': '.join(str(x) for x in ("GTIN #", barcode, text)) 
         print(term) 
         listbox.insert(END, term) 
        except(): 
         error = Toplevel() 
         error.wm_title("Error") 
         error_label = Label(error, text="please enter only numbers.")  
         error_label.pack(side=TOP, fill=X) 

       else: 
        error = Toplevel() 
        error.wm_title("Error") 
        error_label = Label(error, text="Error") 
        error_label.pack(side=TOP, fill=X) 
        error_label1 = Label(error, text="please enter 7 digits") 
        error_label1.pack(side=TOP, fill=X) 
        time.sleep(5) 
        error.destroy() 

      def Get_Check_Digit(): 
       checklist = [] 
       number = 1 
       nonlocal barcode 
       for item in barcode: 
        checkitem = int(item) * int(number) 
        checklist.append(checkitem) 
       if number == 3: 
        number = 1 
       elif number == 1: 
        number = 3 
       checklist = sum(checklist) 
       def roundup(x): 
        return int(math.ceil(x/10.0)) * 10 
       check_digit = roundup(checklist) 
       check_digit -= checklist 
       num = "".join(str(x) for x in (barcode, check_digit)) 
       return num 

      btn = Button(nroot, text="Add", command=lambda:Add(self)) 
      btn.pack(side=BOTTOM, fill=Y) 

     def Save(self): 
      global listbox 
      for item in listbox: 
       print(item) 

     def onExit(self): 
      root.destroy() 
      sys.exit() 
    app = Example(root) 
    root.mainloop() 

except(): 
    pass 
+0

這不是一個很好的做法,就像現在用'except()' –

+0

那樣隱藏異常,我只在測試它時做了它,只有它才顯示錯誤。完成的代碼將是「除(例外):」 –

回答

3

如果你想在實例類以後使用列表框的變量,你需要聲明一下,例如:

self.listbox = Listbox(root) 

,然後用它後來像:

self.listbox.insert(END, term) 
相關問題