Tkinter的布局问题

时间:2017-04-11 12:46:15

标签: python layout tkinter

我对tkinter中的布局有疑问。这是 Layout我想拥有,这就是I have right now. 我不明白为什么会这样。我想当我指定特定帧的宽度和高度时,它会占用所有这些位置,但这不会发生self.GameStatFrame。如果有人能够向我解释为什么代码会做它的作用以及我的错误是什么,那将是很好的。

以下是代码:

class Application(tk.Frame):
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        #self.update_idletasks()
        self.createWidgets()

    def createWidgets(self):

        #new frame for everything not in the canvas
        #self.Frame = tk.Frame(self.parent)
         self.MlGameStatFrame = tk.Frame(self.parent, width=600, height=300, 
         bg='blue')
         self.GameStatFrame = tk.Frame(self.MlGameStatFrame, bg='white', 
         width=300, height=300)
         self.MlFrame = tk.Frame(self.MlGameStatFrame,  bg='white', 
         width=300, height=300)


        #self.Frame.pack()
        self.createGraphWidget()
        self.MlGameStatFrame.pack()
        self.GameStatFrame.pack(side=tk.LEFT, anchor=tk.W, fill=tk.BOTH)
        self.MlFrame.pack(side=tk.BOTTOM, fill=tk.BOTH)

        self.createGameStats(self.GameStatFrame)
        self.createMlStats(self.MlFrame)
        #self.createLog()


    def createGraphWidget(self):
        self.graph = tk.Canvas(self.parent, background ='white',             
        width=200,height=300)
        self.graph.create_rectangle(0,20,40,50)

        self.graph.pack(side=tk.TOP,fill=tk.X)
# needs to get a frame because side by side with Ml stats
    def createGameStats(self,GameFrame):

        Frame1 = tk.Frame(GameFrame, bg='red', width=300)
        tk.Label(Frame1, text="Status: ").pack(side=tk.LEFT, expand=tk.YES) 
        #initialize with certain value for now
        self.statusChange = tk.Label(Frame1, 
        text="Learning").pack(side=tk.LEFT, expand=tk.YES)

        Frame2=tk.Frame(GameFrame, bg='green')

        tk.Label(Frame2, text="Fitness").pack(side=tk.LEFT, expand=tk.YES, 
        anchor=tk.W)    #could get changed
        self.fitnessChange = tk.Label(Frame2, text="6").pack(side=tk.LEFT, 
        expand=tk.YES)

        Frame1.pack(side=tk.TOP, anchor=tk.W)
        Frame2.pack(side=tk.TOP,fill=tk.X, anchor=tk.W)


    def createMlStats(self, MlFrame):
        Frame1 = tk.Frame(MlFrame)
        tk.Label(Frame1, text="Status: ").pack(side=tk.LEFT, expand=tk.YES)  
    # initialize with certain value for now
        self.statusChange = tk.Label(Frame1,     
        text="Learning").pack(side=tk.LEFT, fill=tk.X, expand=tk.YES)

        Frame2=tk.Frame(MlFrame)
        tk.Label(Frame2, text="Fitness").pack(expand=tk.YES, anchor=tk.W) 
        self.fitnessChange = tk.Label(Frame2, text="6").pack(side=tk.LEFT, 
        fill=tk.X, expand=tk.YES)

        Frame1.pack(side=tk.TOP, fill=tk.BOTH,anchor=tk.W)
        Frame2.pack(side=tk.TOP,fill=tk.BOTH)

def main():
    root = tk.Tk()

    root.geometry('600x900-0+0') #120* 50 ppixels in top right corner of desktop
    app = Application(root)

    app.master.title('Sample application')

    app.mainloop()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

.grid布局管理器更加灵活:

class Application(Frame):
    def __init__(self, parent):
       self.parent = parent
       Frame.__init__(self, parent)
       self.create_widgets()

    def create_widgets(self):
        graph = Canvas(self.parent, width=200, height=300, bg="white")
        graph.create_rectangle(0, 20, 40, 50)

        mlgamestat_frame = Frame(self.parent, width=600, height=300, bg="blue")
        gamestat_frame = Frame(mlgamestat_frame, width=300, height=300, bg="yellow")
        ml_frame = Frame(mlgamestat_frame, width=300, height=300, bg="green")

        graph.grid(row=0, column=0)
        mlgamestat_frame.grid(row=1, column=0)
        gamestat_frame.grid(row=0, column=0)
        ml_frame.grid(row=0, column=1)

您可以将小部件放置在表格中。上面的代码可以实现您的预​​期布局。