- 帖子
- 4
- 精华
- 0
- 积分
- 20
- 阅读权限
- 10
- 注册时间
- 2021-8-18
- 最后登录
- 2021-11-8
|
如下代码,我定义了两个函数,想在函数kaishi中调用pathCallBack中获得的文件地址,请问怎么改
global srtPath
strPath = StringVar()
strResult = StringVar()
var= tk.StringVar()
def pathCallBack():
filePath=filedialog.askopenfilename();
filePath=filePath.replace("/","\\\\") #通过replace函数替换绝对文件地址中的/来使文件可被程序读取 #注意:\\转义后为\,所以\\\\转义后为\\
if(filePath != ''):
strPath.set(filePath);
m=(re.findall(r'\d+',filePath))
N=m[len(m)-1]
strResult.set(N)
def kaishi():
data=open(strPath)
f=data.readlines()
data.close()
print(f[0])
得到的错误如下:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\hi\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "E:/python1/sp/chuangkou.py", line 33, in kaishi
data=open(strPath)
TypeError: expected str, bytes or os.PathLike object, not StringVar
|
|