Newbie questions and general code critique requested

Your question is ambigous: Your code works fine and alphanumeric caracters
appears when you press keys so where's the problem ?

The keyboard events don't work on my machine... I should have noted my platform, I'm running Debian Sid on a amd64. I'll give it a shot on a more 'traditional' box and see if it works. Thanks

···

"Valeri" <patrick.valeri@9online.fr> wrote:

-----Message d'origine-----
De : jahurt644@netscape.net [mailto:jahurt644@netscape.net]
Envoyé : lundi 6 septembre 2004 08:15
À : wxPython-users@lists.wxwidgets.org
Objet : [wxPython-users] Newbie questions and general code critique
requested

Hello. I'm a newbie to wxPython and GUI programming in general (although I
have encountered Python before, but I'm by no means an expert). I'm trying
to write a very simple application that does the following...

Initially displays a window containing a button and a text window. The text
window should display numeric keycodes as keys on the keyboard are pressed.
Pressing the button should kill the current form and create another form
with the same controls (the form positions are offset a bit to verify this
is happening). I haven't had any luck with the keyboard event handler, so
I'm likely doing something very fundamentally wrong. Anyway, here's the
code... please keep in mind I'm new to this, so try not to laugh too hard :slight_smile:
Again, any (constructive) criticism is greatly appreciated.

----main.py----
#!/usr/bin/env python

from wxPython.wx import *
import MyFrame1
import MyFrame2

visible = None

class MyApp(wxApp):
   def OnInit(self):
       visible = MyFrame1.MyFrame1(NULL)
       self.SetTopWindow(visible)
       return true

def main():

   app = MyApp(0)
   app.MainLoop()

if __name__=='__main__':
   main()

----MyFrame1.py----
#!/usr/bin/env python

from wxPython.wx import *
import MyFrame2
import main

[ID_FRAME1,
ID_STATICBOX,
ID_BUTTON,
ID_TEXTCTRL] = map(lambda __init__: wxNewId(), range(4))

class MyFrame1(wxFrame):
   def __init__(self, prnt):
       wxFrame.__init__(self,
           id = ID_FRAME1,
           name = 'MyFrame1',
           parent = prnt,
           pos = wxPoint(325, 250),
           size = wxSize(350, 250),
           style = wxDEFAULT_FRAME_STYLE,
           title = 'Frame #1')

       EVT_CHAR(self, self.KeyboardEvent)

       self.Static_Box = wxStaticBox(
           id = ID_STATICBOX,
           label = 'Click to switch frames',
           name = 'Static_Box',
           parent = self,
           pos = wxPoint(25,25),
           size = wxSize(300, 200),
           style = 0)

       self.My_Button = wxButton(
           id = ID_BUTTON,
           label = 'Click Me!',
           name = 'My_Button',
           parent = self,
           pos = wxPoint(100, 82),
           size = wxSize(150, 35),
           style = 0)

       EVT_BUTTON(self, ID_BUTTON, self.ButtonClick)

       self.My_Text = wxTextCtrl(
           id = ID_TEXTCTRL,
           name = 'My_TextCtrl',
           parent = self,
           pos = wxPoint(100, 140),
           size = wxSize(150, 35),
           style = wxTE_CENTER,
           value = '')

       self.Show()

   def ButtonClick(self, event):
       self.KillMe()
       main.visible = MyFrame2.MyFrame2(NULL)
       event.Skip()

   def KeyboardEvent(self, event):
       keycode = event.KeyCode()
       self.My_Text.Clear()
       self.My_Text.WriteText(keycode)
       event.Skip()

   def KillMe(self):
       self.Static_Box.Destroy()
       self.My_Button.Destroy()
       self.My_Text.Destroy()
       self.Destroy()

----MyFrame2.py----
#!/usr/bin/env python

from wxPython.wx import *
import MyFrame1
import main

[ID_FRAME2,
ID_STATICBOX,
ID_BUTTON,
ID_TEXTCTRL] = map(lambda __init__: wxNewId(), range(4))

class MyFrame2(wxFrame):
   def __init__(self, prnt):
       wxFrame.__init__(self,
           id = ID_FRAME2,
           name = 'MyFrame2',
           parent = prnt,
           pos = wxPoint(330, 240),
           size = wxSize(350, 250),
           style = wxDEFAULT_FRAME_STYLE,
           title = 'Frame #2')
       EVT_CHAR(self, self.KeyboardEvent)

       self.Static_Box = wxStaticBox(
           id = ID_STATICBOX,
           label = 'Click to switch frames',
           name = 'Static_Box',
           parent = self,
           pos = wxPoint(25,25),
           size = wxSize(300, 200),
           style = 0)

       self.My_Button = wxButton(
           id = ID_BUTTON,
           label = 'Click Me!',
           name = 'My_Button',
           parent = self,
           pos = wxPoint(100, 82),
           size = wxSize(150, 35),
           style = 0)

       EVT_BUTTON(self, ID_BUTTON, self.ButtonClick)

       self.My_Text = wxTextCtrl(
           id = ID_TEXTCTRL,
           name = 'My_TextCtrl',
           parent = self,
           pos = wxPoint(100, 140),
           size = wxSize(150, 35),
           style = wxTE_CENTER,
           value = '')

       self.Show()

   def ButtonClick(self, event):
       self.KillMe()
       main.visible = MyFrame1.MyFrame1(NULL)
       event.Skip()

   def KeyboardEvent(self, event):
       keycode = event.KeyCode()
       self.My_Text.Clear()
       self.My_Text.WriteText(keycode)
       event.Skip()

   def KillMe(self):
       self.Static_Box.Destroy()
       self.My_Button.Destroy()
       self.My_Text.Destroy()
       self.Destroy()

__________________________________________________________________
Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at AOList Teil der Yahoo Markenfamilie

Netscape. Just the Net You Need.

New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at AOList Teil der Yahoo Markenfamilie

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

__________________________________________________________________
Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at AOList Teil der Yahoo Markenfamilie

Netscape. Just the Net You Need.

New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at AOList Teil der Yahoo Markenfamilie