更改条目值时更改标签值

更改条目值时更改标签值

本文介绍了更改条目值时更改标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我的问题是我试图将标签(标签16)更改为entry_values [0]的第一个值,该值不起作用.经过一个小时的研究,我找不到解决方案.我认为主要的问题是,它会在运行带有条目的代码之前设置标签,以便它不会更改.当我将其设置为textvariable时,它会生成一个空字符串(我认为),但是当我仅使用文本时,它会在期望数字的位置放入0.

My problem at the moment is I am trying to change a label(label 16) to the first value of entry_values[0] which isn't working I have tried passing it in as a variable and many other things, after about an hour of research I couldn't find a solution.I think the main problem is that it sets the label before the code with the entry is run so that it wont change. when I set it to a textvariable it produces an empty string (I think) but when I use just text it puts in a 0 where I expect my number.

 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

推荐答案

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

In order to change the text of a label you can do:

label["text"] = textVar

label.config(text=textVar)

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

So in your above code, when the entry changes, reconfigure the label using one of the above options.

这篇关于更改条目值时更改标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:49