StringVar()返回PY_VAR1

时间:2017-11-07 03:58:23

标签: 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) > 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很新。无论哪种方式,任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:0)

解决:不知道StringVar()只能被分配一次,我有其他代码干扰它。除了评论其他代码外,我该如何解决这个问题?