My attempt is to try to import modules and their depending classes dynamically. Here is my code. File "Sample_tabwidget_base.py" have three classes names "Contact", "Personal" and "Educational". And I need to import all those classes into my file "sample_tabwidget.py" dynamically. Googleing so many items and trying them, But lack of knowledge, I can't succeed.
File : sample_tabwidget.py
import sys
from PyQt5.QtWidgets import *
# from sample_tabwidget_base import *
import importlib
module = importlib.import_module('sample_tabwidget_base')
print(module.__doc__)
# class_ = getattr(module)
# module = __import__("sample_tabwidget_base")
# mod = getattr(module,'Contact')
# print(mod)
class MainPage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QTabWidget Samples")
self.showMaximized()
self.subwindow = None
self.Ui()
self.show()
def Ui(self):
self.tab = QTabWidget()
self.tab.setTabsClosable(True)
self.tab.setTabShape(QTabWidget.Rounded)
self.tab.setTabPosition(QTabWidget.North)
self.tab.setMovable(True)
self.tab.setDocumentMode(True)
self.tab.setAutoFillBackground(True)
self.tab.setStyleSheet(stylesheet())
font = self.tab.font()
font.setPointSize(10)
self.tab.setFont(font)
self.right_layout = QHBoxLayout()
self.main_layout = QHBoxLayout()
self.right_frame = QFrame()
self.right_frame.setObjectName("rightframe")
self.right_frame.setStyleSheet(f"QFrame#rightframe{{background-color: lightgreen;}}")
self.right_frame.setContentsMargins(0,0,0,0)
self.right_frame.setLayout(self.right_layout)
self.right_layout.setContentsMargins(15,0,0,0)
self.right_layout.addWidget(self.tab)
self.main_layout.setContentsMargins(0,0,0,0)
self.main_layout.addWidget(self.right_frame)
self.main_layout.addStretch()
self.contacttab = Contact()
self.personaltab = Personal()
self.educationaltab = Educational()
self.setLayout(self.main_layout)
self.tab.addTab(self.contacttab, "Contacts")
self.tab.addTab(self.personaltab,"Personal")
self.tab.addTab(self.educationaltab,"Educational")
def stylesheet():
return """
QTabBar {background-color: grey;}
QTabWidget::pane {border: 0px solid yellow; top:-1px; background: grey; }
QTabBar::tab:hover{background-color:yellow;}
QTabBar::tab {background-color: grey; color : black;
border-left: 1px solid grey;border-top:1px solid grey;border-right:1px solid grey;
text-align:centre ; font-family:Trebuchet MS; padding : 10px; }
QTabBar::tab:selected {background: lightgrey;color : Black;border-bottom:5px solid red;margin-bottom: -1px;
text-align:centre ; font-family:Trebuchet MS; color:Black; padding : 10px;}
"""
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainPage()
app.setStyle("windows vista")
mainwindow.show()
sys.exit(app.exec_())
File : smaple_tabwidget_base.py
import sys
from PyQt5.QtWidgets import *
class Contact(QWidget):
def __init__(self):
super().__init__()
print("its a contact class")
self.setWindowTitle("Contact Details")
self.layout = QFormLayout()
self.layout.addRow("Name", QLineEdit())
self.layout.addRow("Address", QLineEdit())
self.setLayout(self.layout)
class Personal(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Personal Details")
self.layout = QFormLayout()
self.sex = QHBoxLayout()
self.sex.addWidget(QRadioButton("Male"))
self.sex.addWidget(QRadioButton("Female"))
self.layout.addRow(QLabel("Sex"), self.sex)
self.layout.addRow("Date of Birth", QLineEdit())
self.setLayout(self.layout)
class Educational(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Educational Details")
self.layout = QHBoxLayout()
self.layout.addWidget(QLabel("subjects"))
self.layout.addWidget(QCheckBox("Physics"))
self.layout.addWidget(QCheckBox("Maths"))
self.setLayout(self.layout)