【问题标题】:Change specific label text更改特定标签文本
【发布时间】:2021-09-04 14:07:02
【问题描述】:

谁能告诉我在这种情况下如何获取特定标签?

这是一个循环,所以它总是转到最后一个项目标签。

我可以传递 ScrolledText 但不能传递标签,标签是 lblspec,这个 (my_var_expected.set(textlabl)) 有效但只更新 for 循环的最后一个标签

def checkval(e, spec0, spec1, spec2, spec3, spec4, spec5):
    e.widget.insert("1.0", PLACEHOLDER if e.widget.get("1.0", "end-1c") == "" else "")
    reqlidst=(spec0, spec1, spec2, spec3, spec4, spec5)

    textlabl=""

    for x in reqlidst:
        x = x.replace("#", "")
        x = x.replace(" ", "")
        if len(x)>1:
            if x.upper() in e.widget.get("1.0", "end-1c").upper():
                if len(textlabl)>1:
                    textlabl = textlabl+ x.replace(" ", "").upper() + "  " + html.unescape('✔')+"\n\n"
                else:
                    textlabl = x.replace(" ", "").upper() + "  " + html.unescape('✔')+"\n\n"
            else:
                if len(textlabl) > 1:
                    textlabl = textlabl+ x.replace(" ", "").lower() + "  " + html.unescape('✘')+"\n\n"
                else:
                    textlabl = x.replace(" ", "").lower() + "  " + html.unescape('✘')+"\n\n"

        my_var_expected.set(textlabl)<---- cant get this to update the correct label
        print(textlabl)


    for line in var_expect_result:
        if len(line)>1:
            x, y, y1, y2, y3, y4, y5 = line.split('<->')
            y_respexpt = (y + "\n\n" + y1 + "\n\n" + y2 + "\n\n" + y3 + "\n\n" + y4 + "\n\n" + y5).replace('##', '').strip()
            y_respexpt = y_respexpt.replace(' ', '').strip()


            my_var_expected = StringVar()
            my_var_expected.set(y_respexpt)

            kiblogbn = ScrolledText(frmbtn, name="frm_"+str(x), width=25, height=30,border=2,relief="solid")
            kiblogbn.grid(column=coll, row=2, padx=10, pady=10, ipady=25,sticky="W")
            PLACEHOLDER = 'Copy logs from Kibana and past them here.'
            kiblogbn.insert(END, PLACEHOLDER)
            kiblogbn.bind("<FocusIn>", lambda e: e.widget.delete("1.0", "end-1c") if e.widget.get("1.0", "end-1c") == PLACEHOLDER else None)
            kiblogbn.bind("<FocusOut>", lambda e, y=y, y1=y1, y2=y2, y3=y3, y4=y4, y5=y5: checkval(e, y, y1, y2, y3, y4, y5))

         
            lblspec = Label(frmbtn, textvariable=my_var_expected, justify='left', anchor=N)
            lblspec.grid(column=coll, row=6, rowspan=99, sticky=N, padx=20, pady=20)

【问题讨论】:

  • textlabl = textlabl, x.replace(" ", "").upper() + ":VALID:" 是字符串吗?还是一个元组?而x 始终是reqlidst 中的最后一项。
  • 它是一个字符串,已经更改了我的代码。 x 是来自 reqlidst 的值,例如:reqlidst=one,word,to,rule,them,all
  • 如果您对所有标签使用一个StringVar,那么显然所有标签将具有相同的值,请提供一个minimal reproducible example,您也可以简单地使用def checkval(e, *args),然后您可以简单地使用@ 987654328@ 因为它已经是您在第一个参数之后指定的许多值的元组

标签: python python-3.x tkinter


【解决方案1】:

其中一种方法是使用kiblogbn的属性来保存变量my_var_expected的引用:

for line in var_expect_result:
    if len(line)>1:
        ...
        my_var_expected = StringVar()
        my_var_expected.set(y_respexpt)

        kiblogbn = ScrolledText(frmbtn, name="frm_"+str(x), width=25, height=30,border=2,relief="solid")
        kiblogbn.my_var_expected = my_var_expected # save reference of variable
        ...

然后在checkval()中使用e.widget.my_var_expected

def checkval(e, spec0, spec1, spec2, spec3, spec4, spec5):
    ...
    for x in reqlidst:
        ...
        e.widget.my_var_expected.set(textlabl)
        print(textlabl)

【讨论】:

  • 谢谢,不知道,非常感谢,会派上用场的
猜你喜欢
  • 2020-05-16
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多