Python - opencv - pyqt5 - python

When I try to import cv2 when using pyqt5 I have this error :
ImportError: /lib64/libQt5Widgets.so.5: symbol _ZN23QPlatformSystemTrayIcon16staticMetaObjectE, version Qt_5 not defined in file libQt5Gui.so.5 with link time reference
If I only import cv2 or pyqt5 there are no errors, but together it doesn't work.
What can i do to resolve my problem ?
import sys
import os
import cv2
from PyQt5.QtWidgets import QApplication
import mainWindow
class Application:
def __init__(self):
self.mainWindow = mainWindow.MainWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
application = Application()
sys.exit(app.exec_())

Related

Python and Pycharm with PyQt5 Library: Showing up a picture

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())

PyQt with Flask dont show the MainWindow

i'm trayin to create a gui that run a flask application using this example https://github.com/onur2677/PyQt-Flask
in this exemple they use pyqt 4
i'm trying to use pyqt 5 so i made some changes
but the mainwindow open and close in the same time
this is my code for the gui
import sys
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import psycopg2
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import *
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
PORT = 5000
ROOT_URL = 'http://localhost:{}'.format(PORT)
class FlaskThread(QThread):
def __init__(self, application):
QThread.__init__(self)
self.application = application
def __del__(self):
self.wait()
def run(self):
self.application.run(port=PORT)
def createGuiFor(application):
qtapp = QApplication(sys.argv)
webapp = FlaskThread(application)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
webview = QWebEngineView()
webview.load(QUrl(ROOT_URL))
webview.show()
webview.setWindowTitle("MyApp")
webview.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
return qtapp.exec_()
if __name__ == '__main__':
from MyWebApp import app
app = QApplication(sys.argv)
sys.exit(app.exec_())
sys.exit(createGuiFor(app))
and this is the flask application that will run in the gui
from flask import Flask ,render_template
app = Flask(__name__)
#app.route("/")
def hello():
return render_template("index.html")
if __name__ == "__main__":
app.run()
for any one ho want to use flask and pyqt there is an example
here https://github.com/smoqadam/PyFladesk
that work for me perfectly
EDIT:
Here is the same code with PySide6
https://github.com/eliaweiss/PySide6Fladesk.git

PyQt5 issue with QApplication module

I have an issue where like many others i can't run some pyqt5 code because i get the error that vscode can't find the module QApplication when i run the command from pyqt5.QtWidgets import QApplication.
Here is my code:
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
ui = uic.loadUi("interface.ui")
ui.show()
def recherche():
if ui.NomProduit.text()!="":
produit = str(ui.NomProduit.text())
else:
ui.NomProduit.setText("Veuillez rentrez le nom d'un produit")
ui.Recherche.clicked.connect(recherche)
I edited from PyQt5 import Uic to from PyQt5.uic import loadUi and I used Opject Oriented Programing (OOP) . And I imported all methods From PyQt5.QtWidgets import *
Try this :
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
class Main(QMainWindow):
def __init__(self):
super().__init__()
loadUi("interface.ui", self)
self.show()
self.Buttons()
def Buttons(self):
self.Recherche.clicked.connect(self.recherche)
def recherche(self):
if self.NomProduit.text()!="":
produit = str(self.NomProduit.text())
else:
self.NomProduit.setText("Veuillez rentrez le nom d'un produit")
ui = Main()
app.exec_()

How to run a external process on a PyQt5 OSX app, without crashing the app?

I'm currently building an app that uses OCR to extract data from PDF files using PYQT5, and I need to use the terminal to convert the pdf into jpeg's before running the OCR; however, when I run this subprocess after compiling it crashes, but it works perfectly if I don't compile the program.
It seems to me that the problem lies within the subprocess call. I've tried many different ways, but I don't know why my app keeps crashing after I compile and run the conversion.
NOTE: For PDF to JPEG I'm using ImageMagick
Currently using to compile:
py2applet --make-setup main.py
python3 setup.py py2app
Converter.py:
from subprocess import call
from PIL import Image
import pytesseract
from PyQt5 import QtWidgets
from ocrgui import Ui_MainWindow
class ImageConverter(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.show()
self.sbmt_btn_convert.clicked.connect(self.convert_image)
def convert_image(self): #Converts image into jpeg
text = str(self.input_convert.text())
output = str(self.output_convert.text())
convert = 'converted.jpeg'
call('convert -density 300 ' + text + ' ' + convert, shell=True)
im = Image.open(convert)
text = pytesseract.image_to_string(im, lang = 'eng')
file = open(output,'w')
file.write(text)
file.close()
call('rm converted.jpeg', shell=True)
print('Succesfully created')
Main.py:
import sys
from PyQt5.QtWidgets import QApplication
from calculator import CalculatorWindow
app = QApplication(sys.argv)
calculator = CalculatorWindow()
sys.exit(app.exec())
I think that problem in the next module:
pathPython\Lib\site-packages\PIL\ImageQt.py
Try to put # on the next lines:
...
try:
from PyQt5.QtGui import QImage, qRgba, QPixmap
from PyQt5.QtCore import QBuffer, QIODevice
qt_version = '5'
except (ImportError, RuntimeError):
print("except (ImportError, RuntimeError):!!!!!!!!!!!!")
#try:
# from PyQt4.QtGui import QImage, qRgba, QPixmap
# from PyQt4.QtCore import QBuffer, QIODevice
# qt_version = '4'
#except (ImportError, RuntimeError):
# try:
# from PySide.QtGui import QImage, qRgba, QPixmap
# from PySide.QtCore import QBuffer, QIODevice
# qt_version = 'side'
# except ImportError:
# qt_is_installed = False
...

Error on trying to convert a python file into an executable with cx_freeze

I've got code with PyQt:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
def main():
page = raw_input('Escriu una web: ')
app = QApplication(sys.argv)
view = QWebView()
view.settings().setAttribute(QWebSettings.JavascriptEnabled, True)
view.load(QUrl(page))
view.setWindowTitle('Titanicus 0.1')
view.show()
app.exec_()
if __name__ == '__main__':
main()
And my setup.py file is this:
import sys
from cx_Freeze import setup, Executable
setup(
name = "On Dijkstra's Algorithm",
version = "3.1",
description = "A Dijkstra's Algorithm help tool.",
executables = [Executable("nautilus.py")])
I've the folder build but when I try to open th new exe file it returns me that error:
File "ExtensionLoader_PyQt4_QtGui.py", line 11, in <module>
ImportError: No module named atexit
Help please!
I'd try adding options = {"build_exe" : {"includes" : "atexit" }} to setup.py,
ref. https://bitbucket.org/reclosedev/cx_freeze/src/f3cacc2fd45a/samples/PyQt4/setup.py
Try something like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.setWindowTitle('Titanicus 0.1')
self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
page = raw_input('Escriu una web: ')
self.load(QtCore.QUrl(page))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
main = myWindow()
main.show()
sys.exit(app.exec_())

Categories

Resources