![](/img/trans.png)
[英]getSaveFileName , getOpenFileName shows me the file to save or open, but cannot save the file or open the file in python QT
[英]Open a file without getOpenFileName?
有沒有不使用QFileDialog.getOpenFileName
參數打開文件的方法? 關鍵是,我有一些按鈕,單擊它們會彈出一個記事本,您可以在其中輸入任何內容。 然后,您可以將在該記事本中編寫的任何內容保存為文本文件。 我想做的是,如果再次單擊該按鈕,我將重新打開以前通過記事本編輯的文件,並可以繼續輸入我從上次中斷的地方。 但是,我不想使用getOpenFileName
。 不使用此功能就可以打開文件嗎? 下面是我的嘗試,但是我的if語句不斷評估為假。 如果有人可以幫助,那就太好了。 謝謝!
#Testing if the file already exists
if(os.path.exists("~/Desktop/" +self.fileName + ".txt")):
f = open(self.fileName + ".txt", 'r')
filedata = f.read()
self.text.setText(filedata)
f.close()
#Opens a new notepad if there wasn't a previous fileconstructed
else:
self.textBox = textBoxWindow(self.fileName)
self.textBox.show()
如果您使用的是Winsows(您說的是Notepad ),則可以使用subprocess
進程模塊打開具有當前與該文件類型關聯的程序的任何文件,如下所示:
import subprocess
self.filename = r'C:\test.txt'
subprocess.call(['start', self.filename], shell=True)
但是shell=True
參數有點危險,尤其是文件名作為輸入。
您也可以使用webbrowser
模塊,盡管我不支持使用它:
import webbrowser
webbrowser.open(self.filename)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.