设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 9347|回复: 2
打印 上一主题 下一主题

菜鸟求助,请大神帮忙解惑

[复制链接]

2

主题

0

好友

20

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2021-8-18 11:42:32 |只看该作者 |倒序浏览
如下代码,我定义了两个函数,想在函数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


回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2021-8-18 22:04:37 |只看该作者
strPath.get()

参考:
https://www.cnblogs.com/hackpig/p/8206763.html
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

2

主题

0

好友

20

积分

新手上路

Rank: 1

板凳
发表于 2021-8-23 16:11:11 |只看该作者
crossin先生 发表于 2021-8-18 22:04
strPath.get()

参考:

学会了,谢谢大神
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-19 11:21 , Processed in 0.020321 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部