Start New Discussion within our Software Development Community

I found that one can use dictionaries to hold Tkinter widget config and display parameters. This then permits one to have these dictionaries elsewhere in the code for easy modification instead of buried within the code for each widget.

I particularly like to place these dictionaries toward the top of my Tkinter code for easy access and modification. I no longer have to search through a long display of widget code, and then over to the pertinent parameter to modify what I need. I now just find the pertinent dictionary in the area of focus--congfig. or display--and modify it. "Easy peasy."

I included a working example.

So now you know; You can use dictionaries to set parameters in Tkinter widgets.

# Python 2.7.10
    # Imports #
from __future__ import print_function
from Tkinter import *

    # Main #
def main():
    tk_TkGUI = Tk()
    tk_TkGUI.title("Tk: Cooking with Dictionaries")

                # Tk Varaibles
    v_EmployeeNum = IntVar() ## Init the built-in IntVar().
    v_EmployeeNum.set("120350")
    v_Password = StringVar() ## Init th ebuilt-in StringVar()
    v_Password.set("mysecretpassword")
    v_Remember = BooleanVar()
    v_Remember.set(True)

                # Tk Config Dicts.
    tkl_ConfigLabel1 = {"text": "Employee Number:"}
    tkl_ConfigLabel2 = {"text": "Login Password:"}
    tke_ConfigEntry1 = {"width": 40, "textvariable": v_EmployeeNum}
    tke_ConfigEntry2 = {"width": 40, "show": "*", "textvariable": v_Password}
    tkb_ConfigButton1 = {"text": "Login", "command": lambda: f_ShowInfo(v_EmployeeNum, v_Password, v_Remember)}
    tkcb_ConfigCheckButton1 = {"text": "Remember Me", "variable": v_Remember}

                # Tk Grid Dicts.
    tke_GridEntry2 = {'column': 1, 'columnspan': 2, 'row': 1}
    tke_GridEntry1 = {'column': 1, 'columnspan': 2, 'row': 0}
    tkl_GridLabel1 = {'column': 0, 'columnspan': 1, 'row': 0}
    tkl_GridLabel2 = {'column': 0, 'columnspan': 1, 'row': 1, 'sticky': "w"}
    tkcb_GridCheckButton1 = {'column': 1, 'columnspan': 1, 'row': 2}
    tkb_GridButton1 = {'column': 2, 'columnspan': 1, 'row': 2}

                # Tk Widgets
    tkl_Label1 = Label(tk_TkGUI, tkl_ConfigLabel1).grid(tkl_GridLabel1)
    tke_Entry1 = Entry(tk_TkGUI, tke_ConfigEntry1).grid(tke_GridEntry1) ## Display an Entry widget.
    tkl_Label2 = Label(tk_TkGUI, tkl_ConfigLabel2).grid(tkl_GridLabel2) ## Display a Label.
    tke_Entry2 = Entry(tk_TkGUI, tke_ConfigEntry2).grid(tke_GridEntry2)
    tkcb_CheckButton1 = Checkbutton(tk_TkGUI, tkcb_ConfigCheckButton1).grid(tkcb_GridCheckButton1)
    tkb_Button1 = Button(tk_TkGUI, tkb_ConfigButton1).grid(tkb_GridButton1) ## Dispaly our button with a "lambda" call to a func with passed parameters.

    tk_TkGUI.mainloop()

    # Main Loop #
if __name__ == '__main__':
    main()