更改条目值时更改标签值

时间:2016-10-04 13:19:54

标签: python-2.7 tkinter label

我目前的问题是我正在尝试将标签(标签16)更改为entry_values [0]的第一个值,该值不起作用我尝试将其作为变量和许多其他内容传递,经过大约一个小时的研究,我无法找到解决方案。我认为主要的问题是它在代码之前设置了标签,并且条目运行时不会改变。当我将它设置为textvariable时,它会产生一个空字符串(我认为),但是当我只使用文本时,它会放入一个0,我期待我的数字。

 def sub_menu(root):
    global subpage
    subpage = Frame(root)
    button5 = Button(subpage, text="Save Generation Data",
                 command = lambda: save_entries())
    button5.grid(row = 1, column = 6, sticky = E)

    button6 = Button(subpage, text="Return To Main Page",
                 command = lambda: switch_page("main"))

    button6.grid(row = 0, column = 6, sticky = W)

    juveniles_label0 = Label(subpage,text="Juveniles")
    adults_label1 = Label(subpage,text="Adults")
    seniles_label2 = Label(subpage,text="Seniles")
    population_label3 = Label(subpage,text="Population (Thousands)")
    survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)")
    birth_rate_label5 = Label(subpage,text="Birth Rate")
    number_of_gens_label6 = Label(subpage,text="Number of Generations")
    disease_trigger_label7 = Label(subpage,text="Disease Trigger Point")

    global entry0
    entry0 = Entry(subpage)
    global entry1
    entry1 = Entry(subpage)
    global entry2
    entry2 = Entry(subpage)
    global entry3
    entry3 = Entry(subpage)
    global entry4
    entry4 = Entry(subpage)
    global entry5
    entry5 = Entry(subpage)
    global entry6
    entry6 = Entry(subpage)
    global entry7
    entry7 = Entry(subpage)
    global entry8
    entry8 = Entry(subpage)


    juveniles_label0.grid(row = 0, column = 1) 
    adults_label1.grid(row = 0, column = 2)
    seniles_label2.grid(row = 0, column = 3)
    population_label3.grid(row = 1, column = 0)
    survival_rate_label4.grid(row = 2, column = 0)
    birth_rate_label5.grid(row = 3, column = 0)
    number_of_gens_label6.grid(row = 3, column = 2)
    disease_trigger_label7.grid(row = 4, column = 0)

    entry0.grid(row = 1, column = 1)
    entry1.grid(row = 1, column = 2)
    entry2.grid(row = 1, column = 3)
    entry3.grid(row = 2, column = 1)
    entry4.grid(row = 2, column = 2)
    entry5.grid(row = 2, column = 3)
    entry6.grid(row = 3, column = 1)
    entry7.grid(row = 3, column = 3)
    entry8.grid(row = 4, column = 1)



    return subpage

def save_entries(): #entry recieve point 
    save_page = Frame(root)

    """ if e0 < 0:
           make a check to check if value is < 0 dont accept and if a value  is inputed or not using if type(string_name) == str """
    e0 = entry0.get()
    if e0 >= 0:
        entry_values[0] = (e0)
    e1 = entry1.get()
    if e0 >= 0:
        entry_values[1] = (e1)
    e2 = entry2.get()
    if e0 >= 0:
        entry_values[2] = (e2)
    e3 = entry3.get()
    if e0 >= 0:
        entry_values[3] = (e3)
    e4 = entry4.get()
    if e0 >= 0:
        entry_values[4] = (e4)
    e5 = entry5.get()
    if e0 >= 0:
        entry_values[5] = (e5)
    e6 = entry6.get()
    if e0 >= 0:
        entry_values[6] = (e6)
    e7 = entry7.get()
    if e0 >= 0:
        entry_values[7] = (e7)
    e8 = entry8.get()
    if e0 >= 0:
        entry_values[8] = (e8)

    print entry_values


    return save_page


def display_values(root):
    sub2 = Frame(root)
    global entry_values


    label8 = Label(sub2, text = "Juveniles")
    label9 = Label(sub2, text = "Adults")
    label10 = Label(sub2, text = "Seniles")

    label11 = Label(sub2, text = "Population(Thousands)")
    label12 = Label(sub2, text = "Survival Rate(Between 1 and 0)")
    label13 = Label(sub2, text = "Birth Rate")
    label14 = Label(sub2, text = "Number of Generations")
    label15 = Label(sub2, text = "Disase Trigger Point")

    label16 = Label(sub2, text = entry_values[0])
    label17 = Label(sub2, textvariable = entry_values[1])
    label18 = Label(sub2, textvariable = "")
    label19 = Label(sub2, textvariable = "")
    label20 = Label(sub2, textvariable = "")
    label21 = Label(sub2, textvariable = "")
    label22 = Label(sub2, textvariable = "")
    label23 = Label(sub2, textvariable = "")
    label24 = Label(sub2, textvariable = "")


    button7 = Button(sub2, text="Return To Main Page",
                 command = lambda: switch_page("main"))


    label8.grid(row = 0, column = 1)
    label9.grid(row = 0, column = 2)
    label10.grid(row = 0, column = 3)
    label11.grid(row = 1, column = 0)
    label12.grid(row = 2, column = 0)
    label13.grid(row = 3, column = 0)
    label14.grid(row = 3, column = 3)
    label15.grid(row = 4, column = 0)
    label16.grid(row = 1, column = 1)
    label17.grid(row = 1, column = 2)
    label18.grid(row = 1, column = 3)
    label19.grid(row = 2, column = 1)
    label20.grid(row = 2, column = 2)
    label21.grid(row = 2, column = 3)
    label22.grid(row = 3, column = 1)
    label23.grid(row = 3, column = 3)
    label24.grid(row = 4, column = 1)

    button7.grid(row = 0, column = 0)

    return sub2 

1 个答案:

答案 0 :(得分:1)

要更改标签文本,您可以执行以下操作:

label["text"] = textVar

label.config(text=textVar)

因此,在上面的代码中,当条目更改时,请使用上述选项之一重新配置标签。