Adding grid.lines to tkinter doesn't work

Viewed 45

I wrote a script, that shows how many days certain food items are opened. It is displayed on an lcd via tkinter. However, when i add a new item, no 7th row is added. One item is overwritten. Where could my mistake be?

#!/usr/bin/python3
import datetime
from tkinter import *


food = {'potatoes': datetime.date(2018, 12, 8),'sausage': datetime.date(2018, 12, 12), 'cream': datetime.date(2018, 12, 13), 'cauliflower': datetime.date(2018, 12, 11), 'ham': datetime.date(2018, 12, 10)}
food_count = len(food.items())



def add():
    food["tomato"] = datetime.date.today()
    food_count = len(food.items())
    show()


def show():    
    for i in range(0, int(food_count)):
        name, date = list(food.items())[i]
        days_open = (datetime.date.today() - date).days
        label_name = Label(master=window, width=width, height=height,
                                          font=("Arial",10),
                                           text = name)
        label_days= Label(master=window, width=width, height=height,
                                          font=("Arial",10),
                                           text = days_open)
        label_name.grid(row=i+1,column=1)
        label_days.grid(row=i+1,column=2)

width = 20
height = 2

window = Tk()
window.geometry('480x320')

button_add = Button(master=window, width=width, height=height,
                text="Add",
                command=add,
                font=("Arial",10))

label_01 = Label(master=window, width=width, height=height,
              font=("Arial",10),
              text = "Lebensmittel")

label_02 = Label(master=window, width=width, height=height,
              font=("Arial",10),
              text = "geöffnet seit")

button_add.grid(row=0,column=0)
label_01.grid(row=0,column=1)
label_02.grid(row=0,column=2)

show()
window.mainloop()
2 Answers

I think i have an answer: your code is:

def add():
    food["tomato"] = datetime.date.today()
    food_count = len(food.items())
    show()


def show():

for i in range(0, int(food_count)):
    name, date = list(food.items())[i]
    days_open = (datetime.date.today() - date).days
    label_name = Label(master=window, width=width, height=height,
                                      font=("Arial",10),
                                       text = name)


    label_days= Label(master=window, width=width, height=height,
                                      font=("Arial",10),
                                       text = days_open)


    label_name.grid(row=i+1,column=1)
    label_days.grid(row=i+1,column=2)

but in def show() the for loop does not do anything because food_count is not defined in it, so the for lop does not do anything, see if this works

def add():
    food["tomato"] = datetime.date.today()
    #food_cound moved to def show
    show()


def show():
    food_count = len(food.items())
    for i in range(0, int(food_count)):
        name, date = list(food.items())[i]
        days_open = (datetime.date.today() - date).days
        label_name = Label(master=window, width=width, height=height,
                                          font=("Arial",10),
                                           text = name)


        label_days= Label(master=window, width=width, height=height,
                                          font=("Arial",10),
                                           text = days_open)


        label_name.grid(row=i+1,column=1)
        label_days.grid(row=i+1,column=2)

The problem in this code is the line:

food_count = len(food.items())

This line creates a new variable in the local scope, but does not change the variable in the global scope (see local vs global scope).

To fix this, you need to change the add function to:

def add():
    global food_count
    food["tomato"] = datetime.date.today()
    food_count = len(food.items())
    show()

This will make sure that food_count is in the global scope.

Related