Issue
I am trying to create a PyQt5 GUI, where main window has 3 tabs inside. I made it before using 2 different classes: first for creating main window, and second for tabs and everything inside them. Now i am trying to fit everything in one class since i need to make instance of a main class. But i have problem with making a layout with those tabs. It shows error: QLayout: Cannot add a null widget to QVBoxLayout.
Here is the code:
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Window'
self.left = 0
self.top = 0
self.width = 620
self.height = 700
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
tabw = self.createtabs()
self.layout = QVBoxLayout()
self.layout.addWidget(tabw)
self.show()
def createtabs(self):
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(300, 200)
self.tabs.addTab(self.tab1, "tab1")
self.tabs.addTab(self.tab2, "tab2")
self.tabs.addTab(self.tab3, "tab3")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
EDIT: I used modification from comments, but also wanted to change class from QWidget to QMainWindow, and i faced another problem. I don't know how to set properly the widget of main window. Since I made those changes it gives me errors: QLayout: Attempting to add QLayout "" to App "", which already has a layout
QWidget::setLayout: Attempting to set QLayout "" on App "", which already has a layout
QLayout: Cannot add a null widget to QVBoxLayout
And here is the code:
class App(QMainWindow):
def __init__(self):
super().__init__()
self.main_widget = QWidget(self)
self.setCentralWidget(self.main_widget)
self.setWindowTitle("Window")
self.left = 0
self.top = 0
self.width = 620
self.height = 700
self.setGeometry(self.left, self.top, self.width, self.height)
tabw = self.createtabs()
self.layout = QVBoxLayout()
self.layout.addWidget(tabw)
self.show()
def createtabs(self):
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.addTab(self.tab1, "tab1")
self.tabs.addTab(self.tab2, "tab2")
self.tabs.addTab(self.tab3, "tab3")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Solution
First of all your method createtabs doesn't return anything to your tabw. Second of all you need to setLayout to yuor central widget.
Here is the code which works as you want
class App(QMainWindow):
def __init__(self):
super().__init__()
self.main_widget = QWidget(self)
self.setCentralWidget(self.main_widget)
self.setWindowTitle("Window")
self.left = 0
self.top = 0
self.width = 620
self.height = 700
self.setGeometry(self.left, self.top, self.width, self.height)
tabw = self.createtabs()
self.layout = QVBoxLayout()
self.layout.addWidget(tabw)
self.main_widget.setLayout(self.layout)
self.show()
def createtabs(self):
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.addTab(self.tab1, "tab1")
self.tabs.addTab(self.tab2, "tab2")
self.tabs.addTab(self.tab3, "tab3")
return self.tabs
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Answered By - Golgi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.