Repository: spyder-ide/spyder

Issue:
RuntimeError when disconnecting monitors
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
Having a second monitor conected to my notebook via HDMI and running spyder in this second monitor, if I disconnect the hdmi monitor I got a "Spyder has encountered an internal problem". 

Spyder does not crashs but it opens a issue reporter. Message is:

  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted
Traceback (most recent call last):
  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted
Traceback (most recent call last):
  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted


### What steps reproduce the problem?

1. Have a secondary monitor connected to your computer
2. Have spyder ruuning in the secondary monitor
3. Disconnect the HDMI monitor

### What is the expected output? What do you see instead?

### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted
Traceback (most recent call last):
  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted
Traceback (most recent call last):
  File "/home/cristiana/anaconda3/envs/fintools/lib/python3.8/site-packages/spyder/app/mainwindow.py", line 1392, in handle_new_screen
    self.screen.logicalDotsPerInchChanged.disconnect(
RuntimeError: wrapped C/C++ object of type QScreen has been deleted


```

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.1.1
* Python version: 3.8.1
* Qt version: 
* PyQt version: 
* Operating System name/version: Ubuntu 18.04.4 LTS

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE
```



Discussion:
Thanks a lot for reporting @Cristianasp! This came right on time before releasing 4.1.2.

@steff456, this is another easy one for you: please add RuntimeError next to to TypeError below:

https://github.com/spyder-ide/spyder/blob/31c2129d4629ff27bc3c36a4235f26a1e15fe0ce/spyder/app/mainwindow.py#L1400-L1405

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -1400,8 +1400,8 @@ def handle_new_screen(self, screen):
         try:
             self.screen.logicalDotsPerInchChanged.disconnect(
                 self.show_dpi_change_message)
-        except TypeError:
-            # See spyder-ide/spyder#11903
+        except (TypeError, RuntimeError):
+            # See spyder-ide/spyder#11903 and spyder-ide/spyder#11997
             pass
         self.screen = screen
         self.screen.logicalDotsPerInchChanged.connect(


Test patch: