Python and Pycharm with PyQt5 Library: Showing up a picture - python

i want to build app with pyqt5 in python but i got some error while running it i tried to solved it but i cant because im newbie to this
i want to show the picture there are 1 frame, 1 button while pressing button on gui that i made from designer pyqt5-tools the picture shown up on frame.
here is my code and where is my fault, can you fix it?
import sys
import cv2
from PyQt5 import QtCore,QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QImage,QPixmap
from PyQt5.QtWidgets import QDialog,QApplication,QMainWindow,QLabel
from uic import loadUi
class ShowImage (QMainWindow):
def __init__(self):
super(ShowImage,self).__init__()
loadUi('GUI.ui',self)
self.image=None
self.loadButton.clicked.connect(self.loadClicked)
#pyqtSlot()
def loadClicked(self):
self.loadImage('smarthealth.jpg')
def loadImage(self,img="smarthealth.jpg"):
self.image=cv2.imread(img)
self.displayImage()
def displayImage(self):
qformat=QImage.Format_Indexed8
if len(self.image.shape)== 3:
if (self.image.shape[2])== 4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img=QImage(self.image,self.image.shape[1],self.image.shape[0],
self.image.strides[0],qformat)
img = img.rgbSwapped()
self.imgLabel.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt
.AlignVCenter)
app=QtWidgets.QApplication(sys.argv)
window= ShowImage()
window.setWindowTitle('Show Image GUI')
window.show()
sys.exit(app.exec())

Related

To turn back from 2nd window to 1st window (different files)

An application is being created with a large number of different windows.
The bottom line is that I am trying to get from the main window (by clicking button) to the client window. There is "Back" button in the clients window, which should return the user to the main window.
Both codes are in different files. The problem occurs at the stage of pressing the "Customers" button (Process finished with exit code -1073740791 (0xC0000409)).
Moreover, if everything is in one file, then everything works fine. But only here the program is not planned to be small, so I don't want to put a huge code in one file.
Welcome_Screen.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication
import Customers
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('screens/welcomescreen.ui', self)
self.CustomerData.clicked.connect(self.go_to_CustomerData)
def go_to_CustomerData(self):
client = Customers.CustomerScreen()
widget.addWidget(client)
widget.setCurrentIndex(widget.currentIndex() + 1)
# main
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('Icons/MainIcon.png'))
app.setApplicationDisplayName('Bonus Program')
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedWidth(600)
widget.setFixedHeight(475)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Customers.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QMessageBox, QTableWidget, QTableWidgetItem, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtGui import QIcon
import Welcome_Screen as ws
import sqlite3
db_path = 'Bonus_db.sqlite'
class CustomerScreen(QDialog):
def __init__(self):
super(CustomerScreen, self).__init__()
loadUi("screens/customerscreen.ui", self)
self.back_btn.setIcon(QIcon('Icons/Back.png'))
self.back_btn.setIconSize(QtCore.QSize(45, 60))
self.back_btn.clicked.connect(self.go_Back_to_WelcomeScreen)
# self.SoldTos.clicked.connect(self.gotoSoldTos)
def go_Back_to_WelcomeScreen(self):
welcome = ws.WelcomeScreen()
ws.widget.addWidget(welcome)
ws.widget.setCurrentIndex(ws.widget.currentIndex() + 1)
Customer_app = QApplication(sys.argv)
cust_window = CustomerScreen()
cust_window.show()
sys.exit(Customer_app.exec_())

How to show a button in a pop up message in Python and how to interact?

I'd like to make a button in a pop up message. I use PyQt5 module and a message in pop up. But when I make a button in the message, my pop up message is in for
import sys
import io
import folium # pip install folium
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView # pip install PyQtWebEngine
import numpy as np
import pandas as pd
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
****some codes***
for c in range (len(flight_df)):
tooltip1 =str(flight_df['x'][c])+"*** "+str(flight_df['y'][c])+"*** "+str(flight_df['name'][c])
folium.Marker([flight_df['x'][c],flight_df['y'][c]], popup=flight_df['x'],tooltip=tooltip1).add_to(m)
data = io.BytesIO()
m.save(data, close_file=False)
webView = QWebEngineView()
webView.setHtml(data.getvalue().decode())
layout.addWidget(webView)
****some other codes****
My code is above. Can you help me how can I add a follow button in pop up screen and how can I see if button work
PyQt comes with a message box dialog built into it for these purposes.
The following code worked for me
import sys
from PyQt5 import Qt
from PyQt5.Qt import QMessageBox
a = Qt.QApplication(sys.argv)
window = Qt.QWidget()
window.resize(320, 240)
window.setWindowTitle("Hello World!")
result = QMessageBox.question(window, 'TITLE', 'MESSAGE', QMessageBox.Yes | QMessageBox.No)
window.show()
sys.exit(a.exec_())

code stops at initialising self.cam = QCamera()

I am using python3.5, pyqt5 and qt5. My problem may be simple but since i am getting no error and never experienced this problem i am writing this here. Here is my code:
import sys
from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject, QCameraViewfinderSettings, QCameraImageCapture
from PyQt5.QtMultimediaWidgets import QCameraViewfinder
class Camera(QObject):
def __init__(self, parent = QObject()):
super(Camera, self).__init__(parent)
print("TROLOLOL3")
self.cam = QCamera()
self.caminfo = QCameraInfo(self.cam)
self.camvfind = QCameraViewfinder()
self.camvfindset = QCameraViewfinderSettings()
self.cammode = self.cam.CaptureMode(2)
self.camimgcap = QCameraImageCapture(self.cam)
if __name__ == '__main__':
print("TROLOLOL1")
app = QtWidgets.QApplication(sys.argv)
print("TROLOLOL2")
cam = Camera()
print("TROLOLOL4")
sys.exit(app.exec_())`
I deleted some irrelevant code for the overview. I tested when the code stops and at self.cam = QCamera() the code just stops, doesnt exit or shows a error. Why does this happen? This code compiled without error on Raspbian but now i am on Ubuntu Mate with Raspberry Pi 3.
If you have questions feel free to ask

Python 3.6 | PyQt5 | QLabel doesn't display QPixmap

I'm trying to display a PNG image in a Widget using QLabel. I built the Widget using Qt Designer and the label shows the image properly. When I load the .ui into mi .py and execute it, everything shows up well, except for the label. I tried loading the QPixmap manually on the python code, but it didn't work either.
from PyQt5.QtWidgets import (QApplication, QLabel)
from PyQt5.QtGui import (QPixmap)
from PyQt5 import uic
import sys
import os
form = uic.loadUiType('gui/Files/Login.ui')
class LoginWindow(form[0], form[1]):
def __init__(self):
super().__init__()
self.setupUi(self)
self.logoLabel.setPixmap(QPixmap(os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo.png'))))
self.logoLabel.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LoginWindow()
win.show()
app.exec_()
Any idea what am I doing wrong? Any sugestions on how could I display the image other than with QLabel?

Python3 and Qt Designer - Display an image in main window

I'm still new with python and Qt designer. I would like to develop an application where user need to press the 'pushbutton' in order to view the image(from local directory path) on GUI mainwindow.This will keep repeating until complete number of cycle.
Did tried using QLabel and QGraphiscView widget but doesn't work.(probably due to lack of knowledge). Could anyone help me to solve it?
Below is the code:
#!/usr/bin/env python
from __future__ import division
import sys
from PyQt4 import QtCore, QtGui, uic
gui_file = 'image.ui'
Ui_MainWindow, QtBaseClass = uic.loadUiType(gui_file)
class showImage(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.get_image_button.clicked.connect(self.getImage)
def getImage(self):
image_path='c:/Users/mohd_faizal4/Desktop/Python/Image/image1.jpg' #path to image file
self.label_image.QPixmap(image_path)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myWindow = showImage()
myWindow.show()
sys.exit()
app.exec_()
Really appreciate!

Categories

Resources