pippo1980
pippo1980

Reputation: 3061

PyQt5 relation between windows, first won't close

I started treading into Linux, python3 three weeks ago, and from the second week started to look at PyQt5 too.

There many tutorials and book, to me Qt documentation seems written in Klingon, so I am trying to follow

examples found on the web.

The last problem I am facing is: given:

test3.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr  8 14:25:12 2020

@author: Pietro
"""

import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QTimer
import time

class Quitto(QtWidgets.QDialog):

    def quitbuttonboxquit(self):
        print('system exit')
        app.quit()          

    def quitbuttonboxnonquit(self):
        print('return to main')
        window.show()
        self.close()

    def __init__(self):
        super(Quitto, self).__init__()
        uic.loadUi('exitdialog3.ui', self)
        self.center()
        self.show()
        print('inside quitting2 ' *5)

        self.QuitbuttonBox.accepted.connect(self.quitbuttonboxquit)
        self.QuitbuttonBox.rejected.connect(self.quitbuttonboxnonquit)

        self.counter = 0
        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.recurring_timer)
        self.timer.start() 

    def center(self):
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def closeEvent(self, event): 
            event.ignore()

    def recurring_timer(self):
        self.counter +=1
        self.label2.setText("TTCounter: %d"  % self.counter)    

class Menu(QtWidgets.QMainWindow):

    def __init__(self): 
        super(Menu, self).__init__()
        uic.loadUi('main2.ui', self)
        self.ButtonQ.clicked.connect(self.qpushbuttonqpressed) 
        self.center()
        self.show() 

        self.counter = 0                      
        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.recurring_timer)
        self.timer.start()

        self.ButtonA.clicked.connect(self.qpushbuttonapressed)          

    def center(self):
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())               

    def qpushbuttonqpressed(self): #This is executed when the button is pressed
        print('buttonQ pressed' *5)

    #    window.close()   # doens't work, wondow doesn't close

        window.hide()     # this one does, windows hide

        self.pippo=Quitto()
        self.pippo.show()            

    def qpushbuttonapressed(self): #This is executed when the button is pressed
        print('buttonA pressed' *5)
        self.oh_no()

    def closeEvent(self, event): #Your desired functionality here 
            event.ignore()

    def recurring_timer(self):
        self.counter +=1
        self.label2.setText("TTCounter: %d"  % self.counter)    

    def oh_no(self):
            time.sleep(5)                           



if __name__ == '__main__':            
    app = QtWidgets.QApplication(sys.argv)
    sshFile="coffee.qss"
    window=Menu()
    sys.exit(app.exec_())

and the two .ui files,

main2.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="windowModality">
   <enum>Qt::WindowModal</enum>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>544</width>
    <height>686</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="windowIcon">
   <iconset resource="resource001.qrc">
    <normaloff>:/main/python.png</normaloff>
    <normalon>:/main/python.png</normalon>
    <disabledoff>:/main/python.png</disabledoff>
    <disabledon>:/main/python.png</disabledon>
    <activeoff>:/main/python.png</activeoff>
    <activeon>:/main/python.png</activeon>
    <selectedoff>:/main/python.png</selectedoff>
    <selectedon>:/main/python.png</selectedon>:/main/python.png</iconset>
  </property>
  <property name="styleSheet">
   <string notr="true">QPushButton{
    background-color: #9de650;
}


QPushButton:hover{
    background-color: green;
}

QPushButton#ButtonQ{
    background-color: orange;
}


QPushButton#ButtonQ:hover{
    background-color: red;
}</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>0</y>
      <width>471</width>
      <height>71</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
      <weight>75</weight>
      <bold>true</bold>
      <underline>true</underline>
     </font>
    </property>
    <property name="text">
     <string>House-Buying-Menu</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="ButtonA">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>200</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>SLEEP 5</string>
    </property>
   </widget>
   <widget class="QPushButton" name="ButtonB">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>310</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>B</string>
    </property>
   </widget>
   <widget class="QPushButton" name="ButtonC">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>420</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>C</string>
    </property>
   </widget>
   <widget class="QPushButton" name="ButtonQ">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>550</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>QUIT</string>
    </property>
   </widget>
   <widget class="QLabel" name="label2">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>80</y>
      <width>221</width>
      <height>91</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>544</width>
     <height>29</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources>
  <include location="resource001.qrc"/>
 </resources>
 <connections/>
</ui>

exitdialog3.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="windowModality">
   <enum>Qt::WindowModal</enum>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>720</width>
    <height>655</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <italic>true</italic>
   </font>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QDialogButtonBox" name="QuitbuttonBox">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>560</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>18</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">QPushButton{                 
    background-color: #17eb3e;
}



QPushButton:hover{                 
    background-color: red;
}


</string>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>140</y>
     <width>491</width>
     <height>341</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">image: url(:/main/alert.png);</string>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>210</x>
     <y>510</y>
     <width>271</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>18</pointsize>
     <weight>75</weight>
     <italic>false</italic>
     <bold>true</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">QLabel { 
    color : red
 }</string>
   </property>
   <property name="text">
    <string>Are you sure to Quit ?!?</string>
   </property>
  </widget>
  <widget class="QLabel" name="label2">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>40</y>
     <width>321</width>
     <height>101</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>20</pointsize>
     <weight>75</weight>
     <italic>false</italic>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
 </widget>
 <resources>
  <include location="resource001.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>QuitbuttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>QuitbuttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

I don't understand why pressing Quit on the first window doesn't close it despite

window.close() #line 80 of test3.py ?

and why if I cancel quitting on the second window and then re-quit from the first window the timer on the second window restarts from 0, while the first window that doesn't close doesn't stop its timer too and if I press 'sleep 5' on the first window that doesn't close, both first and second window timers stop for 5 seconds?

Is it something about the parent/child window relation? None of the .ui files says anything like Parent or Child? Is it something else? How could I make the first window timer stop (i.e. closing it for sure) while the second window is on

Upvotes: 0

Views: 63

Answers (1)

Alexander Chernin
Alexander Chernin

Reputation: 444

Because the variable window hadn't been declared in the scope of the def:

def qpushbuttonqpressed(self): #This is executed when the button is pressed
    print('buttonQ pressed' *5)

    self.close() # this is correct

    self.pippo=Quitto()
    self.pippo.exec() # use exec() to display the dialog
    self.show() # When the dialog will be closed we show the window again

The same mistake is in Quitto.quitbuttonboxnonquit:

def quitbuttonboxnonquit(self):
    print('return to main')
    # window.show() just delete this row
    self.close()

Upvotes: 1

Related Questions