0

Tengo el siguiente problea.

Tengo una venta con un boton cuya función es mostrar una segunda ventana, pero cuando vuelvo a la primera ventana la segunda ventana que se abrio se va al fondo de la primera y si vuelvo a dar click en el boton no se muestra al frente de nuevo.

Que puedo hacer para que al dar click de nuevo en el boton esa ventana pase al frente de nuevo.

primero.py

    from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic
from segundo import Secundaria

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("1.ui",self)

        self.segunda  = Secundaria()
        self.boton.clicked.connect(lambda:self.segunda.show())




app = QApplication([])
p = Principal()
p.show()
app.exec_()

segundo.py

    from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic


class Secundaria(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("2.ui",self)

1.ui

<?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>609</width>
            <height>435</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <widget class="QPushButton" name="boton">
            <property name="geometry">
             <rect>
              <x>220</x>
              <y>190</y>
              <width>151</width>
              <height>23</height>
             </rect>
            </property>
            <property name="text">
             <string>Mostrar SEGUNDA Ventana</string>
            </property>
           </widget>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>609</width>
             <height>21</height>
            </rect>
           </property>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
         </widget>
         <resources/>
         <connections/>
        </ui>

2.ui

<?xml version="1.0" encoding="UTF-8"?>
  <ui version="4.0">
   <class>MainWindow</class>
   <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>439</width>
      <height>279</height>
     </rect>
    </property>
    <property name="windowTitle">
     <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralwidget"/>
    <widget class="QMenuBar" name="menubar">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>439</width>
       <height>21</height>
      </rect>
     </property>
    </widget>
    <widget class="QStatusBar" name="statusbar"/>
   </widget>
   <resources/>
   <connections/>
  </ui>

1 respuesta 1

1

La solución es llamar a la propiedad activateWindow(), en self.boton

El código resultante quedaria:

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic
from segundo import Secundaria

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("1.ui",self)

        self.segunda  = Secundaria()
        self.boton.clicked.connect(lambda:self.segunda.show())
        self.boton.clicked.connect(lambda:self.segunda.activateWindow())




app = QApplication([])
p = Principal()
p.show()
app.exec_()

De esta manera indicamos que al presionar el botón, muestre la segunda ventana y si la venta pasa a segundo plano al oprimir el boton nuevamente la función activateWindow() la traerá de nuevo al frente

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.