正如标题所说,当我的StringVar变量应该返回两个字符串的公共前缀时,它将返回PY_VAR1。我以前也遵循同样的逻辑,这一点很管用。在
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()只能分配一次,我有其他代码干扰它。除了注释其他代码之外,我如何解决这个问题?在
相关问题 更多 >
编程相关推荐