I'm trying to learn wxPython. Unfortunately when I came to China in Jan I had to leave the nice wxPython book in Canada because it was too heavy for my suitcase. So I humbly ask you to bear with my ignorance.
As practice (and because I have a practical use for it) I want to read in a textfile and display the text line by line. Much to my surprise & delight, the following program actually works!
''' TextFileDisp00.py
'''
import wx
import codecs
#-------------------
class TextFrame(wx.Frame):
def __init__(self, parent, mytitle, mysize):
wx.Frame.__init__(self, parent, -1, mytitle, mysize)
self.menubar = wx.MenuBar()
self.file = wx.Menu()
self.SetMenuBar(self.menubar)
self.menubar.Append(self.file,'&File')
self.openitem = self.file.Append(wx.ID_ANY,'&Open')
self.Bind(wx.EVT_MENU,self.openevent,self.openitem)
self.runitem = self.file.Append(wx.ID_ANY,'&Run')
self.Bind(wx.EVT_MENU,self.runevent,self.runitem)
self.exititem = self.file.Append(wx.ID_ANY,'E&xit')
self.Bind(wx.EVT_MENU,self.exitevent,self.exititem)
self.background = wx.Panel(self)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.background, proportion=0, flag=wx.EXPAND)
self.SetSizer(self.vbox)
self.fileisopen = False
self.Show()
def openevent(self, event):
filedialog = wx.FileDialog(self,
message = 'Open text file',
defaultDir = '.',
defaultFile = 'TestTOC.txt',
wildcard = 'Text file (*.txt;*.prn)| *.txt;*.prn',
style = wx.OPEN)
if filedialog.ShowModal() == wx.ID_OK:
self.path = filedialog.GetPath()
self.fileisopen = True
def runevent(self, event):
if not self.fileisopen:
msgbox = wx.MessageDialog(self.background, message="No file is open!", style=wx.OK)
if msgbox.ShowModal() == wx.ID_OK:
msgbox.Destroy()
else:
myOK = True
linenr = 0
InFile = codecs.open(self.path, 'r')
textline = InFile.readline()
while textline <> '' and myOK:
linenr += 1
msg = "Line " + repr(linenr) + ": " + textline
msgbox = wx.MessageDialog(self.background, message=msg, style=wx.OK+wx.CANCEL)
if msgbox.ShowModal() == wx.ID_CANCEL:
myOK = False
msgbox.Destroy()
textline = InFile.readline()
msg = "Finished, # lines = " + repr(linenr)
msgbox = wx.MessageDialog(self.background, message=msg, style=wx.OK)
if msgbox.ShowModal() == wx.ID_OK:
msgbox.Destroy()
InFile.close()
self.Destroy()
def exitevent(self, event):
self.Destroy()
#-------------------
''' Main application
'''
app = wx.App(redirect = False)
mywidth = 300
myheight = 150
mytitle = "Text file display"
frame = TextFrame(None, mytitle, (mywidth,myheight))
app.MainLoop()
I would appreciate the comments of experienced users about style and things I have not understood. I have a couple of specific questions as well:
1. I don't seem to understand sizers, and I can't make the background panel extend to the bottom of the app window.
2. I would like to display the text much bigger, i.e. in a (possibly multiline) textbox in which I could set font and font color & size. But I would still like to use a modal box, so I can press Enter to see the next line. How is this done?
Thanks for your help and understanding! Next time my questions might be smarter.