Wxpython脚本在<string>中给出“TypeError:”要求字符串作为左操作数,而不是QString“</string>

时间:2013-09-11 10:45:13

标签: python exception wxpython

这个简单的wxpytohn脚本给出了以下错误,有什么问题?错误消息不会给出脚本中出现错误的行:

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

这是剧本:

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()  

3 个答案:

答案 0 :(得分:1)

here存在spyderlib问题。显然它是关闭的,所以也许尝试更新您的库并查看它是否仍然是一个问题。

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

答案 1 :(得分:0)

问题似乎在

p[i-1] not in '/\\'

p[i-1]QString,而Python期望一个字符串。也许是

str(p[i-1]) not in '/\\'

解决了这个问题。

答案 2 :(得分:0)

截至2015年12月,奇怪的是存在同样的问题,尽管报道的#1540问题被解决了:包含(例如)此行的代码:

newPath = os.path.dirname(newFile)

在蜘蛛下运行。在Windows中的cmd中编译时会抛出TypeError:     TypeError:&#39; in&#39;需要字符串作为左操作数,而不是QString

您可以考虑:

newPath = os.path.dirname(str(newFile))
相关问题