从文本文件导入项并在树视图中显示

2024-10-01 09:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我做这件事是为了我和朋友们正在探索的一个项目。目前,我想用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()

然后是我的用户_预订.txt保存这些文件

^{pr2}$

但后来我试着逃跑,但上面写着:

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

请帮帮我!我该怎么办?在


Tags: columnsinselftreefordatacontainerdef
1条回答
网友
1楼 · 发布于 2024-10-01 09:18:06

假设错误是唯一的问题,那么您将尝试append指向不是的对象。方法首先要求它们的对象存在,append是{}对象的方法。因此,首先创建一个空的list对象:

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

这样可以消除即时错误。在

相关问题 更多 >