How to create classes within classes? - python

I want to clearly be able to see that my class LoginScrollbar is a class (also a tkinter Frame) composing of elements belonging to the LoginFrame and not for example the MainWindow which is launched after logging in as it is very unclear which classes are simpley frameworks to display elements under which Frame (main class). What is the best way to achieve this goal?
I don't know what to try.
This is my code I currently have, as you can see it's very unclear that MainWindow and MainWindowScrollbar are related. It's hard to tell that MainWindowScrollbar is a grouping that contains elements for MainWindow. It appears that LoginFrame,LoginScrollbar,Mainwindow,MainwindowScrollbar are all classes in the same classification.
class LoginFrame:
def __init__(self):
#somecode
class LoginScrollbar:
def __init__(self):
#somecode
class MainWindow:
def __init__(self):
#somecode
class MainWindowScrollbar:
def __init__(self):
#somecode
This is what I want, is this somehow possible without creating a seperate file for each frame of my program?
class LoginFrame:
def __init__(self):
#somecode
class LoginScrollbar:
def __init__(self):
#somecode
class LeftSideOfLogin:
def __init__(self):
#WidgetsForLeftSideOfLogin
class MainWindow:
def __init__(self):
#somecode
class MainWindowScrollbar:
def __init__(self):
#somecode
class MainWindowRightSide:
def __init__(self):
#WidgetsForRightSideOfMainWindow

Related

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

Inheriting a class from another file

Suppose I have backend.py file and inside is:
class Database():
def __init__(self):
pass
def printDatabase(self):
print('Printing test')
And suppose I have another fie called frontend.py
import backend
class Tester(backend.Database):
def __init__(self):
pass
def testInheritance(self):
self.printDatabase()
Is this the correct code to inherit a class from another file, and use one of its methods?

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.

python 2.7 - how to call parent class constructor

I have base class like below
class FileUtil:
def __init__(self):
self.outFileDir = os.path.join(settings.MEDIA_ROOT,'processed')
if not os.path.exists(outFileDir):
os.makedirs(outFileDir)
## other methods of the class
and I am extending this class as below:
class Myfile(FileUtil):
def __init__(self, extension):
super(Myfile, self).__init__()
self.extension = 'text'
## other methods of class
But i am getting below error?
super(Myfile, self).__init__()
TypeError: super() takes at least 1 argument (0 given)
I gone through many documents and found that there are different sytex of calling super() in 2.x and 3.x. I tried both ways but getting error.
You have 2 options
old style class, you should call the super constructor directly.
class FileUtil():
def __init__(self):
pass
class Myfile(FileUtil):
def __init__(self, extension):
FileUtil.__init__(self)
new style class, inherit from object in your base class and your current call to super will be processed correctly.
class FileUtil(object):
def __init__(self):
pass
class Myfile(FileUtil):
def __init__(self, extension):
super(Myfile, self).__init__()
You may need to create the FileUtil class using the super() function as well:
class FileUtil(object):
def __init__(self):
super(FileUtil, self).__init__()
...

how to access derived class methods from another derived class

I'm using wxpython to generate a GUI. The structure of the program I'm doing is shown below. I have a class for each section of the GUI (class1 and class2). I'm using the Panel class to create these sections. class1 and class2 are derived from another class (the Group class). I want to access the methods of a derived class from the other derived class on the fly. So when I'm in classA.method1() I want to call classB.method3(). what is the best way to do that?
class Panel(wx.Panel):
def __init__(self, parent):
class1 = ClassA()
class2 = ClassB()
class Group(wx.Panel):
def __init__(self, parent, name):
.
.
.
class ClassA(Group):
def method1(self):
....
def method2(self):
....
class ClassB(Group):
def method3(self):
....
def method4(self):
....
I's assuming you want to call the class method, not the method of an instance of that class.
From ClassA.method1(), ClassB be should be a global, so all you have to do is:
class ClassA(Group):
def method1(self):
classB.method3(someclass)
Now all you have to do is figure out what you want to put in for 'someclass'. If method3 never uses 'self' then just pass it None. If ClassA is compatible, then you can pass it 'self'. Otherwise, you need to pass it an instance of ClassB.

Categories

Resources