Python StringVar()返回PY_VAR1
正如标题所述,我的StringVar变量在返回两个字符串的公共前缀时返回PY_VAR1。我以前也遵循同样的逻辑,这一点已经奏效了Python StringVar()返回PY_VAR1,python,tkinter,Python,Tkinter,正如标题所述,我的StringVar变量在返回两个字符串的公共前缀时返回PY_VAR1。我以前也遵循同样的逻辑,这一点已经奏效了 def getPreFix(): wordOne = ent1.get() wordTwo = ent2.get() if (len(wordOne) > len(wordTwo)): longWord = wordOne shortWord = wordTwo elif (len(wordTwo
def getPreFix():
wordOne = ent1.get()
wordTwo = ent2.get()
if (len(wordOne) > len(wordTwo)):
longWord = wordOne
shortWord = wordTwo
elif (len(wordTwo) > len(wordOne)):
longWord = wordTwo
shortWord = wordOne
elif (len(wordTwo) == len(wordOne)) or (len(wordOne) == len(wordTwo)):
randNum = r.randint(0,1)
if randNum == 0:
longWord = wordOne
shortWord = wordTwo
elif randNum == 1:
longWord = wordTwo
shortWord = wordOne
commonPreFix = []
lShort = len(shortWord)
for i in range(0,(len(shortWord))):
if shortWord[i] == longWord[i]:
commonPreFix.append(shortWord[i])
print(*commonPreFix)
cpfLabel = ''.join(commonPreFix)
print(cpfLabel)
h.set(cpfLabel) #Problem is here
root1 = Tk()
root1.geometry("200x200")
root1.title("LCP")
Label(root1, text = "Enter a word").pack()
ent1 = Entry(root1)
ent1.pack(pady=(5,0))
Label(root1, text = "Enter another word").pack()
ent2 = Entry(root1)
ent2.pack(pady=(5,0))
bOne = Button(root1, text="Enter", command = getPreFix)
h = StringVar()
Label(root1, textvariable = h).pack(side=BOTTOM) #Problem is also here
bOne.pack(pady=(5,0))
我意识到这很可能是一些基本的东西,因为我对python非常陌生。无论哪种方式,都非常感谢您的帮助。已解决:我不知道StringVar()只能分配一次,我有其他代码干扰了这一点。除了注释其他代码外,我如何解决这个问题?请注意,您只需检查相同的长度一次,就可以记住这个;`或者(len(wordOne)==len(wordTwo)`您不正确。您可以根据需要多次分配给
StringVar
。