tkinter widget doesn't appear

Question:

I’m trying to make a very simple tkinter app, but I can’t seem to figure out why one of my widgets isn’t appearing. The below code should render the string ’30’ in a row above the button-label pairs, but it only shows the button-label pairs and no additional string.

from tkinter import *
from tkinter import ttk

data = {
    'projects': {
      'project': {
           'name': 'Project',
           'starttime': 0
    },
       'another': {
           'name': 'Another',
           'starttime': 0
       }
    },
    'clock': {
        'clockedin?': False,
        'time': 30
    }
}

def makebutton():
    row = 1
    for item in data['projects']:
        ttk.Button(frm, text=data['projects'][item]['name']).grid(column=0,row=row)
        ttk.Label(frm, text=data['projects'][item]['starttime']).grid(column=1, row=row)
        row = row + 1

root = Tk()

frm = ttk.Frame(root, padding=10)
frm.grid()

ttk.Label(frm, text=data['clock']['time']).grid(column=0, row=2) # should say '30'

makebutton()

root.mainloop()

If I move the ’30’ label line below makebutton(), it displays ’30’, but over the bottom button.

What am I misunderstanding here?

Asked By: Sjiveru

||

Answers:

You are putting the label on row 2, and then in makebutton you’re putting widgets in rows 1 and 2. Since makebutton is called after you create the label, the second button it creates sits on top of the label.

You should either move the label to row 3, or initialize row to 0 at the top of makebutton.

Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.