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 include an image; the dialog box is not allowing me to browse and upload the saved image).
(Widget, start_row, start_col, row_span, column_span):
Yet, when I run my code, the buttons and Frame3 are rendered fine, but Frame1 vertically "squashes" Frame2. Any suggestions would be greatly appreciated. (I have read the suggested StackOverflow answers and none seem to provide information that can be used to solve my problem. Also, I have searched extensively online to no avail.)
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title("Grid Manager")
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
for i in range(5):
self.master.button = Button(master, text = "Button {0}".format(i))
self.master.button.grid(row=6, column=i, sticky=W+E)
self.Frame1 = Frame(master, bg="red")
self.Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
self.Frame2 = Frame(master, bg="blue")
self.Frame2.grid(row = 2, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
self.Frame3 = Frame(master, bg="green")
self.Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)
root = Tk()
app = Application(master=root)
app.mainloop()
UPDATE: Now that I'm at my personal computer, here are images for the result that I want and the result that I get, respectively:
Tkinter can support the creation of more than one widget in the same frame. Not just this it also supports a mechanism to align them relative to each other. One of the easiest ways of aligning the different widgets in the Tkinter is through grid manager.
This method can be used on both windows and frames. The grid() method allows you to indicate the row and column positioning in its parameter list. Both row and column start from index 0. For example grid(row=1, column=2) specifies a position on the third column and second row of your frame or window.
padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders. row − The row to put widget in; default the first row that is still empty. rowspan − How many rowswidget occupies; default 1. sticky − What to do if the cell is larger than widget.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With