Choice of element on tkinter python



  • I write a simple GUI with standard Tkinter elements. There are a few apps on the main screen. the same containers frame with the same. elements like button, entry, label.

    To adhere to the golden rule DRY, the elements in the Freumas (and the Freimas themselves) create a cycle like:

    for frames in range(3):
        tab = Frame(mainWindow)
    
    for elements in range(1, 5):
    
        alarmH = Entry(tab, width=2)
        alarmH.grid(column=0, row=elements)
    
        sounds = Combobox(tab, values=soundList)
        sounds.set("Select")
        sounds.grid(column=4, row=elements)
    

    и т.д.

    The question now is how to turn to a separate element of one of the frimets (to allow a change in the text or background of the element). Is it possible?

    I suspect it's necessary to take everything into separate variables manually, but still. ♪ ♪



  • References to elements in lists or dictionaries can be retained.

    For example:

    frames = {}
    

    for i in range(3):

    frames[i] = {}
    tab = Frame(mainWindow)
    frames[i]['tab'] = tab
    frames[i]['elements'] = {}
    for j in range(1, 5):
        frames[i]['elements'][j] = {}
        alarmH = Entry(tab, width=2)
        alarmH.grid(column=0, row=elements)
        frames[i]['elements'][j]['entry'] = alarmH
        sounds = Combobox(tab, values=soundList)
        sounds.set("Select")
        sounds.grid(column=4, row=elements)
        frames[i]['elements'][j]['combobox'] = sounds
    

    And then you can use the dictionary to address the specific element:

    To a specific taboo:

    print(frames[0]['tab'])

    Or a specific element:

    frames[1]['elements'][0]['combobox'].set("Select")




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2