奇怪的错误,小弟找不出问题,请大伙帮忙!关于Python wxFileDialog的。

lejins 2004-11-30 05:06:44
在win32下通过,顺利运行,到RH9下就出错。
win32环境 python2.3.4 wxpython2.5.1
rh9:python2.3.4 wxpython2.5.3

源码:
# simple text viewer
#
# Copyright 2004
#
# License: Python

from wxPython.wx import *
import os
import string

#----------------------------------------------------------------------

ID_ABOUT = wxNewId()
ID_EXIT = wxNewId()
ID_FILEOPEN = wxNewId()
ID_FILECLOSE = wxNewId()

#----------------------------------------------------------------------

#----------------------------------------------------------------------

class MyFrame(wxFrame):
#----------------------------------------------------------------------
def __init__(self, parent, ID, title):

#Init our frame use wxFrame

wxFrame.__init__(self, parent, ID, "simple text viewer",
wxDefaultPosition,size=(800,600))


self.CreateStatusBar() #
self.SetStatusText("No File Opened!")
self.MakeMenuBar()
self.MakeDisplay()


EVT_MENU(self, ID_ABOUT, self.OnAbout)
EVT_MENU(self, ID_EXIT, self.TimeToQuit)
EVT_MENU(self, ID_FILEOPEN, self.OnFileOpen)
EVT_MENU(self, ID_FILECLOSE, self.OnFileClose)

#----------------------------------------------------------------------

def MakeMenuBar(self):
menu = wxMenu()
menu.Append(ID_FILEOPEN, "&Open",
"Open file")
menu.Append(ID_FILECLOSE, "&Close",
"Close file")
menu.AppendSeparator()
menu.Append(ID_EXIT, "E&xit", "Terminate the program")

menu_help = wxMenu()
menu_help.Append(ID_ABOUT, "&About",
"More information about this program")
menuBar = wxMenuBar()
menuBar.Append(menu, "&File");
menuBar.Append(menu_help, "&Help");

self.SetMenuBar(menuBar)

#----------------------------------------------------------------------

def MakeDisplay(self):
self.DisPlay = wxTextCtrl(self, -1,'',
style=wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2 ,
size=self.GetClientSize())
self.DisPlay.SetInsertionPoint(0)

#----------------------------------------------------------------------

def OnAbout(self, event):
dlg = wxMessageDialog(self, "simple text viewer \n"
"CopyRight 2004.",
"About Me", wxOK | wxICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()

#----------------------------------------------------------------------

def TimeToQuit(self, event):
self.Close(true)

#----------------------------------------------------------------------

def OnFileOpen(self, event):
defaultDir = "."
defaultFile = ""
wildCard = "Text files(*.txt)|*.txt|Python Script(*.py)|*.py|All Files(*.*)|*.*"
fileName = None
fileDialog = wxFileDialog(self, "Choose a file", defaultDir, defaultFile,
wildCard, wxOPEN|wxMULTIPLE)
result = fileDialog.ShowModal()
if result == wxID_OK:
fileName = fileDialog.GetPath()
"""dlg = wxMessageDialog(self, ('%s\n' % fileName), "filename",
wxOK | wxICON_INFORMATION)
result = dlg.ShowModal()
dlg.Destroy()"""
self.DisPlayFile(fileName)
self.SetStatusText('Opened: %s\n' % fileName)
fileDialog.Destroy()

#----------------------------------------------------------------------

def OnFileClose(self, event):
self.DisPlay.Clear()
self.DisPlay.GetClientSize()
self.SetStatusText("No File Opened!")

#----------------------------------------------------------------------

def DisPlayFile(self,FileName):
self.DisPlay.Clear()
self.DisPlay.GetClientSize()
f = file(FileName, 'rU')
TotalContents = ''
contents = f.readline()

while contents:
if 32768 < len(contents):
self.DisPlay.AppendText(contents)

else:
TotalContents += contents
ContentsLen=len(TotalContents)
if 32768 < ContentsLen :
self.DisPlay.AppendText(TotalContents)
TotalContents = ''
contents = f.readline()
self.DisPlay.AppendText(TotalContents)
f.close()


#----------------------------------------------------------------------

class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1, "wxPython")
frame.SetBackgroundColour('White')
frame.Show(true)
self.SetTopWindow(frame)
return true

#----------------------------------------------------------------------

app = MyApp(0)
app.MainLoop()

...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lejins 2004-12-01
  • 打赏
  • 举报
回复
没有用中文名,在Fedora下边测试也有警告,但是可以顺利运行。
Python2.3.3, WxPython 2.5.2.8


[root@localhost jason]# python2.3 simple.py

Gdk-CRITICAL **: file gdkwindow.c: line 1390 (gdk_window_get_size): assertion `window != NULL' failed.

xyzxyz1111 2004-12-01
  • 打赏
  • 举报
回复
是不是用了什么中文的文件名?
这应该是一个bug
lejins 2004-12-01
  • 打赏
  • 举报
回复
谢谢各位
lejins 2004-12-01
  • 打赏
  • 举报
回复
那也就是说不是我的程序问题了?是系统问题了。
xyzxyz1111 2004-12-01
  • 打赏
  • 举报
回复
在src/common/string.cpp(1060)

wxASSERT(nLen != (size_t)-1); //should not be true! If it is system mbtowc could be bad

所以应该是libc 中的mbtowc方法出现问题也(返回0)。

在win32中是用MultiByteToWideChar实现的,同linux下不同。

应该可以报告bug.
lejins 2004-11-30
  • 打赏
  • 举报
回复
错误提示:
[root@iwl work]# python2.3 simple.py
Traceback (most recent call last):
File "simple.py", line 93, in OnFileOpen
wildCard, wxOPEN|wxMULTIPLE)
File "/usr/lib/python2.3/site-packages/wx-2.5.3-gtk2-unicode/wx/_windows.py", line 2359, in __init__
newobj = _windows_.new_FileDialog(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "nLen != (size_t)-1" failed in ../src/common/string.cpp(1060)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧