F
F
First Name Last Name2015-07-08 18:12:55
Python
First Name Last Name, 2015-07-08 18:12:55

How to open a new window on clicking a button in the toolbar?

Help me, please, I want to open a new window in which I will place some information by clicking on the button in the toolbar. Here's what I already wrote. While the button performs the exit function.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('RM.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Toolbar')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

6f7a96fdf44a472f8db6dd4445df067d.JPG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-07-10
@FedkaOfficial

You can try like this:

import sys

from PyQt5.Qt import QVBoxLayout, QLabel, QDialog, QDialogButtonBox, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication


class InfoDialog(QDialog):

    def __init__(self, info_str, parent=None):
        super(InfoDialog, self).__init__(parent)

        layout = QVBoxLayout(self)
        layout.addWidget(QLabel(info_str))

        buttons = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal, self)
        buttons.accepted.connect(self.accept)

        layout.addWidget(buttons)


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('RM.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        infoAction = QAction(QIcon(), "Info", self)
        infoAction.triggered.connect(self.onInfoAction)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(infoAction)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Toolbar')
        self.show()

    def onInfoAction(self):
        w = InfoDialog("Some information", self)
        w.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

D
DENIS SHELESTOV, 2015-07-08
@djdeniro

the button has events, try to write print help(NAME_OF_BTN)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question