简体   繁体   English

Tkinter 在多个标签中更改编号

[英]Tkinter change number in multiple labels

I am making a program that lets a user input a flower type, and it will make a new row with row#, name, and days remaining before it dies.我正在制作一个程序,让用户输入花卉类型,它将创建一个新行,其中包含行号、名称和死亡前剩余的天数。 At the moment the UI is a bit messy and code could be improved a lot but that's not the point.目前用户界面有点混乱,代码可以改进很多,但这不是重点。 I would like to know how I would go about making multiple new labels that I can change the days remaining with the click of a button.我想知道我将如何制作多个新标签,我可以通过单击按钮更改剩余天数。

Here is my code so far:到目前为止,这是我的代码:

It runs ok but only the lastest made row can be changed, this is because every time one is made, the last one can't be edited anymore, and that's what I want to change.它运行正常,但只能更改最新制作的行,这是因为每次制作时,最后一行都无法再编辑,这就是我想要更改的内容。

from tkinter import *

#Flower Types

flowers_days = {
    "peony": 1,
    "rose": 2,
    "daffodil": 3,
    "dandelion": 4,
    "lavender": 5
}
day_change = {}

#Variables
day = 1
days_left = 5
row_Num = 0
name = ""

#Commands


def new_flower():
    #make a new line with the new flower
    global row_Num
    global days_left
    global name
    global new_row

    row_Num += 1

    name = str(clicked.get())
    print("Test:" + name)

    days_left = flowers_days[clicked.get()]

    day_change[days_left] = int(row_Num)

    new_row = Label(main_Frame, text=str(row_Num)+"    "+name+"    " + str(days_left))
    new_row.pack()

    return new_row


def next_day():
    global days_left
    global name
    days_left -= 1
    new_row.config(text=str(row_Num)+"    "+name+"    " + str(days_left))


root = Tk()

new_row = Label()

clicked = StringVar()
clicked.set("No option Selected")

#FLOWER TYPE
flower_Type_Frame = LabelFrame(root, text="New Flowers", padx=5, pady=5)
flower_Type_Frame.grid(row=0, rowspan=4, column=0, columnspan=2, padx=10, pady=10)
flower_Type_Label = Label(flower_Type_Frame, text="Flower Type:")
flower_Type_Label.grid(row=0, column=0, columnspan=2, padx=5, pady=5)

flower_Type_Drop = OptionMenu(flower_Type_Frame, clicked, """
No option Selected
""", "peony", "rose", "daffodil", "dandelion", "lavender")
flower_Type_Drop.grid(row=1, column=0, columnspan=2, pady=5, padx=5)

flower_Type_Submit = Button(flower_Type_Frame, text="Submit", padx=10, pady=10, command=new_flower)
flower_Type_Submit.grid(row=2, column=0, columnspan=2, rowspan=2)


#Empty slot
space_Frame = LabelFrame(root, text="Empty", padx=5, pady=5)
space_Frame.grid(row=0, rowspan=4, column=3, columnspan=2, padx=10, pady=10)
space_Fill = Label(space_Frame, text="Space          ").grid(row=0, column=0)


#Day Pass
day_Pass_Frame = LabelFrame(root, text="Day Pass", padx=5, pady=5)
day_Pass_Frame.grid(row=0, rowspan=2, column=6, columnspan=4, padx=10, pady=10)
day_Pass = Button(day_Pass_Frame, text="Next Day", padx=10, pady=10, command=next_day)
day_Pass.grid(row=0, rowspan=2, column=3, columnspan=2)


#Row Delete


#Main stuff
main_Frame = LabelFrame(root, text="Flowers In Stock", padx=5, pady=5)
main_Frame.grid(row=5, column=0, columnspan=7, padx=10, pady=10)
header = Label(main_Frame, text="   Row #   /   Flower Type   /   Days Remaining   ", padx=5, pady=5)
header.pack(padx=5, pady=5)


root.mainloop()

Once this is sorted I also plan on making it to have a remove row button, so the row numbers need to be able to be changed too if possible.排序完成后,我还计划让它有一个删除行按钮,因此如果可能的话,行号也需要能够更改。

Thanks for any help.谢谢你的帮助。

You're keeping only one 'days_left' information (in a global variable), but you need to keep one for each flower.您只保留一个“days_left”信息(在全局变量中),但您需要为每朵花保留一个。 So your main data structure needs to be a list of flowers, and you should remove the 'global' statements for days_left, name, new_row, as that information needs to be secific to each flower.因此,您的主要数据结构需要是花列表,并且您应该删除 days_left、name、new_row 的“全局”语句,因为这些信息需要对每朵花都是特定的。

Add this to the global scope (just before the new_flower() definition):将此添加到全局 scope (就在 new_flower() 定义之前):

# List of [name, days_left, label], one per flower
flowers = []

In the new_flower() function, add the newly-created flower to the list with 'append':在 new_flower() function 中,使用 'append' 将新创建的花添加到列表中:

new_row = Label(main_Frame, text=str(row_Num)+"    "+name+"    " + str(days_left))
new_row.pack()
flowers.append([name, days_left, new_row])

The next_day function should look like this: next_day function 应该如下所示:

def next_day():
    for i, f in enumerate(flowers):
        # f is a 3-element list [name, days_left, label]
        f[1] -= 1
        name, days_left, label = f
        label.config(text=str(i+1)+"    "+name+"    " + str(days_left))

The 'enumerate' call iterates over a list, returning both the current index in the list (in 'i') and the current list item (in 'f'). 'enumerate' 调用遍历列表,返回列表中的当前索引(在 'i' 中)和当前列表项(在 'f' 中)。 The index gives you the row number.索引为您提供行号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM