How to bring a window to the front again PyQt5

0

I have the following problem.

I have a sale with a button whose function is to show a second window, but when I return to the first window the second window that was opened goes to the bottom of the first one and if I click again on the button it is not shown to the front again.

What can I do so that when I click on the button again that window comes to the front again.

first.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_()

second.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>
    
asked by Revsky01 15.10.2018 в 06:43
source

1 answer

0

The solution is to call the property activateWindow() , in self.boton

The resulting code would be:

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_()

This way we indicate that when pressing the button, it shows the second window and if the sale goes to the second plane when pressing the button again the function activateWindow() will bring it back to the front

    
answered by 15.10.2018 / 17:46
source