简体   繁体   English

如何在Python Tkinter GUI的“标签”小部件中显示更新的值?

[英]How do I show updated values in the 'label' widget in Python Tkinter GUI?

I have a python Tkinter based GUI in which I wish to show values of several variables when the "read" button is clicked. 我有一个基于python Tkinter的GUI,我希望在单击“读取”按钮时显示几个变量的值。 Ideally, this should happen in a certain frame in a window where the updated variables are shown, without disturbing the rest of the GUI widgets, which have other functions. 理想情况下,这应该发生在显示更新变量的窗口中的某个帧中,而不会干扰具有其他功能的其他GUI小部件。

The problem I'm encountering is this- Each time I hit "read", the updated variables are listed right underneath the older variables instead of overwriting in that location. 我遇到的问题是这样 - 每次我点击“读取”时,更新的变量都列在旧变量的正下方,而不是在该位置覆盖。 I seem to have an incorrect understanding of the working of the label, and padx, pady (the placement of the labels). 我似乎对标签的工作以及padx,pady(标签的放置)有不正确的理解。 I have pasted the code below. 我已粘贴下面的代码。 What should be the correct way so that the previous data is erased and new data is pasted in that very location? 什么应该是正确的方法,以便先前的数据被删除,新数据被粘贴在那个位置?

def getRead():
    #update printList
    readComm()
    #update display in GUI
    i=0
    while i < (len(printList)):
        labelR2=Tk.Label(frameRead, text=str(printList[i]))
        labelR2.pack(padx=10, pady=3)
        i=i+1

frameRead=Tk.Frame(window)
frameRead.pack(side=Tk.TOP)
btnRead = Tk.Button(frameRead, text = 'Read', command= getRead)
btnRead.pack(side = Tk.TOP)  

window.mainloop()

The code above successfully displays elements of printList in one column type display. 上面的代码在一个列类型显示中成功显示了printList的元素。 However, each time getRead is called (when the read button is clicked), it appends to the previous display. 但是,每次调用getRead时(单击读取按钮时),它都会附加到上一个显示。

PS - If there is a better way to display data, other than label widget, then please suggest that. PS - 如果有更好的方式来显示数据,除了标签小部件,那么请建议。

The problem is you're creating a new set of labels each time you run getRead . 问题是每次运行getRead时都要创建一组新的标签。 It sounds like what you want to do is update the text of the existing labels, not create new ones. 听起来你想要做的是更新现有标签的文本,而不是创建新标签。 Here's one way to do that: 这是一种方法:

labelR2s = []

def getRead():
    global labelR2s
    #update printList
    readComm()
    #update display in GUI

    for i in range(0, len(labelR2s)):               # Change the text for existing labels
        labelR2s[i].config(text=printList[i])

    for i in range(len(labelR2s), len(printList)):  # Add new labels if more are needed
        labelR2s.append(Tk.Label(frameRead, text=str(printList[i])))
        labelR2s[i].pack(padx=10, pady=3)

    for i in range(len(printList), len(labelR2s)):  # Get rid of excess labels
        labelR2s[i].destroy()
    labelR2s = labelR2s[0:len(printList)]

    window.update_idletasks()

I'm answering my own question, by modifying the answer Brionius gave, since his answer is giving a referencing error. 我正在回答我自己的问题,修改了Brionius给出的答案,因为他的回答是引用了一个引用错误。 This code below works fine for me. 以下代码对我来说很好。

labelR2s=[]
def getRead():
    #update printList
    readComm()
    #update display in GUI

    for i in range(0, len(printList)):               # Change the text for existing labels
        if (len(printList)>len(labelR2s)):           # this is the first time, so append
            labelR2s.append(Tk.Label(frameRead, text=str(printList[i])))
            labelR2s[i].pack(padx=10, pady=3)
        else:
            labelR2s[i].config(text=printList[i])    # in case of update, modify text

    window.update_idletasks()

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

相关问题 Tkinter标签小部件未更新 - Tkinter Label widget is not updated Tkinter,Python:如何保存在“输入”小部件中输入的文本? 如何移动标签? - Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label? 如何获得GUI在标签中而不是{C和D}上打印C和D? Python Tkinter - How do I get the GUI to print out C and D in the label and not {C and D}? Python Tkinter 如何使我的python tkinter输出显示在GUI中而不是python shell中? - How do I make my python tkinter output to show in GUI instead of python shell? 如何使用 Tkinter 刷新 python 中的 label - How do i refresh the label in python with Tkinter 我试图将标准输出重定向到tkinter标签小部件,但文本未正确更新 - I tried to redirect stdout to tkinter label widget but text is not updated correctly 如何在 GUI 中使用 Tkinter 在 Python 中显示过滤后的 DataFrame? - How can i show in GUI a filtered DataFrame in python with Tkinter? 在Tkinter标签中显示更新的文本 - Show updated text in Tkinter Label 如何在 python gui 中的 label 小部件的打包方法中同时将 label 对齐到右上角? - how can i align label to top right at the same time in pack method of label widget in python gui? Tkinter:如何使用 scale-widget 的值来更改 label-widget 的文本? - Tkinter: How do I use the value of a scale-widget to change the text of a label-widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM