Python 将SQL表插入到树中

Python 将SQL表插入到树中,python,sql,tkinter,tree,listbox,Python,Sql,Tkinter,Tree,Listbox,我设法改进了一段代码,该代码在很大程度上完成了我希望它完成的任务,除了原始代码使用列表将数据插入到树中。我希望将要插入的数据从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

我设法改进了一段代码,该代码在很大程度上完成了我希望它完成的任务,除了原始代码使用列表将数据插入到树中。我希望将要插入的数据从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,但我不确定是什么导致了错误:

名称错误:未定义全局名称“self”

函数定义中缺少self。您应该更改:

def sqlcode():

函数定义中缺少self。您应该更改:

def sqlcode():


看起来像是行级错误。在您的例子中,有多列ListBox类和几个独立的方法。请在修复缩进的方法之前添加空格选项卡。缩进似乎不正确。现在应该是了,谢谢你指出这一点。看起来像是行级错误。在您的例子中,有多列ListBox类和几个独立的方法。请在修复缩进的方法之前添加空格选项卡。缩进似乎不正确。应该是现在,谢谢你指出这一点。我已经做了调整,但现在出现了错误名称错误:名称“self”没有定义。你必须在你的类下保留正确的缩进。例如,def uu init uu self:的数字空格应该与def sqlcodeself相同:我已经做了调整,但现在出现了错误名称错误:名称“self”未定义您必须在类下保留正确的缩进。对于ex,def__init__self:的数字空间应与def sqlcodeself相同: