2016-03-30 8 views
0

元のコードでは、ツリーにデータを挿入するためのリストを使用していたことを除けば、ほとんどの場合、私がやりたいことができます。私は、挿入されるデータをSQLテーブルから呼び出して正しいカラムに出力したい。私は、これまでのコードは次のようになります。SQLテーブルをツリーに挿入する

import tkinter as tk 
import tkinter.font as tkFont 
import tkinter.ttk as ttk 
import sqlite3 
class MultiColumnListbox(object): 
    """use a ttk.TreeView as a multicolumn ListBox""" 

    def __init__(self): 
     self.tree = None 
     self._setup_widgets() 
     self._build_tree() 

    def _setup_widgets(self): 
     s = """\click on header to sort by that column 
     to change width of column drag boundary 
     """ 
     msg = ttk.Label(wraplength="4i", justify="left", anchor="n", 
      padding=(10, 2, 10, 6), text=s) 
     msg.pack(fill='x') 
     container = ttk.Frame() 
     container.pack(fill='both', expand=True) 
     # create a treeview with dual scrollbars 
     self.tree = ttk.Treeview(columns=getvariablesC, show="headings") 
     vsb = ttk.Scrollbar(orient="vertical", 
      command=self.tree.yview) 
     hsb = ttk.Scrollbar(orient="horizontal", 
      command=self.tree.xview) 
     self.tree.configure(yscrollcommand=vsb.set, 
      xscrollcommand=hsb.set) 
     self.tree.grid(column=0, row=0, sticky='nsew', in_=container) 
     vsb.grid(column=1, row=0, sticky='ns', in_=container) 
     hsb.grid(column=0, row=1, sticky='ew', in_=container) 
     container.grid_columnconfigure(0, weight=1) 
     container.grid_rowconfigure(0, weight=1) 

    def _build_tree(self): 
     for col in getvariablesC: 
      self.tree.heading(col, text=col.title(), 
       command=lambda c=col: sortby(self.tree, c, 0)) 
      # adjust the column's width to the header string 
      self.tree.column(col, 
       width=tkFont.Font().measure(col.title())) 
    def sqlcode(): 
     db = sqlite3.connect('File') 
     cursor = db.cursor() 
     sql1="Select * FROM ProductTable" 
     cursor.execute(sql1) 
     result = cursor.fetchall() 
     count = (len(result)) 
     for item in result: 
      self.tree.insert('', 'end', values=item) 
      # adjust column's width if necessary to fit each value 
      for ix, val in enumerate(item): 
       col_w = tkFont.Font().measure(val) 
       if self.tree.column(getvariablesC[ix],width=None)<col_w: 
        self.tree.column(getvariablesC[ix], width=col_w) 
     cursor.close() 

    sqlcode() 

    getvariablesC= ["CustomerID","Title","Name","Address1","Address2","Town","County","PostCode","STDCode","HomeNo","MobileNo","Email","Source","Month","Year"] 


if __name__ == '__main__': 
    root = tk.Tk() 
    root.title("Multicolumn Treeview/Listbox") 
    listbox = MultiColumnListbox() 
    root.mainloop() 

私が午前主な問題は、コードが実行されるとselfが定義されていないことですが、私は、エラーの原因となっているものがわからないよ:

NameError: global name 'self' is not defined

+0

は、ラインレベルのエラーのように見えます。あなたのケースでは、MultiColumnListboxクラスといくつかの独立したメソッドがあります。インデントを修正する方法の前にスペース(タブ)を追加してください。 –

+0

インデントが正しくないようです。それを指摘してくれてありがとう。 – Coder101

答えて

2

機能定義にselfがありません。あなたは変更する必要があります。

def sqlcode(self): 

def sqlcode(): 

+0

私はその調整をしましたが、エラー'NameError:name 'self' is not defined'が発生しました。 – Coder101

+0

クラスの下で正しいインデントを維持する必要があります。例えば、 'def __init __(self):'のための数字スペースは 'def sqlcode(self):'と同じでなければなりません – krishv