从文本文件导入项目并在Treeview中显示它

时间:2018-02-10 03:16:14

标签: python python-3.x tkinter treeview

我正在为我的朋友和我正在探索的节目做这件事。目前,我想使用Tkinter在树视图中显示预订列表,这是我使用Python 3.6的编码

    import tkinter as Tkinter
    import tkinter.font as tkFont
    import tkinter.ttk as ttk
    import datetime


    d = 0
    t = 1
    u = 2
    dn = 3
    a = 4
    co = 5


    now = datetime.datetime.now()

    readFile = open('user_booking.txt', 'r')
    allbooking_array = readFile.read().split(',')

    while d <= len(allbooking_array):
        if allbooking_array[d] >= now.strftime("%d-%m"):
            date = allbooking_array[d]
            time = allbooking_array[t]
            user = allbooking_array[u]
            doctor = allbooking_array[dn]
            age = allbooking_array[a]
            consultationoption = allbooking_array[co]

            temporary_data.append(date,time,user,doctor,age,consultationoption)

            d = d + 6
            t = t + 6
            u = u + 6
            dn = dn + 6
            a = a + 6
            co = co + 6

        else:
            d = d + 6
            t = t + 6
            u = u + 6
            dn = dn + 6
            a = a + 6
            co = co + 6

    tree_columns = ("Date", "Time", "Patient Name", "Doctor Name")
    tree_data = (temporary_data)

    def sortby(tree, col, descending):
        """Sort tree contents when a column is clicked on."""
        # grab values to sort
        data = [(tree.set(child, col), child) for child in tree.get_children('')]

        # reorder data
        data.sort(reverse=descending)
        for indx, item in enumerate(data):
            tree.move(item[1], '', indx)

        # switch the heading so that it will sort in the opposite direction
        tree.heading(col,
            command=lambda col=col: sortby(tree, col, int(not descending)))

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

        def _setup_widgets(self):
            msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
                padding=(10, 2, 10, 6),
                text=("Your itineraray with other doctors are shown below:"), )
            msg.pack(fill='x')

            container = ttk.Frame()
            container.pack(fill='both', expand=True)

            .
            self.tree = ttk.Treeview(columns=tree_columns, 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 tree_columns:
                self.tree.heading(col, text=col.title(),
                    command=lambda c=col: sortby(self.tree, c, 0))


                self.tree.column(col, width=tkFont.Font().measure(col.title()))

            for item in tree_data:
                self.tree.insert('', 'end', values=item)


                for indx, val in enumerate(item):
                    ilen = tkFont.Font().measure(val)
                    if self.tree.column(tree_columns[indx], width=None) < ilen:
                        self.tree.column(tree_columns[indx], width=ilen)

    def main():
        root = Tkinter.Tk()
        root.wm_title("Multi-Column List")
        root.wm_iconname("mclist")



        app = App()
        root.mainloop()

    if __name__ == "__main__":
        main()

然后我的user_booking.txt有这些保存文件

07-12,1800,2,Ivan,Sia,Normal Consultation,07-12,1500,3,Ivan,Sia,Normal Consultation,06-12,1300,6,Ivan,Sia,Normal Consultation,07-12,1800,2,Ivan,Sia,Normal Consultation,07-02,1800,2,Ivan,Sia,Normal Consultation,

但后来我试图跑,但它说:

    tree_data = (temporary_data)
NameError: name 'temporary_data' is not defined

请帮帮我!我该怎么办?

1 个答案:

答案 0 :(得分:0)

假设错误是唯一的问题,那么您正在尝试append处理错误。方法首先要求它们的对象存在,而appendlist对象的方法。首先创建一个空的list对象:

temporary_data = list() # or []
while d <= len(allbooking_array):
    ...

这应该删除即时错误。