向tkinter添加grid.lines不起作用

时间:2018-12-16 16:29:19

标签: python python-3.x tkinter

我写了一个脚本,显示了某些食品被打开多少天。它通过tkinter在LCD上显示。但是,当我添加新项目时,不会添加第7行。一项被覆盖。 我的错误在哪里?

#!/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 个答案:

答案 0 :(得分:0)

我想我有一个答案: 您的代码是:

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)

但是在def show()中,for循环不执行任何操作,因为未在其中定义food_count,因此for lop不执行任何操作, 看看是否可行

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)

答案 1 :(得分:0)

此代码中的问题是以下行:

food_count = len(food.items())

此行在本地范围内创建一个新变量,但不会在全局范围内更改该变量(请参见local vs global scope)。

要解决此问题,您需要将add函数更改为:

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

这将确保food_count在全局范围内。