Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tkinter widget doesn't appear

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading