0

这个简单的 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()  
4

3 回答 3

1

这里有一个 spyderlib问题。显然它已关闭,所以也许尝试更新您的库,看看它是否仍然是一个问题。

日志

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
于 2013-09-11T10:58:17.430 回答
0

问题似乎在

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

p[i-1]是 aQString而 Python 需要一个字符串。也许一个

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

解决问题。

于 2013-09-11T10:51:26.693 回答
0

截至 2015 年 12 月,尽管报告的 #1540 问题据说已修复,但奇怪的是存在相同的问题:包含(例如)这一行的代码:

newPath = os.path.dirname(newFile)

在蜘蛛下运行。在 Windows 中在 cmd 中编译时抛出 TypeError: TypeError: 'in ' requires string as left operand, not QString

你可以考虑:

newPath = os.path.dirname(str(newFile))
于 2015-12-13T02:04:02.780 回答