简体   繁体   English

Python:如何在 Tkinter 上存储用户输入的文本?

[英]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小部件做到这一点。
  • If you press the 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:方法一:

  • is to update the 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:方法二:

  • is to use global keyword inside the function and store the raw_input 's data to the global variable.是在 function 中使用global关键字并将raw_input的数据存储到全局变量中。
def userinput():
    global data
    data = raw_input(v.get())

method 3:方法3:

  • is to update the 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.

相关问题 如何在tkinter,Python 3.2.5的文本框中打印并输入用户? - How do I print and have user input in a text box in tkinter, Python 3.2.5? 如何将用户输入放入文本小部件中并使用 Python 使用 tkinter 在日志文件中进行搜索 - How can I put user input inside text widget and do search in Log file with tkinter using Python 如何在 tkinter 中将用户输入文本转换为小写? - How do I convert user input text to lowercase in tkinter? 如何从 Tkinter GUI 表单存储用户数据? - How do I store user data from a Tkinter GUI form? 如何在tkinter python的文本文档中保存之前的输入 - How do I save the previous input in the text document in tkinter python 如何使用 tkinter 从用户输入中获取数据? - How do I get data from user input with tkinter? 如何从 tkinter 中的文本输入中获取变量? - How do I get a variable from a text input in tkinter? 如何从 python 3.8 中的 tkinter 中的文本获取输入 - how can i get input from Text in tkinter in python 3.8 我如何要求用户输入数字然后存储在文本文件中? - How do I ask user to input numbers to then store in text file? 如何存储从用户输入创建的变量并在 Python 的不同文件中使用它? - How do I store a variable that has been created from a user input and use it in a different file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM