Tkinter:如何访问每个字符的控制变量字符



Hello_,我对Python很陌生,我发现了Tkinter,如果我的标题不那么清楚,我很抱歉,提前谢谢。

我想做的是在一个Entry小部件中键入一些数字,并根据公式将它们打印在另一个Entry部件中。但我希望每个数字都被修改,例如,如果公式是x到x**2,如果我键入12,我希望是14,而不是144。

如何编写回调函数来实现这一点?

我的代码显然返回数字**2,而不是每个数字**2:

import tkinter

def CallBack(*args):
Entry_variable1.set(Entry_variable.get() ** 2)

Appli = tkinter.Tk()
Appli.title("x to x**2")
Entry_variable = tkinter.IntVar()
Entry_variable1 = tkinter.IntVar()
Entry_variable.trace("w", CallBack)
Entry = tkinter.Entry(Appli, textvariable=Entry_variable)
Entry1 = tkinter.Entry(Appli, textvariable=Entry_variable1)


Entry.pack()
Entry1.pack()
Appli.mainloop()

好吧,如果列表理解对您来说很难理解,您可以使用easy-for循环来迭代stringvar()

def CallBack(*args):
Entry_variable1.set("") # reset the second value in the input,And both of variable is Stringvar()
for i in Entry_variable.get(): # use for loop through it.
Entry_variable1.set(Entry_variable1.get()+str(int(i)**2)) # each of them **2 and add it to the variable.

最新更新