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文件。