Search code examples
pythonexceptionwxpython

Wxpython script gives "TypeError: 'in <string>' requires string as left operand, not QString"


this simple wxpytohn script gives the following error, what is wrong? The error message doesnt give which line the error occurs in script:

Traceback (most recent call last):
  File "c:\Python27\lib\site-packages\spyderlib\plugins\externalconsole.py", line 721, in run_script_in_current_shell
    "and try again.") % osp.basename(filename), QMessageBox.Ok)
  File "c:\Python27\lib\ntpath.py", line 198, in basename
    return split(p)[1]
  File "c:\Python27\lib\ntpath.py", line 173, in split
    while i and p[i-1] not in '/\\':
TypeError: 'in <string>' requires string as left operand, not QString

This is the script:

import wx
import os


def getParentFolder():
    moduleFile = __file__
    moduleDir = os.path.split(os.path.abspath(moduleFile))[0]
    programFolder = os.path.abspath(moduleDir)
    parentFolder = os.path.abspath(os.path.join(programFolder, os.pardir))
    return programFolder, parentFolder

class MainWindow(wx.Frame):

    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(1200,900))
        #self.Bind(wx.EVT_CLOSE, self.closeWindow)
        self.SetIcon(wx.Icon("plane.ico", wx.BITMAP_TYPE_ICO))
        self.Center()

        icons_folder = getParentFolder()[1] 

        menubar = wx.MenuBar()
        scenario_menu = wx.Menu()
        database_menu= wx.Menu()
        settings_menu = wx.Menu()


        open_scenario = wx.MenuItem(scenario_menu, 101, '&Open\tCtrl+O', 'Open an existing scenario')
        open_scenario.SetIcon((wx.Icon(icons_folder+"\\open_dir.ico", wx.BITMAP_TYPE_ICO)))
        scenario_menu.AppendItem(open_scenario)

        scenario_menu.Append(102, "&Create", "Create new scenario")
        scenario_menu.Append(103, "&Save", "Save scenario")

        menubar.Append(scenario_menu, "&File")
        menubar.Append(database_menu, "&Database")
        menubar.Append(settings_menu, "&Settings")



if __name__ == "__main__":
    app = wx.App()
    frame = MainWindow("CrewOpt")
    frame.Show()
    app.MainLoop()  

Solution

  • There is a spyderlib issue for this here. Apparently it's closed, so maybe try updating your libraries and see if it's still a problem.

    Log

    Log message
    
    Executing script in current Python/IPython interpreter while no interpreter is
    running was raising a TypeError exception:
    
    Traceback (most recent call last):
      File "[...]\spyderlib\plugins\externalconsole.py", line 722, in
    run_script_in_current_shell
        "and try again.") % osp.basename(filename), QMessageBox.Ok)
      File "[...]\python-2.7.5.amd64\lib\ntpath.py", line 198, in basename
        return split(p)[1]
      File "[...]\python-2.7.5.amd64\lib\ntpath.py", line 173, in split
        while i and p[i-1] not in '/\\':
    TypeError: 'in <string>' requires string as left operand, not QString
    
    Update  Issue 1540 
    Status: Fixed
    
    Affected files
        expand all   collapse all
        Modify  /spyderlib/plugins/externalconsole.py   diff