在两个函数之间传递的 tkinter 参数
tkinter argument passing between two functions
from Tkinter import *
root=Tk()
x1=StringVar()
y=IntVar()
def hello(z):
print z
def temp():
root1=Tk()
x1.set("hello world")
Entry(root1,textvariable=x1).pack()
Button(root1,text="try me",command=(lambda :hello (x1.get()))).pack()
root1.mainloop()
Button(root,text="enter the new window",command=temp).pack()
root.mainloop()
我是 tkinter.i 的新手,我对 tkinter 中函数之间传递的参数感到震惊
在上面的代码中,当点击 try me 按钮时,我希望将在输入框中输入的文本传递给该函数,但每次打印 hello world 时都会传递给该函数。
请告诉我如何从条目中获取文本,并且该文本必须传递给函数
提前致谢
您正在将 x1.get() 传递给 hello() 这就是每次打印 hello world
的原因。取而代之的是获取当前 Entry
文本并将其传递给 hello()。要获取当前 Entry
文本,请使用 Entry Widget
的 get
方法
如下所示修改您的代码:
e = Entry(root1,textvariable=x1)
e.pack();
Button(root1,text="try me",command=(lambda :hello (e.get()))).pack()
from Tkinter import *
root=Tk()
x1=StringVar()
y=IntVar()
def hello(z):
print z
def temp():
root1=Tk()
x1.set("hello world")
Entry(root1,textvariable=x1).pack()
Button(root1,text="try me",command=(lambda :hello (x1.get()))).pack()
root1.mainloop()
Button(root,text="enter the new window",command=temp).pack()
root.mainloop()
我是 tkinter.i 的新手,我对 tkinter 中函数之间传递的参数感到震惊
在上面的代码中,当点击 try me 按钮时,我希望将在输入框中输入的文本传递给该函数,但每次打印 hello world 时都会传递给该函数。
请告诉我如何从条目中获取文本,并且该文本必须传递给函数 提前致谢
您正在将 x1.get() 传递给 hello() 这就是每次打印 hello world
的原因。取而代之的是获取当前 Entry
文本并将其传递给 hello()。要获取当前 Entry
文本,请使用 Entry Widget
get
方法
如下所示修改您的代码:
e = Entry(root1,textvariable=x1)
e.pack();
Button(root1,text="try me",command=(lambda :hello (e.get()))).pack()