Python, Tkinter里的button导入的文件,如何被其他函数使用
我用Tkinter包写了一个打开文件的GUI,defOpenFile():paths=tkFileDialog.askopenfilename()returnpaths想...
我用Tkinter包写了一个打开文件的GUI,
def OpenFile():
paths=tkFileDialog.askopenfilename()
return paths
想通过open(paths)的方式导入文件,并被requests包使用,但是一直提示错误,请问为什么啊 展开
def OpenFile():
paths=tkFileDialog.askopenfilename()
return paths
想通过open(paths)的方式导入文件,并被requests包使用,但是一直提示错误,请问为什么啊 展开
1个回答
展开全部
你用的python版本是什么?
还有,你倒是把错误提示贴出来啊?
python2.7 选择文件,获取路径:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter as tk
from tkFileDialog import askopenfilename
class Demo(object):
def __init__(self, master=None):
self.root = master
self.create_frame()
def create_frame(self):
self.btn = tk.Button(self.root, text="OpenFile", command=self.OpenFile)
self.label = tk.Label(self.root, text="")
self.btn.grid(row=0, column=0, padx=15, pady=15, sticky="wesn")
self.label.grid(row=1, column=0, padx=15, pady=15, sticky="wesn")
def OpenFile(self):
file_path = askopenfilename(filetypes=(("py files", "*.py"),
("All files", "*.*")))
if not file_path:
return
self.label["text"] = file_path
if __name__ == '__main__':
'''
main loop
'''
root = tk.Tk()
root.title("Demo")
Demo(master=root)
root.resizable(False, False)
root.mainloop()
还有,你倒是把错误提示贴出来啊?
python2.7 选择文件,获取路径:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter as tk
from tkFileDialog import askopenfilename
class Demo(object):
def __init__(self, master=None):
self.root = master
self.create_frame()
def create_frame(self):
self.btn = tk.Button(self.root, text="OpenFile", command=self.OpenFile)
self.label = tk.Label(self.root, text="")
self.btn.grid(row=0, column=0, padx=15, pady=15, sticky="wesn")
self.label.grid(row=1, column=0, padx=15, pady=15, sticky="wesn")
def OpenFile(self):
file_path = askopenfilename(filetypes=(("py files", "*.py"),
("All files", "*.*")))
if not file_path:
return
self.label["text"] = file_path
if __name__ == '__main__':
'''
main loop
'''
root = tk.Tk()
root.title("Demo")
Demo(master=root)
root.resizable(False, False)
root.mainloop()
追问
def OpenFile():
paths=tkFileDialog.askopenfilename()
return paths
def video_scrapy():
files=open(paths)
希望实现的是OpenFile()读取了某个txt文件的路径后,能够为video_scrapy()函数所用,变量怎么样能够实现传递?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询