1. python TK怎麼做一個瀏覽按鈕獲取文件全路徑,下邊的代碼只能獲取到最後一層文件夾,不能獲取文件名
因為你函數用錯了啊
tkFileDialog.askdirectory是用來獲取目錄的
tkFileDialog.askopenfilename是用來獲取文件全路徑的
tkFileDialog.askopenfilenames是用來獲取多個文件的路徑的
2. python如何選擇文件,並獲得所選擇文件的全路徑名稱
這是所有GUI編程裡面基礎的內容,直接使用框架提供的內置函數即可。比版如pyqt,使用
fromPyQt4importQtGui
#insideaQWidgetclass
fname=QtGui.QFileDialog.getOpenFileName(self,'Openfile')
默認返回的就是完整路權徑。
3. pyqt5按鈕打開文件
[python]view plain
importsys
importos
fromPyQt5.QtCoreimport*
fromPyQt5.QtWidgetsimport*
classNotepad(QMainWindow):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
openAction=QAction('Open',self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Openafile')
openAction.triggered.connect(self.openFile)
closeAction=QAction('Close',self)
closeAction.setShortcut('Ctrl+Q')
closeAction.setStatusTip('CloseNotepad')
closeAction.triggered.connect(self.close)
menubar=self.menuBar()
fileMenu=menubar.addMenu('&File')
fileMenu.addAction(openAction)
fileMenu.addAction(closeAction)
self.textEdit=QTextEdit(self)
self.textEdit.setFocus()
self.textEdit.setReadOnly(True)
self.resize(700,800)
self.setWindowTitle('Notepad')
self.setCentralWidget(self.textEdit)
self.show()
defopenFile(self):
filename,_=QFileDialog.getOpenFileName(self,'OpenFile',os.getenv('HOME'))
fh=''
ifQFile.exists(filename):
fh=QFile(filename)
ifnotfh.open(QFile.ReadOnly):
QtGui.qApp.quit()
data=fh.readAll()
codec=QTextCodec.codecForUtfText(data)
unistr=codec.toUnicode(data)
tmp=('Notepad:%s'%filename)
self.setWindowTitle(tmp)
self.textEdit.setText(unistr)
defmain():
app=QApplication(sys.argv)
notepad=Notepad()
sys.exit(app.exec_())
if__name__=='__main__':
main()
4. Qt選取文件路徑,上一次的文件路徑
你將上次選擇的路徑保存在一個變數中
下次再彈出文件對話框中,將其路徑預先設置成保存的這個路徑。
看看相應的類,有提供方法的。
5. python中選擇文件夾(即路徑)的對話框如何實現
1、首先,確保我們已經正確安裝了python2.7的環境,然後,編輯一個.py文件。