[Assorted-commits] SF.net SVN: assorted:[1050] sandbox/trunk/src/py/wxtest.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-11-02 19:02:56
|
Revision: 1050 http://assorted.svn.sourceforge.net/assorted/?rev=1050&view=rev Author: yangzhang Date: 2008-11-02 19:02:52 +0000 (Sun, 02 Nov 2008) Log Message: ----------- added a simple demo of using wx Added Paths: ----------- sandbox/trunk/src/py/wxtest.py Added: sandbox/trunk/src/py/wxtest.py =================================================================== --- sandbox/trunk/src/py/wxtest.py (rev 0) +++ sandbox/trunk/src/py/wxtest.py 2008-11-02 19:02:52 UTC (rev 1050) @@ -0,0 +1,95 @@ +import wx + +class ModeFrame(wx.Frame): + def __init__(self): + wx.Frame.__init__(self, parent = None, title = '6.00 Word Game') + buttons = {wx.Button(self, label = '&Solo Game'): self.OnSolo, + wx.Button(self, label = 'Vs. &Computer'): self.OnVsComp, + wx.Button(self, label = 'Vs. &Human'): self.OnVsHuman} + hbox = wx.BoxSizer(wx.HORIZONTAL) + for button, handler in buttons.iteritems(): + button.Bind(wx.EVT_BUTTON, handler) + hbox.Add(button, 1, wx.EXPAND | wx.ALL) + self.SetSizer(hbox) + self.Center() + def OnSolo(self, event): + frame = PlayFrame('solo') + frame.Show() + def OnVsComp(self, event): + frame = PlayFrame('vs comp') + frame.Show() + def OnVsHuman(self, event): + frame = PlayFrame('vs human') + frame.Show() + +class InteractionPanel(wx.Panel): + def __init__(self, parent): + wx.Panel.__init__(self, parent, style = wx.BORDER_SUNKEN) + # play panel + playPanel = wx.Panel(self) + try: + # entry panel + entryPanel = wx.Panel(playPanel) + submitButton = wx.Button(entryPanel, label = 'Enter!') + submitButton.SetDefault() + self.inputBox = wx.TextCtrl(entryPanel) + inputBoxLabel = wx.StaticText(entryPanel, label = '&Enter word:') + # TODO wire focusing on inputBoxLabel to focusing on inputBox + hbox = wx.BoxSizer(wx.HORIZONTAL) + hbox.Add(inputBoxLabel, 0, wx.ALIGN_CENTER_VERTICAL) + hbox.Add(self.inputBox, 1, wx.EXPAND) + hbox.Add(submitButton, 0, wx.EXPAND) + submitButton.Bind(wx.EVT_BUTTON, self.OnEnter) + entryPanel.SetSizer(hbox) + except: pass + historyLabel = wx.StaticText(playPanel, label = 'Previously entered words:') + self.history = wx.ListBox(playPanel) + vbox = wx.BoxSizer(wx.VERTICAL) + vbox.Add(historyLabel, 0, wx.EXPAND) + vbox.Add(self.history, 1, wx.EXPAND) + vbox.Add(entryPanel, 0, wx.EXPAND) + playPanel.SetSizer(vbox) + # rest + sizer = wx.FlexGridSizer(rows = 2, cols = 3) + empty = lambda: wx.StaticText(self, label = '') + self.stats = [None,None] + self.stats[0] = wx.StaticText(self, label = 'Player 1', style = wx.ALIGN_CENTER, size = (100, -1)) + self.stats[1] = wx.StaticText(self, label = 'Player 2', style = wx.ALIGN_CENTER, size = (100, -1)) + cells = [ self.stats[0], empty(), self.stats[1], empty(), playPanel, empty() ] + sizer.AddGrowableRow(1) + sizer.AddGrowableCol(1) + sizer.AddMany( [ (cell, 1, wx.EXPAND) for cell in cells ] ) + self.SetSizer(sizer) + def OnEnter(self, event): + is_valid_word = lambda *args: True + self.hand = None + word = self.inputBox.GetValue() + if is_valid_word(word, self.hand): + self.history.Insert(word, 0) + self.stats[0].SetLabel('Player 1\n\nScore: 5') + self.GetGrandParent().statusBar.SetStatusText('yay!') + else: + self.GetGrandParent().statusBar.SetStatusText('nay.') + self.inputBox.Clear() + +class PlayFrame(wx.Frame): + def __init__(self, mode): + wx.Frame.__init__(self, parent = None, title = '6.00 Word Game') + masterPanel = wx.Panel(self) + interactionPanel= InteractionPanel(masterPanel) + self.statusBar = wx.StatusBar(masterPanel) + self.statusBar.SetStatusText('This is the status bar.') + vbox = wx.BoxSizer(wx.VERTICAL) + vbox.Add(interactionPanel, 1, wx.EXPAND | wx.ALL, 5) + vbox.Add(self.statusBar, 0, wx.EXPAND | wx.ALL, 5) + masterPanel.SetSizer(vbox) + self.SetMinSize((450, 200)) + self.Center() + +if __name__ == '__main__': + app = wx.App() + frame = ModeFrame() + frame.Show() + app.MainLoop() + +# vim:et:sw=4:ts=4 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |