Skip to content Skip to sidebar Skip to footer

Tkinter Grid() Alignment Issue In Python

I have been working in python for the first time and basically I am trying to get these to labels to align in the correct columns and the correct row but for some reason it doesn't

Solution 1:

If a row or column does not contain anything, it is then of size 1 pixel, which is very small. What you see in the output is actually the two text 20 rows apart. Add in widgets, and you will see your result.

You can also use grid_rowconfigure, grid_columnconfigure and sticky properties in order to specify how a widget in a grid stretches. That way you can place your widgets in the right screen locations.

Have a look at my answer here for more details on how to use grid properties: Tkinter. subframe of root does not show

To understand your grid better, I added another widget to you code:

from tkinter import *
import sys
from tkinter.scrolledtext import ScrolledText

#Setup GUI#Create main window
main = Tk()
#Create title of main window
main.title = "Waterizer 1.0"#Create default size of main window
main.geometry("1024x768")
#Lets get a fram to stick the data in we plan on using
mainApp = Frame(main)
#Snap to grid
mainApp.grid(row=0, column=0, sticky='nsew')
#main grid stretching properties
main.grid_columnconfigure(0, weight=1)
main.grid_rowconfigure(0, weight=1)
#Font for main labels
labelFont = ('times', 20, 'bold')
#Label for the Power
powerLabel = Label(mainApp, text="Power  Status")
powerLabel.config(fg="Red", bd="1")
powerLabel.config(font=labelFont)
powerLabel.grid( row=0,column=20, sticky = W)
#Label for Water
waterLabel = Label(mainApp, text="Water Status")
waterLabel.config(fg="Blue", bd="1")
waterLabel.config(font=labelFont)
waterLabel.grid(row=20, column=20, sticky = W)
#ScrollText to fill in between space
ScrollText = ScrolledText(mainApp)
ScrollText.grid(row=1, column=20,sticky = 'nsew')
#mainApp grid stretching properties
mainApp.grid_rowconfigure(1, weight=1)
mainApp.grid_columnconfigure(20, weight=1)

main.mainloop()

Try this.

PS: You do not need ; after every line python

Solution 2:

Empty rows have a height of zero, and empty columns have a width of zero. Grid is behaving as designed with your code, since there are no widgets in rows 1-19, and no widgets in columns 0-19. Put something in the other rows and columns and your labels will move.

Post a Comment for "Tkinter Grid() Alignment Issue In Python"