相关文章推荐
飞奔的铁板烧  ·  thymeleaf ...·  2 周前    · 
耍酷的柳树  ·  SQLite 触发器 | 菜鸟教程·  2 天前    · 
冷冷的枕头  ·  error C2871: “cv”: ...·  11 月前    · 
果断的水龙头  ·  在Android ...·  1 年前    · 
粗眉毛的松鼠  ·  java - spring code ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using PyQt5 and Qt-Designer to design an application.

How do I instantiate a class for each page on QstackedWidget. I can do it in a single class, all widgets belong to the same QMainWindow. But, the issue is that the file will get too long and impracticale. How do I assign a class for each page. For example, class I handles all the widgets on Page I and class II handles all the widgets on Page II ; in the QMainWindow file I can just assign an Object that represents each page.

How can I do it?

Your question is unclear. You don't "assign a class" to a page, at the very least you will add a class instance (generally, a QWidget) for each page. Do you want to have a more "modular" approach, using separated classes (and possibly separated files) that will then be used for the pages? Also, "the issue is that the file will get too long and impracticale": you're not trying to modify the file generated by pyuic, are you? musicamante Jan 29, 2021 at 18:44 @musicamante I am not modifying the pyuic files, that is not a good approach. "Do you want to have a more "modular" approach, using separated classes (and possibly separated files) that will then be used for the pages?" Yes that is exactly what I want to do. The Main Window Logic file is getting larger, it is annoying to read and debug. What I want to do is: put each page of the StackedWidget in a file, so I can easily read and modify. Assign is how I would Idescribe instance when my vocabulary is limited. Grabinuo Jan 29, 2021 at 18:58
from PyQt5.QtWidgets import QWidget
class Widget1(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

widget2.py

from PyQt5.QtWidgets import QWidget
class Widget2(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

main.py

from widget1 import Widget1
from widget2 import Widget2
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setTitle("Stackked widget demo")
        self.stacked = QStackedWidget(self)
        self.setCentralWidget(self.stacked)
        self.widget1 = Widget1()
        self.stacked.addWidget(self.widget1)
        self.widget2 = Widget2()
        self.stacked.addWidget(self.widget2)
if __name__ == "__main__":
    app = QApplication([])
    mainwin = MainWindow()
    mainwin.show()
    app.exec_()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.