miércoles, 17 de mayo de 2017

¿CÓMO CREAR VENTANAS EMERGENTES EN PYTHON-PYQT? FORMA I


Ejercicio en Construcción:

Hola a todos, esta vez mostraré un ejemplo para crear ventana emergente, en la que nos mostrará un aviso.

Este ejercicio se hará tomando una ventana creada en QT como la aplicación principal, y la ventana emergente será una creada desde la consola.

PASO 1: Crear la interfaz gráfica.


Este es el código creado para la ventana principal.


ppal.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ppal.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(362, 178)
        self.splitter = QtGui.QSplitter(Dialog)
        self.splitter.setGeometry(QtCore.QRect(70, 40, 221, 81))
        self.splitter.setOrientation(QtCore.Qt.Vertical)
        self.splitter.setObjectName(_fromUtf8("splitter"))
        self.label = QtGui.QLabel(self.splitter)
        self.label.setObjectName(_fromUtf8("label"))
        self.lineEdit = QtGui.QLineEdit(self.splitter)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.pushButton = QtGui.QPushButton(self.splitter)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "INGRESE SU NOMBRE:", None))
        self.pushButton.setText(_translate("Dialog", "OK", None))

PASO 2: Crear el archivo ejecutable.

ejecutable.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
from ppal import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class EMERGENTE(QtGui.QWidget):
    def __init__(self,name=None):
        super(EMERGENTE, self).__init__()
        self.name = name
        btn1 = QtGui.QLabel("%s" % self.name,self)
        btn1.move(50, 50)
        contenedor = QVBoxLayout()
        self.setLayout(contenedor)
        btnsalir=QPushButton("salir", None)
        contenedor.addWidget(btnsalir)
        btnsalir.move(50, 50)
        self.connect(btnsalir, SIGNAL("clicked()"), self.salir)
        self.setGeometry(500, 300, 250, 180)
        self.setWindowTitle('EMERGENTE')
        self.show()
        
    def salir(self):
        self.close()

class PRINCIPAL(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'),self.aceptar)
        self.children = []
        self.setWindowTitle('VENTANA PRINCIPAL')
        self.show()

    def aceptar(self):
        name=self.ui.lineEdit.text()
        child= EMERGENTE(name)
        self.children.append(child)
       
 
if __name__ == '__main__':
    app=QtGui.QApplication(sys.argv)
    myapp = PRINCIPAL()
    myapp.show()
    sys.exit(app.exec_())

Resultados: Como se puede observar, al ingresar en el espacio Nombre, se abrirá una ventana emergente que mostrará la linea de texto ingresada en la ventana principal.

 Espero haya sido útil el ejercicio, y cualquier duda que tengan con gusto la resolveré.

Este ejercicio también lo voy a realizar utilizando una ventana emergente pero creada en QT. Pronto avisaré cuando esté realizada.

Salu2.

1 comentario: