Merge two pieces of code
-
Hello,
How do I insert this code:
import sys from PySide2.QtCore import QRect from PySide2.QtCore import QSize from PySide2.QtWidgets import QWidget from PySide2.QtWidgets import QRubberBand from PySide2.QtWidgets import QApplication class Widget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.rubberBand = None self.origin = None def mousePressEvent(self, event): self.origin = event.pos() if not self.rubberBand: self.rubberBand = QRubberBand(QRubberBand.Rectangle, self) self.rubberBand.setGeometry(QRect(self.origin, QSize())) self.rubberBand.show() def mouseMoveEvent(self, event): self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized()) def mouseReleaseEvent(self, event): self.rubberBand.hide() # determine selection, for example using QRect.intersects() # and QRect.contains(). if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec_())
inside this code:
so that when I insert an image to be able to select a part of the image with a red rectangle. I tried inserting the code under this class without any success:
class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
Basically I get this error:
Traceback (most recent call last): File "code.py", line 653, in <module> window = OrthoView() File "code.py", line 279, in __init__ self.plotCanvas = MyMplCanvas(self) File "code.py", line 109, in __init__ super().__init__(parent) File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 21, in __init__ super().__init__(figure=figure) File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 214, in __init__ super().__init__(figure=figure) File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1722, in __init__ figure.set_canvas(self) AttributeError: 'OrthoView' object has no attribute 'set_canvas'
Any idea/help?
-
@john_hobbyist
I don't know what you mean or intend by "insert ... inside this code".Your
Widget(QWidget)
declares a standalone, new class namedWidget
which derives fromQWidget
and overrides some methods. But yourOrthoView
derives fromQMainWindow
, and has nothing to do with thatWidget
class.Depending on what you are trying to achieve, maybe you want to move the methods you have overridden in
Widget
so that they are methods in yourOrthoView
? -
This post is deleted!
-
@JonB: Ok, I will try it!! Thank you...
UPDATE: I did what you suggest. I got this error (which I am trying to solve):
Traceback (most recent call last): File "code.py", line 505, in <module> widget = Widget() NameError: name 'Widget' is not defined
it indicates the error in the line
widget = Widget()
from here:
if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec_())
-
@john_hobbyist
hi
But what did you do ?
Did you move the
def mousePressEvent(self, event):
def mousePressEvent(self, event):
def mouseReleaseEvent(self, event):to OrthoView class ?
-
@mrjj Yes
And this:
class OrthoView(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.rubberBand = None self.origin = None . . .
and this:
. . . if __name__ == "__main__": app = QApplication(sys.argv) icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico')) app.setWindowIcon(icon) window = OrthoView() window.show() widget = Widget() widget.show() sys.exit(app.exec_())
-
Hi
Then you dont need the lines
widget = Widget()
widget.show()as that was the "old" class and now all code that it had is moved to OrthoView
so those lines are not needed anymore. -
Ok, the application runs, but when I try to choose area on the image with the mouse, I do not see any rectangle from rubberband...
-
@john_hobbyist
Hi
Yes. but now you are in training of moving the code around :)I think the reason that it don't work is that the OrthoView is not the one that stuff is drawn on.
I think it might be the Canvas class.
Which means that canvas class might cover the MainWindow (OrthoView)
and hence it gets all the mouse events.
But you knows best. What class actually display the image ? -
@john_hobbyist
Hi
I drew on top of a picture i found.
i dont have python so i cant run it.Anyway, what class draws the image ?
You must have teh mouse function in that class. since its not OrthoView
it seems. it must be another class.I tried to read the code from
https://github.com/Axel-Erfurt/OrthoViewLite/blob/main/OrthoViewLite.pybut it seems to set "Widget" as central widget so Im not sure that code is
current. Did it used to be that class MyMplCanvas ? -
@mrjj: I do not know.
When I put the rubberband methonds on class
MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
I get the errors I posted on first post.
When I put the code on class:
class OrthoView(QMainWindow):
I do not get any errors but the execution never reach the rubbeband methods. I identified it with "prints" throughout the code.
-
Hi
its odd.
what is mpl_qt.FigureCanvasQTAgg ?is that a Widget class ?
I cant find the source of it.
-
@mrjj I do not know. However, the initial code (where the one we talk about was based) is here:
https://github.com/kklmn/OrthoView/blob/master/OrthoView.py -
Hi,
The method override must be added to the widget that you click your mouse on. That is not the main window.
As for your original error, you copied the name of the class but not its implementation hence the error. QMainWindow does indeed not have a set_canvas method.
-
About what ?
-
Both on
MyMplCanvas(mpl_qt.FigureCanvasQTAgg)
and
OrthoView(QMainWindow)
the methods do not run.
So where else should I put the methods?"QMainWindow does indeed not have a set_canvas method." ---> What should I do in this case? I am based on ready code, I cannot build a set_canvas method
Thanks!
-
Please show the exact code you have currently.
-
# -*- coding: utf-8 -*- """ OrthoViewLite.py based on https://github.com/kklmn/OrthoView by Konstantin Klementiev modified by Axel Schneider (added File Selector) https://github.com/Axel-Erfurt """ __author__ = "Konstantin Klementiev" __versioninfo__ = (1, 0, 2) __version__ = '.'.join(map(str, __versioninfo__)) __date__ = "8 Feb 2020" __license__ = "MIT license" import os print("1") import sys print("2") import cv2 print("3") from matplotlib.figure import Figure print("4") import numpy as np print("5") from PyQt5.QtGui import QIcon print("6") from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget,QAction, QFileDialog, QMenu, QToolBar, QHBoxLayout, QTreeView, QFileSystemModel, QSizePolicy, QMessageBox) print("7") from PyQt5.QtCore import Qt, QDir, QStandardPaths, QFileInfo print("8") import matplotlib.backends.backend_qt5agg as mpl_qt print("9") ######################################################################### import rubberband import sys """ from PySide2.QtCore import QRect from PySide2.QtCore import QSize from PySide2.QtWidgets import QWidget from PySide2.QtWidgets import QRubberBand from PySide2.QtWidgets import QApplication import PySide2 """ from PyQt5 import QtWidgets from PyQt5 import QtCore from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtCore import * ########################################################################### try: from ConfigParser import ConfigParser except ImportError: from configparser import ConfigParser print("10") selfDir = os.path.dirname(__file__) print("11") iniApp = (os.path.join(selfDir, 'OrthoViewList.ini')) print("12") config = ConfigParser( dict(pos='[0, 0]', corners='[None]*4', scalex=0, scaley=0)) print("13") config.add_section('window') print("14") config.read(iniApp) print("15") def write_config(): print("16") with open(iniApp, 'w+') as cf: print("17") config.write(cf) print("18") class MyToolBar(mpl_qt.NavigationToolbar2QT): def set_message(self, s): print("19") try: print("20") sstr = s.split() print("21") while len(sstr) > 5: print("22") del sstr[0] print("23") x, y = float(sstr[0][2:]), float(sstr[1][2:]) print("24") s = f'x = {x:.2f}\ny = {y:.2f}' print("25") except Exception: print("26") pass print("27") if self.coordinates: print("28") self.locLabel.setText(s) print("29") class MyMplCanvas(mpl_qt.FigureCanvasQTAgg): def __init__(self, parent=None): print("30") self.fig = Figure() print("31") self.fig.patch.set_facecolor('None') print("32") super(MyMplCanvas, self).__init__(self.fig) print("33") self.setParent(parent) print("34") self.updateGeometry() print("35") self.setupPlot() print("36") self.mpl_connect('button_press_event', self.onPress) print("37") self.img = None print("38") self.setContextMenuPolicy(Qt.CustomContextMenu) print("39") self.mouseClickPos = None print("40") self.menu = QMenu() print("41") def setupPlot(self): print("42") rect = [0., 0., 1., 1.] print("43") self.axes = self.fig.add_axes(rect) print("44") self.axes.axis("off") print("45") def imshow(self, img): print("46") if self.img is None: print("47") self.img = self.axes.imshow(img) print("48") else: print("49") prev = self.img.get_array() print("50") self.img.set_data(img) print("51") if prev.shape != img.shape: print("52") self.img.set_extent( [-0.5, img.shape[1]-0.5, img.shape[0]-0.5, -0.5]) print("53") self.axes.set_xlim((0, img.shape[1])) print("54") self.axes.set_ylim((img.shape[0], 0)) print("55") self.toolbar.update() print("56") self.draw() print("57") def onPress(self, event): print("58") if (event.xdata is None) or (event.ydata is None): print("59") self.mouseClickPos = None print("60") return print("61") self.mouseClickPos = int(round(event.xdata)), int(round(event.ydata)) print("62") class OrthoView(QMainWindow): def __init__(self, parent=None): #################################### super().__init__(parent) self.rubberBand = None self.origin = None #################################### print("63") super(OrthoView, self).__init__(parent) print("64") self.setWindowTitle('OrthoViewLite') print("65") self.setStyleSheet("QMainWindow {background: #e9e9e9;} QHBoxLayout \ {background: #e9e9e9;} QTreeView {background: #e9e9e9;} \ FigureCanvasQTAgg {background: #e9e9e9;} QToolBar {border: 0px;}") print("66") self.setMinimumSize(500, 350) print("67") self.image_file_path = "" print("68") if config.has_option('window', 'win_left') and config.has_option('window', 'win_top') \ and config.has_option('window', 'win_width') and config.has_option('window', 'win_height'): left = int(config.get('window', 'win_left')) top = int(config.get('window', 'win_top')) width = int(config.get('window', 'win_width')) height = int(config.get('window', 'win_height')) print("69") self.setGeometry (left, top, width, height) print("70") print("71") self.home_path = QStandardPaths.standardLocations(QStandardPaths.PicturesLocation)[0] print("72") self.tb = QToolBar("File") print("73") empty = QWidget() print("74") empty.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) print("75") self.tb.addWidget(empty) print("76") open_btn = QAction("Open Image File", self, triggered=self.openFile) print("77") open_btn.setIcon(QIcon.fromTheme("document-open")) print("78") self.tb.addAction(open_btn) print("79") go_up_btn = QAction("one level up", self, triggered=self.oneUp) print("80") go_up_btn.setIcon(QIcon.fromTheme("go-up")) print("81") self.tb.addAction(go_up_btn) print("82") go_home_btn = QAction("go to home", self, triggered=self.goHome) print("83") go_home_btn.setIcon(QIcon.fromTheme("go-home")) print("84") self.tb.addAction(go_home_btn) print("85") stretch = QWidget() print("86") stretch.setFixedWidth(200) print("87") self.tb.addWidget(stretch) print("88") self.plotCanvas = MyMplCanvas(self) print("89") self.plotCanvas.setSizePolicy( QSizePolicy.Expanding, QSizePolicy.Expanding) print("90") self.toolbar = MyToolBar(self.plotCanvas, self) print("91") for action in self.toolbar.findChildren(QAction): if action.text() in ['Customize', 'Subplots']: action.setVisible(False) print("92") self.toolbar.locLabel.setAlignment(Qt.AlignRight) print("93") self.toolbar.locLabel.setFixedWidth(200) print("94") self.tb.setContextMenuPolicy(Qt.PreventContextMenu) print("95") self.tb.setMovable(False) print("96") self.tb.setAllowedAreas(Qt.TopToolBarArea) print("97") self.toolbar.setContextMenuPolicy(Qt.PreventContextMenu) print("98") self.toolbar.setMovable(False) print("99") self.toolbar.setAllowedAreas(Qt.TopToolBarArea) print("100") self.addToolBar(self.toolbar) print("101") self.addToolBar(self.tb) print("102") mylayout = QHBoxLayout() print("103") mylayout.addWidget(self.plotCanvas) print("104") self.mylistwidget = QTreeView() print("105") self.mylistwidget.setFixedWidth(300) print("106") mylayout.addWidget(self.mylistwidget) print("107") self.fileModel = QFileSystemModel() print("108") self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files | QDir.AllDirs) print("109") self.fileModel.setRootPath(QDir.homePath()) print("110") self.mylistwidget.setModel(self.fileModel) print("111") self.mylistwidget.setRootIndex(self.fileModel.index(self.home_path)) print("112") #self.mylistwidget.setRootIndex(self.fileModel.index("/")) print("113") self.mylistwidget.selectionModel().selectionChanged.connect(self.on_clicked) print("114") self.mylistwidget.clicked.connect(self.tree_clicked) print("115") col = [1, 2, 3, 4] print("116") for c in col: print("117") self.mylistwidget.hideColumn(c) print("118") mywidget = QWidget() print("119") mywidget.setLayout(mylayout) print("120") self.setCentralWidget(mywidget) print("121") self.myfilter = ["tif", "tiff", "png", "jpg"] print("122") self.treefilter = ["*.tif", "*.tiff", "*.png", "*.jpg"] print("123") self.fileModel.setNameFilters(self.treefilter) print("124") self.fileModel.setNameFilterDisables(False) print("125") self.mylistwidget.setFocus() print("126") self.mylistwidget.resizeColumnToContents(1) print("127") def goHome(self): print("128") self.mylistwidget.setRootIndex(self.fileModel.index(self.home_path)) print("129") def oneUp(self): print("130") self.mylistwidget.setRootIndex(self.mylistwidget.rootIndex().parent()) print("131") def on_clicked(self): print("132") path = self.fileModel.fileInfo(self.mylistwidget.currentIndex()).absoluteFilePath() print("133") if QDir.exists(QDir(path)): print("134") print(path, "is folder") print("135") else: print("136") if QFileInfo(path).suffix() in self.myfilter: print("137") if not os.stat(path).st_size == 0: print("138") print(path) print("139") self.image_file_path = path print("140") self.updateFrame() print("141") else: print("142") print("file not exists or has size 0") print("143") self.msgbox("File is empty (size 0)") print("144") def tree_clicked(self): print("145") index = self.mylistwidget.currentIndex() print("146") if not self.mylistwidget.isExpanded(index): print("147") self.mylistwidget.setExpanded(index, True) print("148") self.mylistwidget.resizeColumnToContents(0) print("149") else: print("150") self.mylistwidget.setExpanded(index, False) print("151") self.mylistwidget.resizeColumnToContents(0) print("152") def openFile(self): print("153") print("open File Dialog") print("154") path, _ = QFileDialog.getOpenFileName(self, "Open File", self.image_file_path,"Image Files (*.tif *.tiff *.png *.jpg)") print("155") if path: print("156") if os.path.isfile(path) and not os.stat(path).st_size == 0: print("157") self.image_file_path = path print("158") print("file exists:",self.image_file_path) print("159") self.updateFrame() print("160") else: print("161") print("file not exists or has size 0") print("162") self.msgbox("File is empty (size 0)") print("163") def getFrame(self, path): print("170") if os.path.isfile(path): print("171") frame = cv2.imread(path, 1) print("172") if not np.shape(frame) == (): print("173") self.img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) print("174") else: print("175") print("Error!") print("176") frame = cv2.imread(path, cv2.IMREAD_UNCHANGED) print("177") self.img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) print("178") else: print("179") self.openFile() print("180") def updateFrame(self): print("181") self.getFrame(self.image_file_path) print("182") overlay = self.img.copy() print("183") alpha = 0.75 # transparency factor print("184") imageNew = cv2.addWeighted(overlay, alpha, self.img, 1-alpha, 0) print("185") self.plotCanvas.imshow(imageNew) print("186") def closeEvent(self, e): print("187") config.set('window', 'win_left', str(self.geometry().x())) print("188") config.set('window', 'win_top', str(self.geometry().y())) print("189") config.set('window', 'win_width', str(self.geometry().width())) print("190") config.set('window', 'win_height', str(self.geometry().height())) print("191") with open(iniApp, 'w+') as cf: print("192") config.write(cf) print("193") def msgbox(self, message): print("194") msg = QMessageBox(2, "Error", message, QMessageBox.Ok) print("195") msg.exec() print("196") #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ def mousePressEvent(self, event): print("1000") self.origin = event.pos() print("1001") if not self.rubberBand: print("1002") self.rubberBand = QRubberBand(QRubberBand.Rectangle, self) print("1003") self.rubberBand.setGeometry(QRect(self.origin, QSize())) print("1004") self.rubberBand.show() print("1005") def mouseMoveEvent(self, event): print("1006") self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized()) print("1007") def mouseReleaseEvent(self, event): print("1008") self.rubberBand.hide() print("1009") # determine selection, for example using QRect.intersects() # and QRect.contains(). #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if __name__ == "__main__": print("197") app = QApplication(sys.argv) print("198") icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico')) print("199") app.setWindowIcon(icon) print("200") window = OrthoView() print("201") window.show() print("202") #widget = Widget() #widget.show() sys.exit(app.exec_())