GUI layout using Tk Grid Geometry Manager

Building a small application for personal use with Python and thought I'd try my hand with a little GUI programming using Tkinter. This is the GUI I've created so far:

GUI layout using Tkinter

Application doubts:

How can I make sure that the three LableFrames - A, B and C in the screenshot - have the same width? (Or rather, have the width equal to the widest of the three? For example, in the screenshot, A is the widest and I'd like B and C to also be as wide - up to the line D).

(It doesn't have to dynamically calculated - it is enough if I can ensure that the width are the same when I code it, the first time round. They don't need to change on runtime.)

Tk Grid Geometry Manager doubts:

  1. When you use frames, is the grid (row, column) specific to only the size of the frame or is it calculated based on the size of the form (root window)?

  2. How is the size of a column determined in a grid?

  3. I haven't fully understood what 'weight' does within a grid. When should it be used?

The Python GUI Code:

import Tkinter

if __name__ == '__main__':
    form = Tkinter.Tk()

    getFld = Tkinter.IntVar()

    form.wm_title('File Parser')

    stepOne = Tkinter.LabelFrame(form, text=" 1. Enter File Details: ")
    stepOne.grid(row=0, columnspan=7, sticky='W', \
                 padx=5, pady=5, ipadx=5, ipady=5)

    helpLf = Tkinter.LabelFrame(form, text=" Quick Help ")
    helpLf.grid(row=0, column=9, columnspan=2, rowspan=8, \
                sticky='NS', padx=5, pady=5)
    helpLbl = Tkinter.Label(helpLf, text="Help will come - ask for it.")
    helpLbl.grid(row=0)

    stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ")
    stepTwo.grid(row=2, columnspan=7, sticky='W', \
                 padx=5, pady=5, ipadx=5, ipady=5)

    stepThree = Tkinter.LabelFrame(form, text=" 3. Configure: ")
    stepThree.grid(row=3, columnspan=7, sticky='W', \
                   padx=5, pady=5, ipadx=5, ipady=5)

    inFileLbl = Tkinter.Label(stepOne, text="Select the File:")
    inFileLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)

    inFileTxt = Tkinter.Entry(stepOne)
    inFileTxt.grid(row=0, column=1, columnspan=7, sticky="WE", pady=3)

    inFileBtn = Tkinter.Button(stepOne, text="Browse ...")
    inFileBtn.grid(row=0, column=8, sticky='W', padx=5, pady=2)

    outFileLbl = Tkinter.Label(stepOne, text="Save File to:")
    outFileLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)

    outFileTxt = Tkinter.Entry(stepOne)
    outFileTxt.grid(row=1, column=1, columnspan=7, sticky="WE", pady=2)

    outFileBtn = Tkinter.Button(stepOne, text="Browse ...")
    outFileBtn.grid(row=1, column=8, sticky='W', padx=5, pady=2)

    inEncLbl = Tkinter.Label(stepOne, text="Input File Encoding:")
    inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)

    inEncTxt = Tkinter.Entry(stepOne)
    inEncTxt.grid(row=2, column=1, sticky='E', pady=2)

    outEncLbl = Tkinter.Label(stepOne, text="Output File Encoding:")
    outEncLbl.grid(row=2, column=5, padx=5, pady=2)

    outEncTxt = Tkinter.Entry(stepOne)
    outEncTxt.grid(row=2, column=7, pady=2)

    outTblLbl = Tkinter.Label(stepTwo, \
          text="Enter the name of the table to be used in the statements:")
    outTblLbl.grid(row=3, column=0, sticky='W', padx=5, pady=2)

    outTblTxt = Tkinter.Entry(stepTwo)
    outTblTxt.grid(row=3, column=1, columnspan=3, pady=2, sticky='WE')

    fldLbl = Tkinter.Label(stepTwo, \
                           text="Enter the field (column) names of the table:")
    fldLbl.grid(row=4, column=0, padx=5, pady=2, sticky='W')

    getFldChk = Tkinter.Checkbutton(stepTwo, \
                           text="Get fields automatically from input file",\
                           onvalue=1, offvalue=0)
    getFldChk.grid(row=4, column=1, columnspan=3, pady=2, sticky='WE')

    fldRowTxt = Tkinter.Entry(stepTwo)
    fldRowTxt.grid(row=5, columnspan=5, padx=5, pady=2, sticky='WE')

    transChk = Tkinter.Checkbutton(stepThree, \
               text="Enable Transaction", onvalue=1, offvalue=0)
    transChk.grid(row=6, sticky='W', padx=5, pady=2)

    transRwLbl = Tkinter.Label(stepThree, \
                 text=" => Specify number of rows per transaction:")
    transRwLbl.grid(row=6, column=2, columnspan=2, \
                    sticky='W', padx=5, pady=2)

    transRwTxt = Tkinter.Entry(stepThree)
    transRwTxt.grid(row=6, column=4, sticky='WE')

    form.mainloop()

(Note: For Python 2.4.3)


Asked by: Maddie725 | Posted: 06-12-2021






Answer 1

If you use the same columnspan and use sticky='WE' on all three LabelFrames then they should have the same width. For example, you want to use

stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ")
stepTwo.grid(row=2, columnspan=7, sticky='WE', \
             padx=5, pady=5, ipadx=5, ipady=5)

Questions

1) When you use frames, is the grid (row, column) specific to only the size of the frame or is it calculated based on the size of the form (root window)?

There is an interdependence here. The preferred size of the form will depend on the preferred sizes of the children and the layout, but the actual sizes of the children will depend on the actual size of the form and the layout. Layout is done from the children to the root to determine the preferred sizes, and then when it gets to the root, the preferred size is used as the actual size (unless overridden). Layout then goes back down assigning actual sizes.

2) How is the size of a column determined in a grid?

The preferred size of a column is determined based to be the minimum preferred width of all the items in that row. The actual size of the column is determined by the preferred size plus some percentage of the extra space of the parent widget.

3) I haven't fully understood what 'weight' does within a grid. When should it be used?

The weight determines the percentage of extra space that I mentioned above. The amount of the extra space given to the column is column_weight/total_weight.

Answered by: Wilson145 | Posted: 07-01-2022



Similar questions

python - Cannot use geometry manager pack inside

So I'm making an rss reader using the tkinter library, and in one of my methods I create a text widget. It displays fine until I try to add scrollbars to it. Here is my code before the scrollbars: def create_text(self, root): self.textbox = Text(root, height = 10, width = 79, wrap = 'word') self.textbox.grid(column = 0, row = 0) Here is my code after: ...


python - Geometry manager

Closed. This question needs details or clarity. It ...


python - Cannot use geometry manager pack inside

So I'm making an rss reader using the tkinter library, and in one of my methods I create a text widget. It displays fine until I try to add scrollbars to it. Here is my code before the scrollbars: def create_text(self, root): self.textbox = Text(root, height = 10, width = 79, wrap = 'word') self.textbox.grid(column = 0, row = 0) Here is my code after: ...


python - cannot use geometry manager pack inside error tkinter

ı am new tkinter. I want to add a image to my programme. But ı am taking a error:cannot use geometry manager pack inside . which already has slaves managed by grid. If ı run this code individual it is working. I can obtain image and programme is working. But if ı run together image and function it is not working. How can ı fix it? from tkinter import * from PIL import ImageTk,Image import tkinter as tk fro...


python - Not using grid anywhere still gives error 'cannot use geometry manager pack inside .frame2 which already has slaves managed by grid' in tkinter

I am creating this app which works completely and I want to display the output (a pandas dataframe in table and a few matplotlib graphs) with a little GUI with tkinter. Since tkinter doesn't allow grid and pack to work together, I replaced all widgets geometry manager from grid to pack. It worked fine when I ran the program for first time but since then it has been giving me following error: TclError: cann...


python - cannot use geometry manager pack inside . which already has slaves managed by grid

I want to use grid to show pdf but it's showing error that cannot use geometry manager pack inside . which already has slaves managed by grid. from tkinter import * from tkPDFViewer import tkPDFViewer as pdf mys = Tk() mys.title("Mystery Books") mys.config(bg='white') I changed to grid() from pack() inside dead function but still there's something I can't find out to change to gri...


python - Geometry manager

Closed. This question needs details or clarity. It ...


python - How do I overlap widgets with the Tkinter pack geometry manager?

I want to put a Canvas with an image in my window, and then I want to pack widgets on top of it, so the Canvas acts as a background. Is it possible to have two states for the pack manager: one for one set of widgets and another for another set?


Good geometry library in python?

Closed. This question does not meet Stack Overflow guid...


python - Postgis - How do i check the geometry type before i do an insert

i have a postgres database with millions of rows in it it has a column called geom which contains the boundary of a property. using a python script i am extracting the information from this table and re-inserting it into a new table. when i insert in the new table the script bugs out with the following: Traceback (most recent call last): File "build_parcels.py", line 258, in <module>...


python - How do I use Django to insert a Geometry Field into the database?

class LocationLog(models.Model): user = models.ForeignKey(User) utm = models.GeometryField(spatial_index=True) This is my database model. I would like to insert a row. I want to insert a circle at point -55, 333. With a radius of 10. How can I put this circle into the geometry field? Of course, then I would want to check which circles overlap a given circle. (my select stat...


Help with Windows Geometry in Python

Why are the commands to change the window position before and after sleep(3.00) being ignored? if self.selectedM.get() == 'Bump': W1 = GetSystemMetrics(1) + 200 print W1 w1.wm_geometry("+100+" + str(W1)) w2.wm_geometry("+100+" + str(W1)) w3.wm_geometry("+100+" + str(W1)) w4.wm_geometry("+100+" + str(W1)) self.rvar.set(0) self.rvar2.set(0) ...


python - Parsing MSDN Geometry Data Type

I have a database where one field gives spatial coordinates. I have learned the field is a serialised MSDN geometry Data Type (http://msdn.microsoft.com/en-us/library/bb933973.aspx). I want to access this database from Python and was wandering if anyone knew the format of the Geometry Data Type, or any libraries capable of parsing ...


geometry - Integral of a triangle function in python

I have just defined a function which is the integral of a given step function, and now I would like to integrate the triangle function obtained. jerk:1,-1,1-1 The step function correspond of a given list of successive jerk. For getting the triangle function, I have used the formula: a(t)=kt+ac with k the Jerk and ac a con...


python - Is there a GUI design app for the Tkinter / grid geometry?

Closed. This question does not meet Stack Overflow guid...


geometry - Python - Pygame Drawing Angle

I need to draw an angle(in Pygame), given it's 1. Measure of the angle (θ) 2. Endpoints of the base(A & B) Here I know 1. Measure of θ (in radians and degrees) 2. (x,y) of A and B 3. Measure of BC My Question How do I calculate the position of the Co-ordinates(...


python - Convert Point Geometry to list

I have the following script that creates a point geometry. How can I convert this point geometry to a list only containing the coordiantes to look like [258432.79138201929, 1001957.4394514663]? >>> import ogr >>> driver = ogr.GetDriverByName('ESRI Shapefile') >>> pointshp = driver.Open('U:/My Documents/Tool/shp/point.shp', 0) >>> pointlyr = pointshp.GetLayer...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top