Python - Multiple frames with Grid manager

后端 未结 3 736
借酒劲吻你
借酒劲吻你 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:14

    If you like try pytkgen (https://github.com/tmetsch/pytkgen or http://pypi.python.org/pypi/pytkgen/) - which generates Tkinter GUIs from JSON files and takes care of the weight, height and row/columnconfigure. See the examples here: https://github.com/tmetsch/pytkgen/tree/master/examples

    0 讨论(0)
  • 2020-12-14 21:18

    Since frame 1, 2 and 3 don't have any widgets inside them and you haven't given them any height, their natural size will be one pixel tall. If you put something in frame2, or give frame2 a height, it will show up.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题