Aug-20-2018, 02:09 PM
(This post was last modified: Aug-20-2018, 02:09 PM by Axel_Erfurt.)
I do not understand your concept.
You place the button somewhere in the window where you want to have the text field later.
Then either the button or the text field is not visible.
Use a toolbar and place the button there.
Or place the button in the statusbar.
Do not use something like "Exit", "Are you sure?"
Users hate that.
better ask for saving changes.
here is a simple example, choose "New" from File Menu to show the Text Field
You place the button somewhere in the window where you want to have the text field later.
Then either the button or the text field is not visible.
Use a toolbar and place the button there.
Or place the button in the statusbar.
Do not use something like "Exit", "Are you sure?"
Users hate that.
better ask for saving changes.
here is a simple example, choose "New" from File Menu to show the Text Field
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication, QTextEdit, QAction, QMenu, QMainWindow) from PyQt5.QtGui import QIcon, QTextCursor, QKeySequence from PyQt5.QtCore import Qt import sys class myEditor(QMainWindow): def __init__(self, parent = None): super(myEditor, self).__init__(parent) self.editor = QTextEdit() self.editor.setTabStopWidth(20) bar=self.menuBar() self.filemenu=bar.addMenu("File") self.separatorAct = self.filemenu.addSeparator() self.filemenu.addAction(QAction(QIcon.fromTheme('document-new'), "New", self, triggered = self.newAct, shortcut = "Ctrl+n")) self.filemenu.addAction(QAction(QIcon.fromTheme('document-open'), "Open", self, triggered = self.openAct, shortcut = "Ctrl+o")) self.filemenu.addAction(QAction(QIcon.fromTheme('document-save'), "Save", self, triggered = self.saveAct, shortcut = "Ctrl+s")) self.filemenu.addAction(QAction(QIcon.fromTheme('document-save-as'), "Save as ...", self, triggered = self.saveAsAct, shortcut = "Shift+Ctrl+s")) self.filemenu.addSeparator() self.filemenu.addAction(QAction(QIcon.fromTheme('application-exit'), "Exit", self, triggered = self.exitAct, shortcut = "Ctrl+q")) editmenu = bar.addMenu("Edit") editmenu.addAction(QAction(QIcon.fromTheme('edit-undo'), "Undo", self, triggered = self.editor.undo, shortcut = "Ctrl+u")) editmenu.addAction(QAction(QIcon.fromTheme('edit-redo'), "Redo", self, triggered = self.editor.redo, shortcut = "Shift+Ctrl+u")) editmenu.addSeparator() editmenu.addAction(QAction(QIcon.fromTheme('edit-copy'), "Copy", self, triggered = self.editor.copy, shortcut = "Ctrl+c")) editmenu.addAction(QAction(QIcon.fromTheme('edit-cut'), "Cut", self, triggered = self.editor.cut, shortcut = "Ctrl+x")) editmenu.addAction(QAction(QIcon.fromTheme('edit-paste'), "Paste", self, triggered = self.editor.paste, shortcut = "Ctrl+v")) editmenu.addAction(QAction(QIcon.fromTheme('edit-delete'), "Delete", self, triggered = self.editor.cut, shortcut = "Del")) editmenu.addSeparator() editmenu.addAction(QAction(QIcon.fromTheme('edit-select-all'), "Select All", self, triggered = self.editor.selectAll, shortcut = "Ctrl+a")) self.layoutV = QVBoxLayout() # self.layoutV.addWidget(self.editor) mq = QWidget(self) mq.setLayout(self.layoutV) self.setCentralWidget(mq) self.createStatusBar() def createStatusBar(self): self.statusBar().showMessage("Ready", 0) def newAct(self): self.layoutV.addWidget(self.editor) self.statusBar().showMessage("TextEdit visible", 0) def openAct(self): return def saveAct(self): return def saveAsAct(self): return def exitAct(self): print("Goodbye ...") app.quit() if __name__ == '__main__': app = QApplication(sys.argv) win = myEditor() # win.setWindowIcon(QIcon.fromTheme("gnome-mime-text-x-python")) win.setWindowTitle("Editor") win.setMinimumSize(640,250) win.showMaximized() app.exec_()