0

I am using PyQt5 and IPython 7.3.0 on Ubuntu 18.04. I am loading this tiny Python module from an ipython console invoked with eith --gui=qt5 switch or %gui qt5 executed after the shell has started. In either case, it displays a black window. Is it an IPython bug, or am I missing something?

from PyQt5.QtWidgets import QApplication, QWidget

class Viewer(QWidget):
    def __init__(self, text):
        super().__init__()      
        self.setWindowTitle('Viewer')        
        self.show()

def view_from_ipython():
    ex = Viewer('Hello')
    import time; time.sleep(5)

The module above is named as demo.py.

$ ipython --gui=qt5
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import demo

In [2]: demo.view_from_ipython()

It shows a black window as shown below.

enter image description here

Update 1: I forgot to mention that I want to show up the window in a non-modal way, meaning that the window that pops up should not prohibit the user from interacting with the ipython session. The solutions pointed out so far blocks the interactive session. I want to make it behave in the same way as matplotlib graph plotting works (just to give an idea).

sherlock
  • 2,397
  • 3
  • 27
  • 44
  • 1
    another solution: returns the view so that it is not eliminated ----> `def view_from_ipython():` `ex = Viewer('Hello')` `return ex` – eyllanesc Aug 16 '19 at 03:52
  • Wow, that did the trick. Do you know why it was rendering a black window earlier? I mean, what's the logic behind? – sherlock Aug 16 '19 at 03:57
  • 1
    Qt lives in an event loop that allows you to execute tasks such as rendering, listen to OS events, etc. but with your time.sleep() you are blocking it causing the GUI to freeze. Do not execute tasks that consume a lot of time in the main thread, depending on what you want to do Qt will offer you reasonable alternatives – eyllanesc Aug 16 '19 at 04:00

0 Answers0