开发者社区> 问答> 正文

python tkinter中想打开一个.txt文件并显示在text中?报错

这是我写的open函数,但在打开文件时总会报错,望坛友赐教。

def open():

    fd = LoadFileDialog(root)
    filename = fd.go()
    file = open(""+filename,"r")
    te.setvar(file.read())
    file.close()
    print(filename)

error:Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/home/tom/PycharmProjects/farm/text.py", line 29, in open
    file = open(""+filename,"r")
TypeError: open() takes no arguments (2 given)

展开
收起
爱吃鱼的程序员 2020-06-14 15:52:07 952 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    Youoverwriteedthepythonpredefinedfileoperationfunction.open()isloadedwhenpythoninterpretorlaunched.Plsrenameyourfunctiontoanothername.Sorry,noChineseinputinmypad.
    <preclass="brush:python;toolbar:true;auto-links:false;">defopen1():fd=LoadFileDialog(root)filename=fd.go()file=open(""+filename,"r")te.setvar(file.read())file.close()print(filename)



    你好,虽然已经编译无措了,但是text中依旧没有显示。这是为什么啊?

    2020-06-14 15:52:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载