Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在tkinter中将StringVar()命令作为字符串使用从条目读取的输入命令_Python_Tkinter - Fatal编程技术网

Python 如何在tkinter中将StringVar()命令作为字符串使用从条目读取的输入命令

Python 如何在tkinter中将StringVar()命令作为字符串使用从条目读取的输入命令,python,tkinter,Python,Tkinter,我正在用tkinter GUI创建一个Python程序。我正在阅读一篇来自用户的文本,目的是将此文本用作进一步函数的参数。但是这些函数要求文本是“字符串”数据类型,而不是“类方法”,如果我使用.get()函数,就会出现这种情况 我使用Entry小部件命令读取并使用StringVar()作为变量。 我在parse1()函数中尝试了str(content.get),但这不起作用 def parse1(): string1=str(content.get) try:

我正在用tkinter GUI创建一个Python程序。我正在阅读一篇来自用户的文本,目的是将此文本用作进一步函数的参数。但是这些函数要求文本是“字符串”数据类型,而不是“类方法”,如果我使用
.get()
函数,就会出现这种情况

我使用Entry小部件命令读取并使用
StringVar()
作为变量。 我在
parse1()
函数中尝试了
str(content.get)
,但这不起作用

def parse1():

    string1=str(content.get)

    try:
        txt = TextBlob(string1) #TextBlob is a function used for string processing
        for sentence in txt.sentences:
            genQuestion(sentence)       
    except Exception as e:
        raise e

Label(window, text="Text").grid(row=0)
content = StringVar()
e1 = Entry(window, textvariable=content)
e1.grid(row=0, column=1)
Button(window, text='Quit', command=window.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(window, text='ADD', command=parse1).grid(row=3, column=1, sticky=W, pady=4)
window.mainloop()
我希望使用
str()
可以将
content.get()
数据类型设置为字符串。当我尝试
string1=str(content.get)
时,什么都没有发生,程序也没有进一步的进展。如果我尝试打印(string1)以检查,我会得到:

<bound method StringVar.get of <tkinter.StringVar object at 0x000001D17418B710>> 
这意味着字符串数据类型需要作为
TextBlob
中的参数。从用户处读取的数据有没有办法变成字符串数据类型?

请考虑以下代码:

string1=str(content.get)
在上面,您没有调用
get
方法,只是提供了名称
get
是一种方法,与任何python方法一样,必须使用括号来调用函数。此外,不需要将其转换为字符串,因为
get
方法将返回一个字符串:

string1 = content.get()
如果要调用条目上的方法并删除对
StringVar
的依赖关系,可以采用类似的方法:

string1 = e1.get()

string1=str(content.get)
更改为
string1=content.get()。使用
get()
方法时需要括号,并且
get()
的结果已经是一个字符串。是的,使用
content.get(“1.0”,END)
捕获输入字段的内容。@Rémy
.get(“1.0”,END)
不正确<代码>内容
StringVar
而不是文本框
get()
就足够了。对于
条目
字段,正确的方法是
get(0,“end”)
get()
而不是
1.0
,因为
get(1.0,“end”)
是用于文本小部件的。哎呀,正确!条目不是文本。
string1 = e1.get()