Python - Multiple frames with Grid manager

后端 未结 3 742
借酒劲吻你
借酒劲吻你 2020-12-14 20:49

I\'m trying to use the functionality from the Tkinter module (Python 2.7) to create a GUI that has eight widgets placed on a 7 row by 5 column grid (sorry that I did not in

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 21:29

    After messing around with my code for a few hours, I was finally able to create the GUI that I intended to. The key was looping over rows and columns and setting their weights using rowconfigure and columnconfigure, respectively. Code is below:

    from tkinter import *
    
    class Application(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.grid()
            self.master.title("Grid Manager")
    
            for r in range(6):
                self.master.rowconfigure(r, weight=1)    
            for c in range(5):
                self.master.columnconfigure(c, weight=1)
                Button(master, text="Button {0}".format(c)).grid(row=6,column=c,sticky=E+W)
    
            Frame1 = Frame(master, bg="red")
            Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
            Frame2 = Frame(master, bg="blue")
            Frame2.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
            Frame3 = Frame(master, bg="green")
            Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)
    
    root = Tk()
    root.geometry("400x200+200+200")
    app = Application(master=root)
    app.mainloop()
    

    enter image description here

提交回复
热议问题