奇怪的错误,小弟找不出问题,请大伙帮忙!关于Python wxFileDialog的。
在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()