Skip to content

Unable to use lantz.ui.app.Frontend #48

Open

Description

@operte

I'm new to Lantz but I found its features to match the requirements of an experiment I'm currently working on. I'm basing most of my development on the tutorial. I've already written the drivers for my device and wanted to start building a GUI. For that, I started the rich-app tutorial. However, I immediately ran into an error after running the first piece of code:

# We import a helper function to start the app
from lantz.ui.app import start_gui_app

# The block consists of two parts the backend and the frontend
from lantz.ui.blocks import FeatScan, FeatScanUi

# An this you know already
from lantz.drivers.examples import LantzSignalGenerator

with LantzSignalGenerator('TCPIP::localhost::5678::SOCKET') as fungen:

    # Here we instantiate the backend setting 'frequency' as the Feat to scan
    # and specifying in which instrument
    app = FeatScan('frequency', instrument=fungen)

    # Now we use the helper to start the app.
    # It takes a Backend instance and a FrontEnd class
    start_gui_app(app, FeatScanUi)
Traceback (most recent call last):

  File "<ipython-input-14-d6e2c2282680>", line 1, in <module>
    runfile('C:/[...]/test.py', wdir='C:/[...]')

  File "C:\[...]\Anaconda\envs\py34\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)

  File "C:\[...]\Anaconda\envs\py34\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/[...]/Documents/JM/Santec TSL-510/PyControl/test.py", line 20, in <module>
    start_gui_app(app, FeatScanUi)

  File "C:\[...]\Anaconda\envs\py34\lib\site-packages\lantz\ui\app.py", line 242, in start_gui_app
    frontend = frontend_class(backend=backend)

  File "C:\[...]\Anaconda\envs\py34\lib\site-packages\lantz\ui\app.py", line 131, in __init__
    filename = os.path.dirname(inspect.getfile(cls))

  File "C:\[...]\Anaconda\envs\py34\lib\inspect.py", line 524, in getfile
    raise TypeError('{!r} is a built-in class'.format(object))

TypeError: <module 'builtins' (built-in)> is a built-in class

It seems the problem resides on a section of lantz.ui.app.Frontend:

class Frontend(QtGui.QMainWindow, metaclass=_FrontendType):

    frontends = {}

     # a declarative way to indicate the user interface file to use.
    gui = None

    # connect widgets to instruments using connect_setup
    auto_connect = True

    def __init__(self, parent=None, backend=None):
        super().__init__(parent)

        self._backend = None

        if self.gui:
            for cls in self.__class__.__mro__:
                filename = os.path.dirname(inspect.getfile(cls))       # <<<<<< This line!
                filename = os.path.join(filename, self.gui)
                if os.path.exists(filename):
                    logger.debug('{}: loading gui file {}'.format(self, filename))
                    self.widget = QtGui.loadUi(filename)
                    self.setCentralWidget(self.widget)
                    break
            else:
                raise ValueError('{}: loading gui file {}'.format(self, self.gui))

    # ...

The elements inside Frontend.__class__.__mro__ are:

(lantz.ui.app._FrontendType,
 PyQt4.QtCore.pyqtWrapperType,
 sip.wrappertype,
 type,
 object)

From this it seems the problem is when for cls in self.__class__.__mro__ goes through type and object. I think that since these two classes are built-ins, inspect.getfile(cls) raises the TypeError exception.
I'm not certain if this problem is specific to my system or if it is a general issue. Could it be an incompatibility between lantz and my current libraries? I'm using Python 3.4 and I think that I have every library up-to-date.

Activity

  1. operte commented on Feb 23, 2015

    @operte
    Author

    My naive approach was to simply remove the last two elements of Frontend.__class__.__mro__, hoping to avoid inspect.getfile(cls) to operate on type and object. I thought about doing it like this:

    class Frontend(QtGui.QMainWindow, metaclass=_FrontendType):
        # ...
        def __init__(self, parent=None, backend=None):
            # ...
            if self.gui:
                for cls in self.__class__.__mro__[0:-2]: # <<< Change here.
                    # ...

    This (certainly) naive modification eliminates the previous error message. However it does raise a new problem:

    Traceback (most recent call last):
    
      File "<ipython-input-1-d6e2c2282680>", line 1, in <module>
        runfile('C:/[...]/test.py', wdir='C:/[...]')
    
      File "C:\[...]\Anaconda\envs\py34\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
        execfile(filename, namespace)
    
      File "C:\[...]\Anaconda\envs\py34\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
        exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
    
      File "C:/[...]/test.py", line 20, in <module>
        start_gui_app(app, FeatScanUi)
    
      File "C:\[...]\Anaconda\envs\py34\lib\site-packages\lantz\ui\app.py", line 242, in start_gui_app
        frontend = frontend_class(backend=backend)
    
      File "C:\[...]\Anaconda\envs\py34\lib\site-packages\lantz\ui\app.py", line 139, in __init__
        raise ValueError('{}: loading gui file {}'.format(self, self.gui))
    
    ValueError: FeatScanUi: loading gui file scan.ui

    It seems to me that now, the condition if os.path.exists(filename) in Frontend.__init__ is never true for the elements in self.__class__.__mro__[0:-2]. However, I have manually tested every element in Frontend.__class__.__mro__[0:-2], namely (lantz.ui.app._FrontendType, PyQt4.QtCore.pyqtWrapperType, sip.wrappertype) and the output of applying os.path.exists(filename) on each one is True. That said, I think that either my naive approach didn't work or I have reached yet another issue. Any tips to solving this are very much welcome!

  2. hgrecco commented on Feb 23, 2015

    @hgrecco
    Contributor

    @operte Thanks for the bug report. I have not seen this problem before. Can you tell me which Python version and PyQt version are you using?

  3. operte commented on Feb 24, 2015

    @operte
    Author

    @hgrecco I am using Python 3.4.2, PyQt4 4.10.4 and Qt 4.8.6. I don't have PySide installed so I'm not certain that the issue you referenced is connected to this problem.

  4. hgrecco commented on Feb 25, 2015

    @hgrecco
    Contributor

    I have roughly the same setup. and it works for me. I will try to mimick your installation, try it and let you know.

  5. operte commented on Feb 25, 2015

    @operte
    Author

    @hgrecco Also I am running on Windows. Could it be related?

  6. hgrecco commented on Feb 25, 2015

    @hgrecco
    Contributor

    I do not use Windows but I have tried it on windows. I am wondering if this is because you are running within IPython. Can you try it outside?

  7. operte commented on Feb 25, 2015

    @operte
    Author

    Running this on a Python shell results in the same error message. I am going to try to install a new conda environment with the minimum amount of libraries necessary to check if my problem persists.

  8. hgrecco commented on Feb 25, 2015

    @hgrecco
    Contributor

    I have access to a Windows computer tomorrow. I will try and let you know.

  9. jflima11br commented on May 20, 2015

    @jflima11br

    Hi! I found the same problem that @operte. I'm using python 3.4.3 on windows. Any tips about how to solve that?

  10. hgrecco commented on May 20, 2015

    @hgrecco
    Contributor

    @operte and @jflima11br are you both usiing PyQt or PySIDE?

  11. jflima11br commented on May 21, 2015

    @jflima11br

    I'm using PyQt.

  12. bizarri commented on Jul 23, 2015

    @bizarri

    I am not sure if you are still looking for an answer but I had a similar issue than the ones reported by @operte and @jflima11br.
    My problem was the missing ui files for the different blocks (scan.ui, loop.ui...). I just added the files taken from lantz_qt/ui/blocks in the lantz/ui/blocks folder and now everything is working.
    Hope it helps.

  13. jflima11br commented on Jul 23, 2015

    @jflima11br

    Hi @bizarri! Same problem here... everything is working. Thank you!

  14. mpwer commented on Apr 12, 2017

    @mpwer

    Thanks @bizarri !
    Solved it for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions