Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in
read-only mode
.
Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).
i created a GUI using QtDesigner. I have been trying to import it using
loadUi
But i see this error:
ImportError: No module named 'histogram'
But my .py file and .ui file are on the same directory. How does it happen?
And this is my full code:
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class Histogram(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.canvas = FigureCanvas(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.sumbu1 = self.canvas.figure.add_subplot(111)
self.canvas.figure.set_facecolor("xkcd:wheat")
self.setLayout(vertical_layout)
# main_histogram.py
import cv2
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
from PyQt5.QtWidgets import QDialog, QFileDialog
from PyQt5.QtGui import QIcon, QPixmap, QImage
import numpy as np
fname = ""
class Display_Histogram(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("histogram.ui", self)
self.setWindowTitle("Image Histogram")
self.pbImage.clicked.connect(self.display_histogram)
self.addToolBar(NavigationToolbar(self.widgetDisplay.canvas, self))
def display_histogram(self):
global fname
fname = QFileDialog.getOpenFileName(self, 'Open file',
'd:\\', "Image Files (*.jpg *.gif *.bmp *.png *.tiff)")
pixmap = QPixmap(fname[0])
self.labelImage.setPixmap(pixmap)
self.labelImage.setScaledContents(True);
self.widgetDisplay.canvas.sumbu1.clear()
read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
color = ('b', 'g', 'r')
for i, col in enumerate(color):
histr = cv2.calcHist([read_img], [i], None, [256], [0, 256])
self.widgetDisplay.canvas.sumbu1.plot(histr, color=col, linewidth=3.0)
self.widgetDisplay.canvas.sumbu1.set_ylabel('Y', color='blue')
self.widgetDisplay.canvas.sumbu1.set_xlabel('X', color='blue')
self.widgetDisplay.canvas.sumbu1.set_title('Histogram')
self.widgetDisplay.canvas.sumbu1.set_facecolor('xkcd:wheat')
self.widgetDisplay.canvas.sumbu1.grid()
self.widgetDisplay.canvas.draw()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Display_Histogram()
ex.show()
sys.exit(app.exec_())
@hernancrespo89
This is way thrown out there on a hunch: if the file named histogram
shown in the UI is relevant, why is not named histogram.py
?
And please copy & paste the full text (we can't see the top) of the traceback here (not screenshot).
@hernancrespo89
This is way thrown out there on a hunch: if the file named histogram
shown in the UI is relevant, why is not named histogram.py
?
this is my reference, and there is no histogram.py
there.
And please copy & paste the full text (we can't see the top) of the traceback here (not screenshot).
Here is the full traceback:
Traceback (most recent call last):
File "C:/Users/mustafa/Desktop/histogram6/histogram", line 68, in <module>
ex = Display_Histogram()
File "C:/Users/mustafa/Desktop/histogram6/histogram", line 36, in __init__
loadUi("histogram.ui", self)
File "C:\Python35\lib\site-packages\PyQt5\uic\__init__.py", line 226, in loadUi
return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
File "C:\Python35\lib\site-packages\PyQt5\uic\Loader\loader.py", line 72, in loadUi
return self.parse(filename, resource_suffix, basedir)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 1000, in parse
actor(elem)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 807, in createUserInterface
self.traverseWidgetTree(elem)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
handler(self, child)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
self.traverseWidgetTree(elem)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
handler(self, child)
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 253, in createWidget
self.stack.push(self.setupObject(widget_class, parent, elem))
File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 217, in setupObject
obj = self.factory.createQObject(clsname, name, args, is_attribute)
File "C:\Python35\lib\site-packages\PyQt5\uic\objcreator.py", line 106, in createQObject
factory = self.findQObjectType(classname)
File "C:\Python35\lib\site-packages\PyQt5\uic\objcreator.py", line 131, in findQObjectType
w = module.search(classname)
File "C:\Python35\lib\site-packages\PyQt5\uic\Loader\qobjectcreator.py", line 115, in search
module = __import__(mname, {}, {}, (cls,))
ImportError: No module named 'histogram'
@tedburns hi and welcome to devnet,
The example in the blog wrote that the Python file should have the same name as the class. This implies that the file shall have a .py
extension as rightfully noted by @JonB.
In C++, uic will generate the required code if you want to have a full class (you usually want that) and build it. In Python, you have to follow the same logique and generate Python code out of the .ui
file if you want to extend the widget with code.