按钮和标签将不会显示在窗口中

时间:2019-07-11 17:59:14

标签: python tkinter

按钮和标签不会显示在我的窗口中。如果我从括号中删除self(或改为插入控制器),则按钮将显示。但是,这些按钮不会将底层窗口提升到预期的位置。我使用了类似的使用pack的代码,并且效果很好。我似乎找不到为什么不能使用网格的问题。我是tkinter的初学者,所以也许我缺少明显的东西。

import tkinter as tk


LARGE_FONT= ("Verdana", 12)
NORM_FONT= ("Verdana", 10)
SMALL_FONT= ("Verdana", 8)


class mGui(tk.Tk):  

    def __init__(self, *args, **kargs): 

       tk.Tk.__init__(self, *args, **kargs) 
       container = tk.Frame(self)
       container.grid_rowconfigure(0, weight=1)
       container.grid_columnconfigure(0, weight=1)

       self.frames = {}  

       for F in (StartPage, PageOne):

            frame = F(container, self)

            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")



       self.show_frame(StartPage)   

    def show_frame(self, cont):  
        frame = self.frames[cont]
        frame.tkraise()   


class StartPage(tk.Frame):  

    def __init__(self, parent, controller):  
        tk.Frame.__init__(self, parent) 
        label_1 = tk.Label(self, text = "Start Page", font = LARGE_FONT)
        label_1.grid(row = 0, column = 0) 


        button1 = tk.Button(self,text = "Go", command = lambda: controller.show_frame(PageOne))
        button1.grid(row = 1, column = 0)     

class PageOne(tk.Frame):  

    def __init__(self, parent, controller): 
        tk.Frame.__init__(self, parent) 
        label_1 = tk.Label(self, text = "Page One", font = LARGE_FONT)
        label_1.grid(row = 1, column = 4)  

        button1 = tk.Button(self, text = "Go Back", command = lambda: controller.show_frame(StartPage))
        button1.grid(row = 4, column = 1)              


app = mGui()
app.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要向grid() container的{​​{1}}布局管理器添加调用,如下所示:

Frame