D
D
DenisPy132018-02-21 00:11:20
Python
DenisPy13, 2018-02-21 00:11:20

How to make a child window?

How can I make a child window so that variable values ​​can be passed from it?
Hello everyone, if someone knows, please write)
There is a main menu with QPushButton and QTextBrowser
It is necessary that through QPushButton a window opens with a name input field and with a button (save), after which, when you click on the button (save), the window closes and the name that was entered in the input field was displayed in the QTextBrowser in the main window)
Explained like a god)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-02-23
@sanya84

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

def log_uncaught_exceptions(ex_cls, ex, tb):
    import traceback
    text = '{}: {}:\n\n'.format(ex_cls.__name__, ex)
    text += ''.join(traceback.format_tb(tb))
    QMessageBox.critical(None, 'Ошибка!', text)
    
sys.excepthook = log_uncaught_exceptions


class EnterName(QWidget):
    text_value = pyqtSignal(str)
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 300, 50)
        self.setWindowTitle('Введите имя')

        self.button = QPushButton('Сохранить', self)
        self.button.clicked.connect(self.bEvent)
        self.button.move(120, 30)

        self.lEdid = QLineEdit(self)
        self.lEdid.setGeometry(0, 0, 300, 30)

    @pyqtSlot()
    def bEvent(self):
        self.text_value.emit(self.lEdid.text())
        self.hide()


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

    def initUI(self):
        self.setGeometry(300, 300, 420, 280)
        self.setWindowTitle('Exsample')

        self.button = QPushButton('Имя', self)
        self.button.move(180, 260)
        self.button.clicked.connect(self.bEvent)

        self.tb = QTextBrowser(self)
        self.tb.setGeometry(0, 0, 420, 260)

        self.eName = EnterName()
        self.eName.text_value.connect(self.signalHandler)

    def bEvent(self):
        self.eName.show()

    def signalHandler(self, text):
        self.tb.append(text)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    example = Example()
    example.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question