R
Vision QLabel ensures that the text or image is displayed.Qt Provides four classes for the processing of the image data:
QImage, QPixmap, QBitmap и QPicture♪ ♪ ♪ ♪QPixmap developed and optimized to display images on the screen.Method setPixmap() Imaging.Set up your image here. self.image = 'cat.jpg'Start the app and press the LCM, the SCM, and look what's going on.import sys
from PyQt5.Qt import *
class QLabelClickable(QLabel):
clicked = pyqtSignal()
def init(self, parent=None):
super(QLabelClickable, self).init(parent)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.clicked.emit()
class MainWindow(QMainWindow):
def init(self):
super().init()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.createActions()
self.createMenus()
self.createContextMenu()
self.labelImage = QLabelClickable(self)
self.labelImage.setStyleSheet(
"QLabel {background-color: write; border: 1px solid "
"#0DFFD7; border-radius: 5px;}")
self.labelImage.setAlignment(Qt.AlignCenter)
self.labelImage.clicked.connect(self.imageClicked)
layout = QVBoxLayout(self.centralWidget)
layout.addWidget(self.labelImage)
self.image = 'cat.jpg'
self.openImage(self.image)
def createActions(self):
self.openAct = QAction(QIcon("img/open.png"), "&Open...", self, shortcut="Ctrl+O",
triggered=self.open)
self.exitAct = QAction(QIcon("img/exit.png"), "E&xit", self, shortcut="Ctrl+Q",
triggered=self.close)
def createMenus(self):
fileMenu = QMenu("&File", self)
fileMenu.addAction(self.openAct)
fileMenu.addSeparator()
fileMenu.addAction(self.exitAct)
self.menuBar().addMenu(fileMenu)
def createContextMenu(self):
self.centralWidget.setContextMenuPolicy(Qt.ActionsContextMenu)
self.centralWidget.addAction(self.openAct)
self.centralWidget.addAction(self.exitAct)
def openImage(self, image):
pixmapImage = QPixmap(image)
pixmapImage = pixmapImage.scaled(
300, 300,
Qt.KeepAspectRatio,
Qt.SmoothTransformation
)
self.labelImage.setPixmap(pixmapImage)
def open(self):
fileName, _ = QFileDialog.getOpenFileName(
self,
"Open Image",
QDir.currentPath(),
"Image Files (*.png *.jpg *.bmp)"
)
if fileName:
self.image = fileName
self.openImage(fileName)
def imageClicked(self):
self.window = QLabel()
self.window.setPixmap(QPixmap(self.image))
self.window.show()
self.window.activateWindow()
if name == 'main':
app = QApplication(sys.argv)
window = MainWindow()
window.resize(300, 300)
window.show()
sys.exit(app.exec_())
Update♪ ♪ ♪ The problem is I can't add buttons and a transparent window, etc. I'm sorry if I didn't quite understand my thoughts.If it doesn't take long, can you tie my code and yours in separate classes?import sys
from PyQt5.Qt import *
from PyQt5 import QtCore, QtWidgets
class UiMainWindow(object):
def setup_ui(self, main_windows):
main_windows.setObjectName("MainWindow")
main_windows.resize(800, 600)
main_windows.setWindowOpacity(0.5)
self.centralwidget = QtWidgets.QWidget(main_windows) #(MainWindow)
self.centralwidget.setObjectName("centralwidget")
"""
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(200, 200, 50, 50))
self.pushButton.setStyleSheet('''
QPushButton {
background-color: #636363;
border-bottom-left-radius: 20;
background-image: url(ball.png) no-repeat;
}
QPushButton:hover {background-color: #969696;}
QPushButton:pressed {background-color: #c7c7c7;}
''')
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(sys.exit)
self.pushButton.clicked.connect(MainWindow.close)
"""
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(49, 49, 700, 500))
self.widget.setObjectName("widget")
main_windows.setCentralWidget(self.centralwidget) #MainWindow
self.retranslate_ui(main_windows)
QtCore.QMetaObject.connectSlotsByName(main_windows)
@staticmethod
def retranslate_ui(self, main_windows):
_translate = QtCore.QCoreApplication.translate
main_windows.setWindowTitle( "MainWindow")
class QLabelClickable(QLabel):
clicked = pyqtSignal()
def init(self, parent=None):
super(QLabelClickable, self).init(parent)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.clicked.emit()
#class MainWindow(QMainWindow):
class MainWindow(QMainWindow, UiMainWindow): # +++ UiMainWindow
def init(self):
super().init()
self.setup_ui(self) # +++
self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget)
self.createActions()
self.createMenus()
self.createContextMenu()
self.labelImage = QLabelClickable(self)
self.labelImage.setStyleSheet(
"QLabel {background-color: write; border: 1px solid "
"#0DFFD7; border-radius: 5px;}")
self.labelImage.setAlignment(Qt.AlignCenter)
self.labelImage.clicked.connect(self.imageClicked)
botton = QPushButton('Это кнопка')
botton.clicked.connect(lambda: print('Это кнопка !!!!!!!!!!!!'))
layout = QVBoxLayout(self.centralwidget)
layout.addWidget(self.labelImage)
layout.addWidget(botton)
self.image = 'cat.jpg'
self.openImage(self.image)
def createActions(self):
self.openAct = QAction(QIcon("img/open.png"), "&Open...", self, shortcut="Ctrl+O",
triggered=self.open)
self.exitAct = QAction(QIcon("img/exit.png"), "E&xit", self, shortcut="Ctrl+Q",
triggered=self.close)
def createMenus(self):
fileMenu = QMenu("&File", self)
fileMenu.addAction(self.openAct)
fileMenu.addSeparator()
fileMenu.addAction(self.exitAct)
self.menuBar().addMenu(fileMenu)
def createContextMenu(self):
self.centralwidget.setContextMenuPolicy(Qt.ActionsContextMenu)
self.centralwidget.addAction(self.openAct)
self.centralwidget.addAction(self.exitAct)
def openImage(self, image):
pixmapImage = QPixmap(image)
pixmapImage = pixmapImage.scaled(
300, 300,
Qt.KeepAspectRatio,
Qt.SmoothTransformation
)
self.labelImage.setPixmap(pixmapImage)
def open(self):
fileName, _ = QFileDialog.getOpenFileName(
self,
"Open Image",
QDir.currentPath(),
"Image Files (*.png *.jpg *.bmp)"
)
if fileName:
self.image = fileName
self.openImage(fileName)
def imageClicked(self):
self.window = QLabel()
self.window.setPixmap(QPixmap(self.image))
self.window.show()
self.window.activateWindow()
if name == 'main':
app = QApplication(sys.argv)
window = MainWindow()
window.resize(300, 300)
window.show()
sys.exit(app.exec_())