Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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将文件名分配给变量_Python_Variables_Tkinter - Fatal编程技术网

Python 使用Tkinter将文件名分配给变量

Python 使用Tkinter将文件名分配给变量,python,variables,tkinter,Python,Variables,Tkinter,我希望能够使用tkinter条目允许用户键入文件名。该文件存储在一个变量中,然后用于运行我一直在使用的验证程序。但是,使用我现在的代码,我只得到一个无效的文件错误,并且代码没有运行 TypeError: invalid file: <function input_text at 0x035DBD68> 其思想是with语句下的代码在指定输入文本之前不会执行,但我找不到执行该操作的方法。如果希望在单击按钮后运行某些代码,请将其放入方法中 另外,input\u text被定义为函数,o

我希望能够使用tkinter条目允许用户键入文件名。该文件存储在一个变量中,然后用于运行我一直在使用的验证程序。但是,使用我现在的代码,我只得到一个无效的文件错误,并且代码没有运行

TypeError: invalid file: <function input_text at 0x035DBD68>

其思想是with语句下的代码在指定输入文本之前不会执行,但我找不到执行该操作的方法。

如果希望在单击按钮后运行某些代码,请将其放入方法中

另外,
input\u text
被定义为函数,
open
需要一个文件作为错误状态。您可能希望将
mtext
StringVar
的内容直接用作文件名

def input_text():
    mtext = stuff.get() #notice the parentheses. You need to call the get method
    label2 = Label(root,text=mtext).pack()
    with open(mtext, 'r') as f:
        reader = csv.reader(f)


root = Tk()
stuff = StringVar()

root.title("Project")
root.geometry('300x100')

label = Label(root,text="My Project").pack()
button1 = Button(root,text="OK",command=input_text).pack()
entry1 = Entry(root,textvariable=stuff).pack()

请注意,如果要在该方法之外访问打开的文件,应将其设置为全局文件或使用类结构。

BTW:始终对完整错误消息(回溯)提出疑问,而不仅仅是最后一部分。还有其他有用的信息。也就是说,它显示了哪条线产生了问题。
def input_text():
    mtext = stuff.get() #notice the parentheses. You need to call the get method
    label2 = Label(root,text=mtext).pack()
    with open(mtext, 'r') as f:
        reader = csv.reader(f)


root = Tk()
stuff = StringVar()

root.title("Project")
root.geometry('300x100')

label = Label(root,text="My Project").pack()
button1 = Button(root,text="OK",command=input_text).pack()
entry1 = Entry(root,textvariable=stuff).pack()