updated to wx namespace

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28896 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2004-08-25 19:57:40 +00:00
parent afacf84f44
commit 0b910ef421
2 changed files with 386 additions and 385 deletions

View File

@@ -1,6 +1,6 @@
"""Hangman.py, a simple wxPython game, inspired by the """Hangman.py, a simple wxPython game, inspired by the
old bsd game by Ken Arnold. old bsd game by Ken Arnold.
From the original man page: >From the original man page:
In hangman, the computer picks a word from the on-line In hangman, the computer picks a word from the on-line
word list and you must try to guess it. The computer word list and you must try to guess it. The computer
@@ -15,8 +15,7 @@ Have fun with it,
Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)""" Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)"""
import random,re import random,re
from wxPython.wx import * import wx
class WordFetcher: class WordFetcher:
@@ -115,18 +114,21 @@ class URLWordFetcher(WordFetcher):
class HangmanWnd(wxWindow): class HangmanWnd(wx.Window):
def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize): def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize):
wxWindow.__init__(self, parent, id, pos, size) wx.Window.__init__(self, parent, id, pos, size)
self.SetBackgroundColour(wxNamedColour('white')) self.SetBackgroundColour(wx.NamedColour('white'))
if wxPlatform == '__WXGTK__': if wx.Platform == '__WXGTK__':
self.font = wxFont(12, wxMODERN, wxNORMAL, wxNORMAL) self.font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)
else: else:
self.font = wxFont(10, wxMODERN, wxNORMAL, wxNORMAL) self.font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL)
self.SetFocus() self.SetFocus()
EVT_PAINT(self, self.OnPaint) self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
def OnSize(self, event):
self.Refresh()
def StartGame(self, word): def StartGame(self, word):
self.word = word self.word = word
self.guess = [] self.guess = []
@@ -165,7 +167,7 @@ class HangmanWnd(wxWindow):
def Draw(self, dc = None): def Draw(self, dc = None):
if not dc: if not dc:
dc = wxClientDC(self) dc = wx.ClientDC(self)
dc.SetFont(self.font) dc.SetFont(self.font)
dc.Clear() dc.Clear()
(x,y) = self.GetSizeTuple() (x,y) = self.GetSizeTuple()
@@ -188,13 +190,13 @@ class HangmanWnd(wxWindow):
self.DrawVictim(dc) self.DrawVictim(dc)
def DrawVictim(self, dc): def DrawVictim(self, dc):
dc.SetPen(wxPen(wxNamedColour('black'), 20)) dc.SetPen(wx.Pen(wx.NamedColour('black'), 20))
dc.DrawLines([(10, 980), (10,900), (700,900), (700,940), (720,940), dc.DrawLines([(10, 980), (10,900), (700,900), (700,940), (720,940),
(720,980), (900,980)]) (720,980), (900,980)])
dc.DrawLines([(100,900), (100, 100), (300,100)]) dc.DrawLines([(100,900), (100, 100), (300,100)])
dc.DrawLine(100,200,200,100) dc.DrawLine(100,200,200,100)
if ( self.misses == 0 ): return if ( self.misses == 0 ): return
dc.SetPen(wxPen(wxNamedColour('blue'), 10)) dc.SetPen(wx.Pen(wx.NamedColour('blue'), 10))
dc.DrawLine(300,100,300,200) dc.DrawLine(300,100,300,200)
if ( self.misses == 1 ): return if ( self.misses == 1 ): return
dc.DrawEllipse(250,200,100,100) dc.DrawEllipse(250,200,100,100)
@@ -210,7 +212,7 @@ class HangmanWnd(wxWindow):
dc.DrawLine(300,600,250,850) dc.DrawLine(300,600,250,850)
def OnPaint(self, event): def OnPaint(self, event):
dc = wxPaintDC(self) dc = wx.PaintDC(self)
self.Draw(dc) self.Draw(dc)
@@ -239,9 +241,9 @@ class HangmanDemo(HangmanWnd):
def Stop(self): def Stop(self):
self.timer.Stop() self.timer.Stop()
class PlayTimer(wxTimer): class PlayTimer(wx.Timer):
def __init__(self,func): def __init__(self,func):
wxTimer.__init__(self) wx.Timer.__init__(self)
self.func = func self.func = func
self.Start(1000) self.Start(1000)
@@ -250,11 +252,11 @@ class HangmanDemo(HangmanWnd):
class HangmanDemoFrame(wxFrame): class HangmanDemoFrame(wx.Frame):
def __init__(self, wf, parent, id, pos, size): def __init__(self, wf, parent, id, pos, size):
wxFrame.__init__(self, parent, id, "Hangman demo", pos, size) wx.Frame.__init__(self, parent, id, "Hangman demo", pos, size)
self.demo = HangmanDemo(wf, self, -1, wxDefaultPosition, wxDefaultSize) self.demo = HangmanDemo(wf, self, -1, wx.DefaultPosition, wx.DefaultSize)
EVT_CLOSE(self, self.OnCloseWindow) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
def OnCloseWindow(self, event): def OnCloseWindow(self, event):
self.demo.timer.Stop() self.demo.timer.Stop()
@@ -262,26 +264,27 @@ class HangmanDemoFrame(wxFrame):
class AboutBox(wxDialog): class AboutBox(wx.Dialog):
def __init__(self, parent,wf): def __init__(self, parent,wf):
wxDialog.__init__(self, parent, -1, "About Hangman", wxDefaultPosition, wxSize(350,450)) wx.Dialog.__init__(self, parent, -1, "About Hangman", wx.DefaultPosition, (350,450))
self.wnd = HangmanDemo(wf, self, -1, wxPoint(1,1), wxSize(350,150)) self.wnd = HangmanDemo(wf, self, -1, (1,1), (350,150))
self.static = wxStaticText(self, -1, __doc__, wxPoint(1,160), wxSize(350, 250)) self.static = wx.StaticText(self, -1, __doc__, (1,160), (350, 250))
self.button = wxButton(self, 2001, "OK", wxPoint(150,420), wxSize(50,-1)) self.button = wx.Button(self, 2001, "OK", (150,420), (50,-1))
EVT_BUTTON(self, 2001, self.OnOK) self.Fit()
self.button.Bind(wx.EVT_BUTTON, self.OnOK)
def OnOK(self, event): def OnOK(self, event):
self.wnd.Stop() self.wnd.Stop()
self.EndModal(wxID_OK) self.EndModal(wx.ID_OK)
class MyFrame(wxFrame): class MyFrame(wx.Frame):
def __init__(self, parent, wf): def __init__(self, parent, wf):
self.wf = wf self.wf = wf
wxFrame.__init__(self, parent, -1, "hangman", wxDefaultPosition, wxSize(400,300)) wx.Frame.__init__(self, parent, -1, "hangman", wx.DefaultPosition, (400,300))
self.wnd = HangmanWnd(self, -1) self.wnd = HangmanWnd(self, -1)
menu = wxMenu() menu = wx.Menu()
menu.Append(1001, "New") menu.Append(1001, "New")
menu.Append(1002, "End") menu.Append(1002, "End")
menu.AppendSeparator() menu.AppendSeparator()
@@ -289,9 +292,9 @@ class MyFrame(wxFrame):
menu.Append(1004, "Demo...") menu.Append(1004, "Demo...")
menu.AppendSeparator() menu.AppendSeparator()
menu.Append(1005, "Exit") menu.Append(1005, "Exit")
menubar = wxMenuBar() menubar = wx.MenuBar()
menubar.Append(menu, "Game") menubar.Append(menu, "Game")
menu = wxMenu() menu = wx.Menu()
#menu.Append(1010, "Internal", "Use internal dictionary", True) #menu.Append(1010, "Internal", "Use internal dictionary", True)
menu.Append(1011, "ASCII File...") menu.Append(1011, "ASCII File...")
urls = [ 'wxPython home', 'http://wxPython.org/', urls = [ 'wxPython home', 'http://wxPython.org/',
@@ -301,7 +304,7 @@ class MyFrame(wxFrame):
'De Volkskrant', 'http://www.volkskrant.nl/frameless/25000006.html', 'De Volkskrant', 'http://www.volkskrant.nl/frameless/25000006.html',
'Gnu GPL', 'http://www.fsf.org/copyleft/gpl.html', 'Gnu GPL', 'http://www.fsf.org/copyleft/gpl.html',
'Bijbel: Genesis', 'http://www.coas.com/bijbel/gn1.htm'] 'Bijbel: Genesis', 'http://www.coas.com/bijbel/gn1.htm']
urlmenu = wxMenu() urlmenu = wx.Menu()
for item in range(0,len(urls),2): for item in range(0,len(urls),2):
urlmenu.Append(1020+item/2, urls[item], urls[item+1]) urlmenu.Append(1020+item/2, urls[item], urls[item+1])
urlmenu.Append(1080, 'Other...', 'Enter an URL') urlmenu.Append(1080, 'Other...', 'Enter an URL')
@@ -310,22 +313,22 @@ class MyFrame(wxFrame):
menubar.Append(menu, "Dictionary") menubar.Append(menu, "Dictionary")
self.urls = urls self.urls = urls
self.urloffset = 1020 self.urloffset = 1020
menu = wxMenu() menu = wx.Menu()
menu.Append(1090, "About...") menu.Append(1090, "About...")
menubar.Append(menu, "Help") menubar.Append(menu, "Help")
self.SetMenuBar(menubar) self.SetMenuBar(menubar)
self.CreateStatusBar(2) self.CreateStatusBar(2)
EVT_MENU(self, 1001, self.OnGameNew) self.Bind(wx.EVT_MENU, self.OnGameNew, id=1001)
EVT_MENU(self, 1002, self.OnGameEnd) self.Bind(wx.EVT_MENU, self.OnGameEnd, id=1002)
EVT_MENU(self, 1003, self.OnGameReset) self.Bind(wx.EVT_MENU, self.OnGameReset, id=1003)
EVT_MENU(self, 1004, self.OnGameDemo) self.Bind(wx.EVT_MENU, self.OnGameDemo, id=1004)
EVT_MENU(self, 1005, self.OnWindowClose) self.Bind(wx.EVT_MENU, self.OnWindowClose, id=1005)
EVT_MENU(self, 1011, self.OnDictFile) self.Bind(wx.EVT_MENU, self.OnDictFile, id=1011)
EVT_MENU_RANGE(self, 1020, 1020+len(urls)/2, self.OnDictURL) self.Bind(wx.EVT_MENU, self.OnDictURL, id=1020, id2=1020+len(urls)/2)
EVT_MENU(self, 1080, self.OnDictURLSel) self.Bind(wx.EVT_MENU, self.OnDictURLSel, id=1080)
EVT_MENU(self, 1013, self.OnDictDump) self.Bind(wx.EVT_MENU, self.OnDictDump, id=1013)
EVT_MENU(self, 1090, self.OnHelpAbout) self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=1090)
EVT_CHAR(self.wnd, self.OnChar) self.wnd.Bind(wx.EVT_CHAR, self.OnChar)
self.OnGameReset() self.OnGameReset()
def OnGameNew(self, event): def OnGameNew(self, event):
@@ -348,14 +351,14 @@ class MyFrame(wxFrame):
self.OnGameNew(None) self.OnGameNew(None)
def OnGameDemo(self, event): def OnGameDemo(self, event):
frame = HangmanDemoFrame(self.wf, self, -1, wxDefaultPosition, self.GetSize()) frame = HangmanDemoFrame(self.wf, self, -1, wx.DefaultPosition, self.GetSize())
frame.Show(True) frame.Show(True)
def OnDictFile(self, event): def OnDictFile(self, event):
fd = wxFileDialog(self) fd = wx.FileDialog(self)
if (self.wf.filename): if (self.wf.filename):
fd.SetFilename(self.wf.filename) fd.SetFilename(self.wf.filename)
if fd.ShowModal() == wxID_OK: if fd.ShowModal() == wx.ID_OK:
file = fd.GetPath() file = fd.GetPath()
self.wf = WordFetcher(file) self.wf = WordFetcher(file)
@@ -365,8 +368,8 @@ class MyFrame(wxFrame):
self.wf = URLWordFetcher(self.urls[item+1]) self.wf = URLWordFetcher(self.urls[item+1])
def OnDictURLSel(self, event): def OnDictURLSel(self, event):
msg = wxTextEntryDialog(self, "Enter the URL of the dictionary document", "Enter URL") msg = wx.TextEntryDialog(self, "Enter the URL of the dictionary document", "Enter URL")
if msg.ShowModal() == wxID_OK: if msg.ShowModal() == wx.ID_OK:
url = msg.GetValue() url = msg.GetValue()
self.wf = URLWordFetcher(url) self.wf = URLWordFetcher(url)
def OnDictDump(self, event): def OnDictDump(self, event):
@@ -422,11 +425,11 @@ class MyFrame(wxFrame):
class MyApp(wxApp): class MyApp(wx.App):
def OnInit(self): def OnInit(self):
if wxPlatform == '__WXGTK__': if wx.Platform == '__WXGTK__':
defaultfile = "/usr/share/games/hangman-words" defaultfile = "/usr/share/games/hangman-words"
elif wxPlatform == '__WXMSW__': elif wx.Platform == '__WXMSW__':
defaultfile = "c:\\windows\\hardware.txt" defaultfile = "c:\\windows\\hardware.txt"
else: else:
defaultfile = "" defaultfile = ""
@@ -449,9 +452,9 @@ overview = __doc__
def runTest(frame, nb, log): def runTest(frame, nb, log):
if wxPlatform == '__WXGTK__' or wxPlatform == '__WXMOTIF__': if wx.Platform == '__WXGTK__' or wx.Platform == '__WXMOTIF__':
defaultfile = "/usr/share/games/hangman-words" defaultfile = "/usr/share/games/hangman-words"
elif wxPlatform == '__WXMSW__': elif wx.Platform == '__WXMSW__':
defaultfile = "c:\\windows\\hardware.txt" defaultfile = "c:\\windows\\hardware.txt"
else: else:
defaultfile = "" defaultfile = ""

File diff suppressed because it is too large Load Diff