簡體   English   中英

將grid.lines添加到tkinter不起作用

[英]Adding grid.lines to tkinter doesn't work

我寫了一個腳本,顯示了某些食品被打開的天數。 它通過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()

我想我有一個答案:你的代碼是:

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)

這段代碼中的問題是:

food_count = len(food.items())

此行在本地范圍內創建新變量,但不更改全局范圍中的變量(請參閱本地范圍與全局范圍 )。

要解決此問題,您需要將add函數更改為:

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

這將確保food_count在全球范圍內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM