【发布时间】:2019-05-28 08:00:06
【问题描述】:
在这里,我想将 Pixmap 和文本添加到 QLabel,通过使用 listdir,我得到了我的文件名,但文件名正在循环中合并,所以任何人都可以帮助我如何将我的文件名放在 QLabel 中。请帮助我,谢谢提前。
import os
from PyQt4 import QtCore, QtGui
class MyLabel(QtGui.QLabel):
def __init__(self):
super(MyLabel, self).__init__()
def paintEvent(self, event):
super(MyLabel, self).paintEvent(event)
pos = QtCore.QPoint(100, 100)
painter = QtGui.QPainter(self)
s = iter(os.listdir("/home/cioc/Documents/pos/images"))
# file1 = next(s)
painter.drawText(pos,"vegitables")#here in place of vegitables i want to print the my filenames
painter.setPen(QtGui.QColor(168, 34, 3))
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
url = '/home/cioc/Documents/pos/images'
highlight_dir = url
self.scroll_area = QtGui.QScrollArea()
self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.setCentralWidget(self.scrollArea)
content_widget = QtGui.QWidget()
self.scrollArea.setWidget(content_widget)
self.layout = QtGui.QGridLayout(content_widget)
self.files_it = iter([os.path.join(highlight_dir, file) for file in os.listdir(highlight_dir)])
try:
for row in range(3):
for column in range(3):
file = next(self.files_it)
pixmap = QtGui.QPixmap(file)
self.label = MyLabel()
self.label.setPixmap(pixmap)
self.layout.addWidget(self.label,row,column)
except StopIteration:
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.setGeometry(500,300,800,400)
w.show()
sys.exit(app.exec_())
【问题讨论】:
-
您希望图像在文本中吗?还是您希望图像位于文本下方?
-
好的,我猜你希望我在图像的中心。我是正确的?另一方面,您是只想要 9 张图片还是希望所有图片都显示在 3 列中?
-
你想让列的数量根据窗口的宽度变化吗?
-
你已经把我弄糊涂了,你想要3行多少列?
-
从文本中我很清楚,现在让我们使用数字,假设有 40 张图像。应该有多少行和列?
标签: python python-2.7 pyqt pyqt4