Answer the question
In order to leave comments, you need to log in
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_())
Answer the question
In order to leave comments, you need to log in
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_())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question