[英]Python: How do i store a text input from the user on Tkinter?
This is what i tried so far to get the user's input:到目前为止,这是我尝试获取用户输入的方法:
master = Tk()
master.title('Title')
v = StringVar()
L1 = Label(master, text = 'Name')
L1.pack(side = LEFT)
E1 = Entry(master, textvariable = v, bd = 5)
E1.pack(side = RIGHT)
def userinput():
a = raw_input(v.get())
print a
b = Button(master, text = 'Submit', command = userinput)
b.pack(side = BOTTOM)
master.mainloop()
The real question is, how can i store the v.get() raw_input into a variable outside the function?真正的问题是,如何将 v.get() raw_input 存储到 function 之外的变量中? Thx!
谢谢!
You can make a global variable to hold the input: 您可以创建一个全局变量来保存输入:
inp = None
and then have your function update that variable: 然后让您的函数更新该变量:
def userinput():
global inp
a = raw_input(v.get())
print a
inp = a
So, your code will look like this: 因此,您的代码将如下所示:
master = Tk()
master.title('Title')
v = StringVar()
# Variable to hold the input
inp = None
L1 = Label(master, text = 'Name')
L1.pack(side = LEFT)
E1 = Entry(master, textvariable = v, bd = 5)
E1.pack(side = RIGHT)
def userinput():
# Declare 'inp' to be global
global inp
a = raw_input(v.get())
print a
# Update the variable
inp = a
b = Button(master, text = 'Submit', command = userinput)
b.pack(side = BOTTOM)
master.mainloop()
In the above code, inp
will always be a fresh copy of the input which you can use elsewhere in the code. 在上面的代码中,
inp
将始终是inp
的全新副本,您可以在代码的其他地方使用它。
However, it might be worthwhile to look into making your code a class. 但是,可能值得考虑将您的代码变成一个类。 That way, you can have a class attribute named
self.inp
and you won't have to do global inp
. 这样,您可以拥有一个名为
self.inp
的类属性,而不必执行global inp
。
As others say - use globe
正如其他人所说-使用
globe
Example using class 使用类的例子
from Tkinter import *
class MainWindow():
def __init__(self, master):
self.master = master
self.master.title('Title')
self.v = StringVar()
self.L1 = Label(self.master, text = 'Name')
self.L1.pack(side = LEFT)
self.E1 = Entry(self.master, textvariable = self.v, bd = 5)
self.E1.pack(side = RIGHT)
self.B1 = Button(self.master, text = 'Submit', command = self.userinput)
self.B1.pack(side = BOTTOM)
self.B2 = Button(self.master, text = 'Print', command = self.print_external_variable)
self.B2.pack(side = TOP)
#------------------
def userinput(self):
global external_variable
external_variable = raw_input(self.v.get())
#external_variable = self.v.get()
print "inside - userinput:", external_variable
#------------------
def print_external_variable(self):
# you don't need global if you don't change external_variable
print "inside - print:", external_variable
#----------------------------------------------------------------------
external_variable = '- none -'
print "before run:", external_variable
master = Tk()
MainWindow(master)
master.mainloop()
print "after run:", external_variable
First of all, why using raw_input
to take input when you can do the same with the entry
widget?首先,当您可以对
entry
小部件执行相同操作时,为什么还要使用raw_input
来获取输入?
There are few disadvantages of following the above practice:遵循上述做法有几个缺点:
raw_input
will cause the whole python interpreter to pause, but entry
widget wont do that. raw_input
将导致整个 python 解释器暂停,但entry
小部件不会这样做。raw_input
can't be customized or it's bit hard to customize, but we can easily do that for entry
widget. raw_input
不能定制或者定制起来有点困难,但是我们可以很容易地为entry
小部件做到这一点。button
again without entering data in raw_input
, it will raise an error, but in entry
widget it wont.button
而不在raw_input
中输入数据,则会引发错误,但在entry
小部件中不会。 and for your problem's solution, there are 3 methods.对于您的问题的解决方案,有 3 种方法。
method 1:方法一:
textvariable
value, then you can access it using v.get()
.textvariable
v.get()
访问它。 ( Best ) def userinput():
a = raw_input(v.get())
v.set(a)
# This would also cause an update in entry widget.
method 2:方法二:
global
keyword inside the function and store the raw_input
's data to the global variable.global
关键字并将raw_input
的数据存储到全局变量中。def userinput():
global data
data = raw_input(v.get())
method 3:方法3:
text
value of the button or entry box, then you can access it using b.cget("text")
.text
值,然后您可以使用b.cget("text")
访问它。 ( changing text value is practically not useful, but still a method. ) def userinput():
a = raw_input(v.get())
b.config(text=a)
I would suggest you to use method 1.我建议你使用方法1。
Just make it return the User Input 只需使其返回用户输入
def userinput():
a = raw_input(v.get())
return a
Then when you call it,you can do that by: 然后,当您调用它时,可以通过以下方法实现:
myvar=userinput()
Now, myvar
contains the value of user input. 现在,
myvar
包含用户输入的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.