Nov-05-2020, 03:44 PM
Hi
I try to have several layouts displayed in a single window. I don't know the best way to do so, maybe should I remove all widgets of the current display and create the widgets of the new one. As I don't know how to do that, I decided to set the central widget of a main window to layouts defined as objets. To try this method, I coded the following script.
Arbiel
I try to have several layouts displayed in a single window. I don't know the best way to do so, maybe should I remove all widgets of the current display and create the widgets of the new one. As I don't know how to do that, I decided to set the central widget of a main window to layouts defined as objets. To try this method, I coded the following script.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
source='/home/remi/Documents/programmation/python_env/essai/racine.py'
#
# Copyright 2020 Rémi Mathieu <remi@remi-Vostro-3550>
#
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QHBoxLayout, QVBoxLayout, QPushButton
from functools import partial
class Racine(QMainWindow):
def __init__(self):
super(Racine,self).__init__()
def init(self, label, localRange):
self.setObjectName=label
self.setWindowTitle(label)
self.setGeometry(localRange[0],localRange[1],localRange[2],localRange[3])
self.a=A()
self.a.init('A',self)
self.b=B()
self.b.init('B',self)
class A(QtWidgets.QWidget):
def __init__(self):
super(A,self).__init__()
self.lesboutons=[('A','a', True), ('B', 'b', False), ('Act_B', 'act_b', True)]
zs=QHBoxLayout(self)
for libellé, signal, actif in self.lesboutons:
btn=qpb(self, libellé, signal, actif, zs, False)
btn.clicked.connect(partial(self.execut, signal))
def init(self,label,appli):
self.appli=appli
self.label=label
def act(self):
auto=self.appli.a
self.appli.setWindowTitle(auto.label)
self.appli.setCentralWidget(auto)
def execut(self, code):
print ('A ' + code)
if code=='act_b':
self.findChild(QtWidgets.QPushButton,code).setEnabled(True)
self.findChild(QtWidgets.QPushButton,'a').setEnabled(False)
self.appli.b.act()
class B(QtWidgets.QWidget):
def __init__(self):
super(B,self).__init__()
self.lesboutons=[('A','a', False), ('B', 'b', True), ('Act_A', 'act_a', True)]
zs=QHBoxLayout(self)
for libellé, signal, actif in self.lesboutons:
btn=qpb(self, libellé, signal, actif, zs, False)
btn.clicked.connect(partial(self.execut, signal))
def init(self,label,appli):
self.appli=appli
self.label=label
def act(self):
auto=self.appli.b
self.appli.setWindowTitle(auto.label)
self.appli.setCentralWidget(auto)
# self.appli.show()
def execut(self, code):
print ('B ' + code)
if code=='act_a':
self.findChild(QtWidgets.QPushButton,code).setEnabled(True)
self.findChild(QtWidgets.QPushButton,'a').setEnabled(False)
self.appli.a.act()
def qpb(self, title, name, active, qlay, stretch=True):
btn=QPushButton(title)
btn.setObjectName(name)
btn.setEnabled(active)
qlay.addWidget(btn)
if stretch:
lay.addStretch()
return btn
def main(args):
app = QtWidgets.QApplication(args)
application=Racine()
application.init("Racine", [250,200,800,300])
application.setCentralWidget(application.a)
application.show()
app.exec()
return 0
if __name__ == '__main__':
import sys
main(sys.argv)
sys.exit()When I click on "Act_B" button in the A layout, the B layout is display. But, when I click on the "Act_A" of the B layout, I get the following error :Error:Traceback (most recent call last):
File "/home/remi/Documents/programmation/python_env/essai/racine.py", line 68, in execut
self.appli.a.act()
File "/home/remi/Documents/programmation/python_env/essai/racine.py", line 39, in act
self.appli.setCentralWidget(auto)
RuntimeError: wrapped C/C++ object of type A has been deleted
Abandon (core dumped)Thank you for giving me a help.Arbiel
