Qpixmap image not loading in pyqt4 4.9 python - python

-EDITED
Im using this code to load a picture in a qlabel found in zetcode. Its working in later version of pyqt4, But not in pyqt4 4.9. Is there a change in using qpixmap in pyqt4 4.9? Thanks.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
pixmap = QtGui.QPixmap("redrock.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Red Rock')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

I am using the same version as you and for this example to work you script must be in the same directory as redrock.png

Related

adding an Icon next to title doesn't work on Mac

I would like to add A LOGO next to Icon but it doesn't seem to work on Mac , any suggestions for [this]
code :
import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('icon-cutout.png'))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'icon-cutout.png')
app.setWindowIcon(QIcon(path))
ex = Example()
sys.exit(app.exec_())
it only appears in [here]

How to change UI in same window using PyQt5?

I'm just getting started with PyQt5. I have been trying to accomplish a seemingly very simple task but haven't been able to get enough info about it. After a fair bit of googling I have been able to get one window to close and another to launch with the other UI loaded but that's not what I want to do here.
I want to switch the UI in the same window. I am loading the UI files as global variables in my python file where I have 2 classes for each UI. When I click a particular button in one UI, I want to switch to the other UI in the same window. Below is a sample of the code:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from PyQt5.uic import loadUiType
import os
about_company_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'about_company.ui'))
intern_placement_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'intern_placement.ui'))
class InternPlacement(QMainWindow, intern_placement_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.intern_pushButton.clicked.connect(self.change)
def change(self):
self.about_company = AboutCompany()
self.about_company.show()
self.close()
class AboutCompany(QMainWindow, about_company_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = InternPlacement()
window.show()
app.exec_()
You have to use a QStackedWidget
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
ui_folder = os.path.join("frontend", "ui")
about_company_ui, _ = uic.loadUiType(os.path.join(ui_folder, "about_company.ui"))
intern_placement_ui, _ = uic.loadUiType(os.path.join(ui_folder, "intern_placement.ui"))
class InternPlacement(QtWidgets.QMainWindow, intern_placement_ui):
def __init__(self, parent=None):
super(InternPlacement, self).__init__(parent)
self.setupUi(self)
class AboutCompany(QtWidgets.QMainWindow, about_company_ui):
def __init__(self, parent=None):
super(AboutCompany, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
intern_window = InternPlacement()
about_window = AboutCompany()
w = QtWidgets.QStackedWidget()
w.addWidget(intern_window)
w.addWidget(about_window)
intern_window.intern_pushButton.clicked.connect(lambda: w.setCurrentIndex(1))
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to disable a QPushButton

I tried calling UI from another file, but couldn't disable a button. I don't know where to place .setEnabled(False). I placed it everywhere except main, but the button is still enabled.
import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow
class machine(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(machine, self).__init__()
self.setupUi(self)
self.initUI()
self.disablebtn()
self.btn_Save.setEnabled(False);
self.btn_Close.setEnabled(False);
self.show()
def initUI(self):
self.desktopSize()
self.statusbar().showMessage("super dooper")
self.btn_Save.setEnabled(False);
self.btn_Close.setEnabled(False);
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Your machine class does disable the buttons correctly, but you never create an instance of it, and so it never gets a chance to work properly.
The code should probably look more like this:
import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow
class machine(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(machine, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.statusBar().showMessage("super dooper")
self.btn_Save.setEnabled(False)
self.btn_Close.setEnabled(False)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = machine()
MainWindow.show()
sys.exit(app.exec_())

QLabel doesn't display pixmap

I'm trying to display a QPixmap in QLabel, but I get the label empty.
Here is my code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Test(QtGui.QWidget):
def __init__(self):
super(Test, self).__init__()
self.initUI()
def initUI(self):
pixmap = QtGui.QPixmap("test.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
self.move(300, 200)
self.setWindowTitle('Test')
self.show()
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
When I start the application, I only get an empty window :
I use a Linux distribution (Manjaro). I tested with Ubuntu, but I had the same problem.
I don't understand what is wrong, since I succeed to display other QPixmaps in QGraphicsScene and QIcon in QPushButton.
How can I display a QPixmap in my label ?
EDIT:
Here is an other version with a layout, but which don't solve the problem:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Test(QtGui.QWidget):
def __init__(self):
super(Test, self).__init__()
self.initUI()
def initUI(self):
layout = QtGui.QVBoxLayout()
pixmap = QtGui.QPixmap("test.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
layout.addWidget(lbl)
self.setLayout(layout)
self.move(300, 200)
self.setWindowTitle('test')
self.show()
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
The trouble not is the path
The trouble is when execute the program from VSC
If you execute the program in CMD or TERMINAL the program RUN and Show the Image !
Tested !

Put an Image on a QPushButton

I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.
Example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(24,24))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Here is the same example from #NorthCat but for PyQt5:
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(200,200))
layout = QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Assuming pyqt supports gif pictures, this should work
icon = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)
QPushButton
Push buttons display a textual label, and optionally a small icon.
These can be set using the constructors and changed later using
setText() and setIcon(). If the button is disabled, the appearance of
the text and icon will be manipulated with respect to the GUI style to
make the button look "disabled".

Categories

Resources