Python Forum
PyQt5 adding image - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: PyQt5 adding image (/thread-32601.html)



PyQt5 adding image - thewolf - Feb-20-2021

I'm trying to add an image to my login window here and I'm not sure sure how to go about adding it on line 30. addWidget of course doesn't work but not exactly sure what method to use.

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QLineEdit, QPushButton, QStatusBar, QLabel,
                             QVBoxLayout)
from PyQt5.QtGui import QPixmap


class loginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(365, 250)
        self.setWindowTitle("Log into Inventory Manager")

        self.windowForm = loginWidget()
        self.setCentralWidget(self.windowForm)


class loginWidget(QWidget):
    def __init__(self):
        super().__init__()

        logoLabel = QLabel(self)
        pixmap = QPixmap('logo.jpg')
        logoLabel.setPixmap(pixmap)

        self.userTexBox = QLineEdit()
        self.passTextBox = QLineEdit()
        self.loginButton = QPushButton("LOGIN")

        layout = QVBoxLayout(self)
        layout.
        layout.addWidget(self.userTexBox)
        layout.addWidget(self.passTextBox)
        layout.addWidget(self.loginButton)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = loginWindow()
    window.show()
    sys.exit(app.exec_())



RE: PyQt5 adding image - thewolf - Feb-20-2021

Never mind. Of course as soon as I post the thread I go back to the code and figure it out instantly.

layout.addWidget(logoLabel)

I was adding self and didn't need to.



(Feb-20-2021, 03:53 PM)thewolf Wrote: I'm trying to add an image to my login window here and I'm not sure sure how to go about adding it on line 30. addWidget of course doesn't work but not exactly sure what method to use.

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QLineEdit, QPushButton, QStatusBar, QLabel,
                             QVBoxLayout)
from PyQt5.QtGui import QPixmap


class loginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(365, 250)
        self.setWindowTitle("Log into Inventory Manager")

        self.windowForm = loginWidget()
        self.setCentralWidget(self.windowForm)


class loginWidget(QWidget):
    def __init__(self):
        super().__init__()

        logoLabel = QLabel(self)
        pixmap = QPixmap('logo.jpg')
        logoLabel.setPixmap(pixmap)

        self.userTexBox = QLineEdit()
        self.passTextBox = QLineEdit()
        self.loginButton = QPushButton("LOGIN")

        layout = QVBoxLayout(self)
        layout.
        layout.addWidget(self.userTexBox)
        layout.addWidget(self.passTextBox)
        layout.addWidget(self.loginButton)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = loginWindow()
    window.show()
    sys.exit(app.exec_())



RE: PyQt5 adding image - Axel_Erfurt - Feb-20-2021

adjust height and width to your needs

        logoLabel = QLabel(self)
        logoLabel.setFixedHeight(32)
        logoLabel.setFixedWidth(32)
        logoLabel.setScaledContents(True)
        pixmap = QPixmap('logo.png')
        logoLabel.setPixmap(pixmap)



RE: PyQt5 adding image - thewolf - Feb-21-2021

(Feb-20-2021, 05:26 PM)Axel_Erfurt Wrote: adjust height and width to your needs

        logoLabel = QLabel(self)
        logoLabel.setFixedHeight(32)
        logoLabel.setFixedWidth(32)
        logoLabel.setScaledContents(True)
        pixmap = QPixmap('logo.png')
        logoLabel.setPixmap(pixmap)


Thanks Axel I appreciate it!