Calling function in parent class and accessing parent self - python

In my main window I set a variable self.print_this. I then call another class PhotoViewer and then in that class I call a function from my main window. In that function I try to print the self.print_this but I get the following error: AttributeError: PhotoViewer object has no attribute print_this
How do I access the self of the window class or avoid sending the self of PhotoViewer to the printfromwindow function?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint, QRect, QSize, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QApplication, QRubberBand, QColorDialog
from PyQt5.QtGui import QPixmap, QPainter, QPen
import sys
class PhotoViewer(QtWidgets.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
rectChanged = pyqtSignal(QRect)
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
Window.printfromwindow(self)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.print_this='test'
PhotoViewer(self)
def printfromwindow(self):
print(self.print_this)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 800, 600)
window.show()
sys.exit(app.exec_())

Untested, but perhaps something like this. Note that in your code, you're calling methods of the Window and PhotoViewer classes but not constructing an instance of those classes.
You need to instantiate and retain those instances within their parent/child class instances, if you want to be able to reference their methods/properties.
As noted in my comment above, Window.printfromwindow(self) should not have the self argument (and should instead refer to <instance of Window>.printfromwindow().
In the Window constructor, I assign self.viewer an instance of the PhotoViewer class and pass self (which is a Window instance) as the parent argument to its constructor.
Then, in PhotoViewer class constructor, we do self.window = parent which should allow you to call self.window.printfromwindow():
class PhotoViewer(QtWidgets.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
rectChanged = pyqtSignal(QRect)
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self.window = parent # relates the "parent" Window instance to this "child" PhotoViewer instance
self.window.printfromwindow() # calls the printfromwindow method from the "parent" Window instance
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.print_this='test'
self.viewer = PhotoViewer(self) # creates an instance of PhotoViewer class as an attribute of this Window instance
def printfromwindow(self):
print(self.print_this)
If you want to make this more readily available (i.e., not just from within the constructor of PhotoViewer) then assign the Window.printfromwindow to an attribute of the PhotoViewer, like:
class PhotoViewer(QtWidgets.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
rectChanged = pyqtSignal(QRect)
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self.window = parent # relates the "parent" Window instance to this "child" PhotoViewer instance
self.printfromwindow = self.window.printfromwindow
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.print_this='test'
self.viewer = PhotoViewer(self) # creates an instance of PhotoViewer class as an attribute of this Window instance
def printfromwindow(self):
print(self.print_this)
Since functions are first-level objects in python, you can do this:
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.viewer.printfromwindow()

To access a parent class you need to pass the parent class through the function. def printfromwindow(Window). Then you inherit all of the attributes from the parent class. You can also modify a subclass with super().__init__ to add changes to the sub classes without effecting the parent class

Related

Accessing object attributes in pyqt window in separate module

I have a main window created in one python module. This module also contains a class definition used to create an object called my_settings.
I also have a separate module which creates a another window (which should be in a different module for various reasons). This window allows various inputs such as opening a file and storing some settings once buttons are clicked blaa blaa.
What I want to do is then amend the attributes of my_setttings. A simplified version of the code is:
MAIN MODULE
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
uic.loadUi(self.some_directory + "arc_custom_main.ui", self)
self.some_button.clicked.connect(self.open_some_widget)
def open_some_widget(self):
widget = widget_in_other_module(self)
widget_in_other_module.exec_()
app = QApplication(sys.argv)
my_settings=settings()
_mainWindow = MainWindow()
_mainWindow.show()
SEPERATE MODULE
class widget_in_other_module(QDialog):
def __init__(self, parent):
my_settings.temppath = my_settings.OutputDir
QDialog.__init__(self)
self.parent = parent
my_settings.some_attribute= foo
uic.loadUi("some.ui", self)
self.pushButtonOpenMain.clicked.connect(self.openMain)
def openMain(self):
my_settings.some_other_attibute=bar
The problem I have is that I can't find a way to be able to access my_settings in the openMain method of widget_in_other_module. I'm a bit of a newbe to qt and can't for the life of me work out where I should pass my_settings. For various reasons I need all the functionality of widget_in_other_module to be outside of the main module (mostly to do with readability and future planed changes to the main module). I've tried as much as I can think of e.g. including it as a parameter in the line self.pushButtonOpenMain.clicked.connect(self.openMain,my_settings) but this doesn't seem to be allowed. Am I missing something basic here?
Instantiate settings variable in separate module and import this varable where you need it. It could be object of a class or just dictionary.
storage.py
class Settings:
def __init__(self):
print("Settings")
my_settings = Settings()
MainWindow.py
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
from Widget import Widget
from storage import my_settings
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
self.open_some_widget()
def open_some_widget(self):
widget = Widget()
widget.show()
widget.test()
self._widget = widget
my_settings.foo = 10
app = QApplication(sys.argv)
_mainWindow = MainWindow()
_mainWindow.show()
app.exec_()
Widget.py
from PyQt5.QtWidgets import QWidget
from storage import my_settings
class Widget(QWidget):
def test(self):
print(my_settings.foo)

Metaclass conflict when trying to create a Python abstract class that also subclasses a PySide6 class

I'm trying to create an abstract base class that also inherits an arbitrary PySide6 class. However, the following produces the error TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases.
from abc import ABC, abstractmethod
from PySide6.QtWidgets import QApplication, QWidget
class MyBaseWidget(QWidget, ABC):
def __init__(self, parent=None):
super().__init__(parent=parent)
#abstractmethod
def foo(self):
pass
class MyConcreteWidget(MyBaseWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
app = QApplication([])
widget = MyConcreteWidget()
widget.show()
app.exec_()
I tried to resolve this using the solution seen below (inspiration came from Resolving metaclass conflicts, http://www.phyast.pitt.edu/~micheles/python/metatype.html, Multiple inheritance metaclass conflict, etc.).
class MyMeta(ABCMeta, type(QWidget)): pass
class MyBaseWidget(QWidget, metaclass=MyMeta):
def __init__(self, parent=None):
super().__init__(parent=parent)
#abstractmethod
def foo(self):
pass
class MyConcreteWidget(MyBaseWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
app = QApplication([])
widget = MyConcreteWidget()
widget.show()
app.exec_()
This executes without error, but I was expecting an error like TypeError: Can't instantiate abstract class MyConcreteWidget with abstract methods foo when instantiating MyConcreteWidget. Not being able to enforce the base class's interface really takes away the benefit of having an abstract base class. Any solutions?

Class for Each QStackedWidget Page

I am using PyQt5 and Qt-Designer to design an application.
How do I instantiate a class for each page on QstackedWidget. I can do it in a single class, all widgets belong to the same QMainWindow. But, the issue is that the file will get too long and impracticale. How do I assign a class for each page. For example, class I handles all the widgets on Page I and class II handles all the widgets on Page II; in the QMainWindow file I can just assign an Object that represents each page.
How can I do it?
Just create multiple modules:
widget1.py
from PyQt5.QtWidgets import QWidget
class Widget1(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
widget2.py
from PyQt5.QtWidgets import QWidget
class Widget2(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
main.py
from widget1 import Widget1
from widget2 import Widget2
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setTitle("Stackked widget demo")
self.stacked = QStackedWidget(self)
self.setCentralWidget(self.stacked)
self.widget1 = Widget1()
self.stacked.addWidget(self.widget1)
self.widget2 = Widget2()
self.stacked.addWidget(self.widget2)
if __name__ == "__main__":
app = QApplication([])
mainwin = MainWindow()
mainwin.show()
app.exec_()

Reuse PyQt window design across multiple instances

I have designed a window in QT designer and then converted it to Python code. Then I created class A and Class B which inherit from PYQT code. Then I create the third class call it C and inherit from class A and B.
from QTfile import *
from PyQt5.QtWidgets import *
class A(QMainWindow):
def __init__(self):
super().__init__()
self.A1= Ui_MainWindow()
self.A1.setupUi(self)
class B(QMainWindow):
def __init__(self):
super().__init__()
self.B1= Ui_MainWindow()
self.B1.setupUi(self)
Now the problem is when I want to use super().__init__() function, I only can initiate one of them.
class C( A,B):
def __init__(self):
super().__init__()
If I use A.__init__() and B.__init__(), it works again only for one classes
class C( A,B):
def __init__(self):
A.__init__()
B.__init__()
Now the question is how can I initiate the multiple classes?
If you want to reuse the design in multiple window instances, you can try the following:
class BaseWindow(Ui_MainWindow, QMainWindow):
def __init__(self):
Ui_MainWindow.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
class A(BaseWindow):
def __init__(self):
super().__init__()
class B(BaseWindow):
def __init__(self):
super().__init__()

what is the best way to embed program logic that seats in some algorithm class into pyqt4 user interface

class SomNetwork(object):
def __init__(self, dataset):
# some parameters that are not important here
pass
def one_step_learn(self, k, sigma_0, gamma_0, alfa, mcolor,population_of_ids):
pass
def learn(self):
pass
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_QSom()
self.ui.setupUi(self)
self.frame = MyFrame(self)
self.setCentralWidget(self.frame)
self.frame.start()
self.show()
class MyFrame(QtGui.QFrame):
simulationSpeed = 5000
def __init__(self, parent):
super(Ramka, self).__init__(parent)
self.init_Board()
def init_Board(self):
self.timer = QtCore.QBasicTimer()
I would like to be able to interact with SomNetwork class in order to be able to call its methods from within inside of the MyFrame class methods. Are there any special design patterns that would suit this example the most or should I just add instance of the class as a field to the MyFrame class.
Thanks of your help in advance!
I don't know if your question is heading towards this, but for your network you could try to subclass a QtCore.QObject instead. On the whole you should instance your network as a member of your QMainWindow, so your frame and the net can interact via signals and slots.
... By the way, there is a little flaw in your code, when you call the constructor of a QWidget in the subclass of a QMainWindow.

Categories

Resources