Tkinter Python3插入表包

时间:2017-01-12 16:49:41

标签: python-3.x tkinter

我发现这个例子在TKinter上有多个页面,我试图使用它但是我遇到了问题,在第一页我想要一个表,它返回给我一些我从DB读取的值,问题是我发现的代码使用pack(),所以我无法获得列和行....

是否有人知道如何介绍一张桌子?我尝试搜索,但我找不到太多。

TITLE_FONT = ("Helvetica", 18, "bold")
text_font = ("Helvetica", 14)

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(10, weight=1)
        container.grid_columnconfigure(10, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Home", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Users",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Courses",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack(side="left")
        button2.pack(side="left")
        #########################
        #I want Introduce my table here with 9 columns and dinamyc lines
        #########################

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="User Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Courses Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

我正在尝试类似的东西,但问题是每次在网格命令上给我错误:

for j in range(10):
    for i in range(10):
        e = tkinter.Entry(f)
        e.grid(column=i,row=j, borderwidth=0)
        es[i,j] = e

1 个答案:

答案 0 :(得分:1)

在页面上,您可以使用Frame()放置pack(),然后在此框架内,您可以使用grid()放置表格。