S
S
savzaycev2022-04-17 11:57:39
Python
savzaycev, 2022-04-17 11:57:39

How to display a new widget after clicking on an item from the menu?

Hello, dear users of Habr! I decided to write a simple "drawer", but a problem arose: I can not implement the work of one of the menu items ('Create'). When you click on a menu item, a canvas should appear (object: Pixmap( ) in white).
Thoughts: maybe the problem is that the function, and, accordingly, the variable referring to our Pixmap object immediately "dies" ?

Many thanks in advance to everyone who responded!

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.resize(1200, 700)
        self.setFixedSize(self.width(), self.height())
        self.setWindowTitle('Paint')
        self.setWindowIcon(QIcon('icon.png'))
        self.mainMenu()

    def getNewFileName(self):
        text, ok = QInputDialog.getText(self, 'Создать', 'Введите имя файла:')

        if ok:
            self.le = QLineEdit(self)
            self.le.setText(str(text))
            return 'NewFile' if len(self.le.text()) == 0 else self.le.setText()

    def newFileWindow(self):
        self.setWindowTitle(self.getNewFileName())
        # self.pix = QPixmap(400, 400)
        # self.pix.fill(Qt.White)
        # self.update()


    def mainMenu(self):
        menuBar = self.menuBar()


        fileMenu = menuBar.addMenu('Файл')

        newFileAction = QAction('Создать', self)
        fileMenu.addAction(newFileAction)
        newFileAction.setShortcut('Ctrl+N')
        newFileAction.triggered.connect(self.newFileWindow)


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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-04-17
@Vindicar

I have not worked with PyQT, but I will offer a guess: you created a new QPixmap () object, but how does your window, or rather its parent QMainWindow, know about it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question