StringVar()返回PY_VAR1

2024-09-04 01:38:31 发布

您现在位置:Python中文网/ 问答频道 /正文

正如标题所说,当我的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还很陌生。不管怎样,任何帮助都是感激的。在


Tags: leniflabelpackeliflongwordrandnument2