1

Я пишу редактор презентации при помощи паттерна MVC.

Вот мой view, main_view.py:

from PyQt5.QtWidgets import QWidget, QMainWindow
from PyQt5 import uic
from model.CommandButton import CommandButton


class MainWindow(QMainWindow):
    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        uic.loadUi('view/firstform.ui', self)
        button_slide_show = CommandButton(self.slide_show_btn, self.controller)
        button_editor = CommandButton(self.editor_btn, self.controller)
        button_add_slide = CommandButton(self.add_slide_btn, self.controller)
        button_remove_slide = CommandButton(self.remove_slide_btn, self.controller)


class EditorWindow(QWidget):
    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        uic.loadUi('view/secondform.ui', self)
        button_add_text_to_slide = CommandButton(self.add_text_btn, self.controller)
        button_add_image_to_slide = CommandButton(self.add_image_btn, self.controller)


class SlideShowWindow(QWidget):
    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        uic.loadUi('view/slideshow.ui', self)
        button_back = CommandButton(self.back_btn, self.controller)
        button_next = CommandButton(self.editor_btn, self.controller)
        button_end_presentation= CommandButton(self.end_presentation_btn, self.controller)

Model, CommandButton.py:

class CommandButton:
    def __init__(self, qbutton, controller):
        self.qbutton = qbutton
        self.controller = controller
        self.qbutton.clicked.connect(lambda x: self.pressed())

    def pressed(self):
        print(self.qbutton.text())
        self.controller.execute(self.qbutton.text())

controller, Controller.py:

class Controller:
    def __init__(self):
        self.current_command = None
    def execute(self, command_name):
        print('executing')
        self.current_command = command_name
        if 'Слайд-шоу' == self.current_command:
            self.slide.show()
        elif 'Редактор' == self.current_command:
            self.editor.show()

Запуск самого приложения:

import sys
from PyQt5.QtWidgets import QApplication
from controller.Controller import Controller
from view.main_view import MainWindow


if __name__ == '__main__':
    app = QApplication(sys.argv)
    controller = Controller()
    main_view = MainWindow(controller)
    main_view.show()
    sys.exit(app.exec_())

В главном меню есть кнопки - слайд-шоу и редактор.

Как в контроллере реализовать их появление при нажатии на эти кнопки, кнопки реализованы при помощи класса CommandButton, связанного непосредственно с контроллером?

3
  • Да, спасибо, забыл отметить
    – smitzz
    7 дек 2022 в 12:46
  • Если их логику сразу в их классах прописывать, то нужно создавать экземпляр класса MainWindow, а тот требует позиционного аргумента - controller
    – smitzz
    7 дек 2022 в 14:12
  • smitzz, пожалуйста, предоставьте минимальный воспроизводимый пример, который демонстрирует проблему и лучше расскажите, что вы хотите сделать, и что не так в вашем коде.
    – S. Nick
    30 дек 2022 в 11:56

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.