纳速健身
标题:
[pyqt]PyQt获取系统文件图标
[打印本页]
作者:
awagink
时间:
2021-1-14 09:18
标题:
[pyqt]PyQt获取系统文件图标
其实也就3行代码
fileInfo = Qt.QFileInfo(filename)
fileIcon = Qt.QFileIconProvider()
icon = QtGui.QIcon(fileIcon.icon(fileInfo))
复制代码
下面是一个小例子:
# !/usr/bin/env python
# -*- coding:utf-8 -*-
from PyQt4 import QtGui, Qt, QtCore
class GetIconDemo(QtGui.QPushButton):
def __init__(self, parent=None):
super(GetIconDemo, self).__init__(parent)
self.setWindowTitle(u"获取图标")
self.setIconSize(Qt.QSize(50, 50))
self.clicked.connect(self.getIconClick)
def getIconClick(self):
filename = unicode(QtGui.QFileDialog.getOpenFileName(self, u" 获取文件... " , '', u'所有类型(*)'))
if filename:
icon, name = self.getFileInfo(filename)
self.setIcon(icon)
self.setText(name)
def getFileInfo(self, filename):
"""获取文件的图片和名字"""
fileInfo = Qt.QFileInfo(filename)
fileIcon = Qt.QFileIconProvider()
icon = QtGui.QIcon(fileIcon.icon(fileInfo))
name = QtCore.QFileInfo(filename).fileName()
return icon, name
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
frm = GetIconDemo()
frm.show()
sys.exit(app.exec_())
复制代码
运行效果如下:
(, 下载次数: 0)
上传
点击文件名下载附件
(, 下载次数: 2)
上传
点击文件名下载附件
转载于:
https://my.oschina.net/lython/blog/120468
欢迎光临 纳速健身 (https://nasue.com/)
Powered by Discuz! X3.4