K
K
KerroViT2020-08-04 12:38:57
Python
KerroViT, 2020-08-04 12:38:57

How to open a new window by clicking on an item in the menuBar?

Made 2 windows in Qt Designer and converted them to Python classes (main_window and about_window) using pyuic5.
Created a main.py file in the folder with these classes

import sys
from PyQt5 import QtWidgets
import main_window

class Application(QtWidgets.QMainWindow, main_window.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = Application()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

What is the question: how to call the about_window window by clicking on the menuBar item in main_window?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2020-08-04
@KerroViT

In the main_window code, you need to attach the menuBar.clicked() signal to the about_window window display method. The display method is about_window.exec_().
PS: Don't forget that about_window needs to be imported into main_window with the import command.
Approximately it should look like this:

# Код окна main_window
#
import about_window  # Импортируем окно About
...
...
# Конструктор main_window
... 
...
self.menuBar.clicked.connect(self.show_about_window)  # Всякий раз при клике на menuBar выполнять self.show_about_window
...
...
# Тело main_window
def show_about_window(self):  # Выполняется всякий раз при клике на menuBar 
    about = about_window()  # Создать окно About
    about.exec_()  # Показать окно About

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question