Chris G
Chris G

Reputation: 115

tkinter grid alignment: Python 2.7

I'm having a problem with the grid alignment when I add in a text box.

I've set up a simple GUI with a 3 column wide display. As part fo the GUI I have a text box for entering notes, and a spinbox for a defined number range.

If I remove the text box from the active code my spinbox aligns correctly with all other entries, but when I place the text box in the grid my spinbox is moved over to the right.

Doesn't seem to matter where the spinbox code is in relation to the text box, once the text box is present the spin box moves over to the right.

At this stage I just can't see what is causing the problem.

I'll admit I'm new to Tkinter (and stackoverflow).

This is the particular line of code that causes the problem:

self.newText.grid(row=display_row, column=0, columnspan=4, padx=10, pady=10).

Sample code below. If I comment out the above line the spin box aligns correctly, if its present then the spinbox is offset. Its only the spinbox that is affected, neither Label, Entry, RadioButton or Text widgets are affected, but Spinbox is. These are the only widgets needed for my GUI.

Any help appreciated.

#-------------------------------------------------------------------------------
# Name:        MyGUI
# Purpose:     Problem Sample
#
#-------------------------------------------------------------------------------


from Tkinter import *

#===============================
# GUI Class
#===============================

class GUIClass():
    '''
    GUI class
    '''
    def __init__(self):
        self.displayframe   = 0
        self.NameVar        = StringVar()
        self.NumVar         = StringVar()   # shouldn't be but it works
        self.ButtonVar      = BooleanVar()

    #===============================
    # Function for standard button
    def StandardEntry(self,  display_row,labeltext,entryvar):
        '''
        Standard 'Entry' feature for GUI.
        '''
        Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
        name = Entry(self.displayframe, textvariable=entryvar)
        name.grid(row=display_row, column=1, sticky=W)
        display_row+=1

    def BooleanRadioButton(self, display_row, labeltext, entryvar):
        '''
        Boolean true/false radio button function
        '''
        Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
        ButtonOn = Radiobutton(self.displayframe, text="Enabled", variable=entryvar, value=True)
        ButtonOn.grid(row=display_row, column=1, sticky=W)
        ButtonOn = Radiobutton(self.displayframe, text="Disabled", variable=entryvar, value=False)
        ButtonOn.grid(row=display_row, column=2, sticky=W)

    def StandardSpinBox(self, display_row, labeltext, min_value, max_value, variablename):
        '''
        Standard spinbox for this project.
        '''
        Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
        spinboxwidget = Spinbox(self.displayframe, from_=min_value, to=max_value, textvariable=variablename)
        spinboxwidget.grid(row=display_row, column=1)

    def AddFreeTextForm(self, display_row, notes):
        '''
        Standard widget for free text entry
        '''
        self.newText = Text(self.displayframe, width=50, height=8, takefocus=0)
        self.newText.grid(row=display_row, column=0, columnspan=4, padx=10, pady=10)

    def movesettingstoclass(self, dataclass):
        dataclass.NameVar       = self.NameVar.get()
        dataclass.NumVar        = int(self.NumVar.get())
        dataclass.ButtonVar     = self.ButtonVar.get()


    def populate(self, dataclass):
        '''
        Takes the data in the data structure and populates the GUI.
        '''
        self.NameVar.set(dataclass.NameVar)
        self.NumVar.set(str(dataclass.NumVar))
        self.ButtonVar.set(dataclass.ButtonVar)

#===============================
# Data Class
#===============================
class DataClass():

    def __init__(self):
        self.NameVar         = 'SomeName'
        self.NumVar          = 11
        self.ButtonVar       = False

    def showvalues(self):
        '''
        Debug function/method for checking values are set correctly.
        '''

        msg += 'NameVar    :     %s\n' %self.NameVar
        msg += 'NumVar     :     %d\n' %self.NumVar
        msg += 'KVVar      :     %d\n' %self.KVVar
        if self.ButtonVar == True:
            msg += 'ButtonVar:     True\n'
        else:
            msg += 'ButtonVar:     False\n'

        print msg


#=============================================
# Main window
#=============================================
root = Tk()

MyData = DataClass()
mainGUI = GUIClass()

root.title('Problem Example')
menu = Menu(root)
root.config(menu=menu)


#==============================================================================
# text fields to be entered by the user...

display_row = 0
Notes = ''

mainGUI.displayframe = Frame(root)

#==============================================================================
# Some entry input parameter

mainGUI.StandardEntry(display_row, "Some Label", mainGUI.NameVar)
display_row+=1

#==============================================================================
# Some spinbox input parameter

mainGUI.StandardSpinBox(display_row, "Some Label", 3, 21, mainGUI.NumVar)
display_row+=1

#==============================================================================
# Some Radiobutton input parameter

SwitchOn = mainGUI.BooleanRadioButton(display_row, "Button Label", mainGUI.ButtonVar)
display_row+=1

Label(mainGUI.displayframe, text="Configuration Notes").grid(row=display_row, column=1, sticky=W)
display_row += 1

#notes = mainGUI.AddFreeTextForm(display_row, "Configuration Notes", MyData.Notes)
notes = mainGUI.AddFreeTextForm(display_row, Notes)
display_row+=1

mainGUI.displayframe.pack(anchor='w')

#==============================================================================
# and the magic all happens here
mainloop()

Upvotes: 2

Views: 1288

Answers (1)

TidB
TidB

Reputation: 1759

You forgot to set the sticky parameter when calling the spinbox' grid method.

spinboxwidget.grid(row=display_row, column=1, sticky=W)

Upvotes: 3

Related Questions