gpt4 book ai didi

python - 如何更新 Tkinter 中的标签,StringVar() 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:28 24 4
gpt4 key购买 nike

我正在编写这段比较两个字符串的单个字符的短代码。第一次运行后,当我更改 entryBoxes 中的字符串时,我想替换之前创建的标签,而不是创建一个新标签。我已经尝试过使用 StringVar() 但它似乎不起作用。 (如果它有用,我正在使用 Python 2.7.6)。你能给我一个提示吗?

from Tkinter import *

app = Tk()
app.geometry('450x300')


labelTF = Label(app, text="Insert sequence of TF").pack()
eTF = Entry(app, width=50)
eTF.pack()
eTF.focus_set()


labelSpazio = Label(app, text="\n").pack()

labelResultedSequence = Label(app, text="Insert sequence of ResultedSequence").pack()
eResultedSequence = Entry(app, width=50)
eResultedSequence.pack()
eResultedSequence.focus_set()

def prova():
count = 0
uno = eTF.get().lower()
due = eResultedSequence.get().lower()
if len(uno)==len(due):
for i in range(0,len(uno)):
if uno[i] == due[i]:
if uno[i] in ("a", "c","g","t"):
count = count + 1
if uno[i] == "r" and due[i] in ("a", "g"):
count = count + 1
if uno[i] == "y" and due[i] in ("t", "c"):
count = count + 1
percentage = int(float(count)/float(len(uno))*100)
labelSpazio = Label(app, text="\n").pack()
mlabel3=Label(app,text= "The final similarity percentage is: "+(str(percentage) + " %")).pack()

if len(uno)!=len(due):
mlabel2 = Label(app,text="The length of the sequences should be the same").pack()



b = Button(app, text="get", width=10, command=prova)
b.pack()

mainloop()

最佳答案

仅在 for 循环之外创建标签一次,并使用 StringVar 修改其值。它看起来像这样:

# initialization
app = Tk()
label3text = StringVar()
mlabel3 = Label(app, textvariable=label3text, width=100)
mlabel3.pack()

然后在函数内的 for 循环中:

label3text.set("The final similarity percentage is: "+(str(percentage) + " %"))

关于python - 如何更新 Tkinter 中的标签,StringVar() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com