Issue
Using my First script, I create a layout as I need. Now I want to use this layout in the second script and add widgets to the frame. Face the following problem: Both the First and second program windows will open simultaneously, I want to open/ show my second program window only. How to resolve it?
First script
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QFrame, QHBoxLayout, QSizePolicy
class MsgBox111(QWidget):
def __init__(self):
super().__init__()
self.frame_main = QFrame()
self.frame_main.setObjectName("ob_frame_main")
self.frame_main.setStyleSheet("QFrame#ob_frame_main{background-color:green}")
self.frame_left_top = QFrame()
self.frame_left_top.setObjectName("ob_frame_left_top")
self.frame_left_top.setStyleSheet("QFrame#ob_frame_left_top{background-color:skyblue}")
self.frame_left_top.setMinimumSize(600, 340)
self.frame_left_bot = QFrame()
self.frame_left_bot.setObjectName("ob_frame_left_bot")
self.frame_left_bot.setStyleSheet("QFrame#ob_frame_left_bot{background-color:lightgreen}")
self.frame_left_bot.setFixedSize(600, 60)
self.frame_right = QFrame()
self.frame_right.setObjectName("ob_frame_right")
self.frame_right.setStyleSheet("QFrame#ob_frame_right{background-color:rgb(230,220,150)}")
self.frame_right.setFixedSize(200, 405)
self.lay_main = QHBoxLayout()
self.lay_main.setContentsMargins(0, 0, 0, 0)
self.lay_main.setSpacing(8)
self.lay_left = QVBoxLayout()
self.lay_left.setSpacing(8)
self.lay_left_top = QVBoxLayout()
self.lay_left_top.setContentsMargins(0, 0, 0, 0)
self.lay_left_bot = QHBoxLayout()
self.lay_left_bot.setContentsMargins(0, 0, 0, 0)
self.lay_right = QVBoxLayout()
self.lay_right.setContentsMargins(0, 0, 0, 0)
self.lay_overall = QHBoxLayout()
self.lay_overall.setContentsMargins(0, 0, 0, 0)
self.lay_overall.setSpacing(5)
self.lay_left_top = QVBoxLayout(self.frame_left_top)
self.lay_left_bot = QHBoxLayout(self.frame_left_bot)
self.lay_right = QVBoxLayout(self.frame_right)
self.lay_main = QHBoxLayout(self.frame_main)
self.lay_left.addWidget(self.frame_left_top)
self.lay_left.addWidget(self.frame_left_bot)
self.lay_main.addLayout(self.lay_left)
self.lay_main.addWidget(self.frame_right)
self.lay_overall.addWidget(self.frame_main)
self.setLayout(self.lay_overall)
frame_right_sizehint = self.lay_left.sizeHint()
self.frame_right.setFixedSize(260, frame_right_sizehint.height())
self.frame_main.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
Second script
import sys
from PyQt5.QtWidgets import QLineEdit
from layour_sample_001 import *
class MsgBox222(QWidget):
def __init__(self):
super().__init__()
self.ss = MsgBox111()
self.ss.show()
self.tb = QLineEdit()
self.ss.lay_left.addWidget(self.tb)
self.ss.setLayout(self.ss.lay_left)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MsgBox222()
mainwindow.show()
sys.exit(app.exec_())
Solution
You need to properly subclass.
After calling the __init__
for the superclass, you can access all inherited attributes of that class and instance: you have a fully constructed MsgBox111
instance that you can then extend according to your needs.
class MsgBox222(MsgBox111):
def __init__(self):
super().__init__()
self.tb = QLineEdit()
self.lay_left.addWidget(self.tb)
I strongly suggest you to do some more research and careful studying on the main aspects of OOP, including classes, instances, inheritance, attributes and subclassing, as knowledge of these fundamental topics is mandatory.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.