Starting wxPython (GUI code)

Reply

Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #21
Jun 30th, 2008
Trapping a key event:
  1. # bind keyevent to key down and display the key value
  2.  
  3. import wx
  4.  
  5. class KeyEvent(wx.Frame):
  6. def __init__(self, parent, id, title):
  7. wx.Frame.__init__(self, parent, id, title, size=(400, 70))
  8. self.SetBackgroundColour("yellow")
  9. # create a label
  10. self.label = wx.StaticText(self, wx.ID_ANY, label=" ", pos=(20, 30))
  11.  
  12. self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  13.  
  14. self.Show(True)
  15.  
  16. def OnKeyDown(self, event):
  17. keycode = event.GetKeyCode()
  18. s = "Key value = " + str(keycode)
  19. self.label.SetLabel(s)
  20. if keycode == wx.WXK_ESCAPE:
  21. choice = wx.MessageBox('Are you sure you want to quit? ',
  22. 'Question', wx.YES_NO|wx.CENTRE|wx.NO_DEFAULT, self)
  23. if choice == wx.YES:
  24. self.Close()
  25. event.Skip()
  26.  
  27.  
  28. app = wx.App()
  29. KeyEvent(None, wx.ID_ANY, 'press any key (press escape to exit)')
  30. app.MainLoop()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #22
Jul 3rd, 2008
Sneekula left one small starter editor somewhere around DaniWeb. So I took it and modified it to use the wxPython toolbar with icon images rather than Snee's menu bar. The images come from wxPython's builtin art library:
  1. # the start of one small text editor with toolbar and image icons
  2. # notice that the wx.TextCtrl() surface has already some advanced
  3. # features: select text, right click to cut, copy and paste etc.
  4.  
  5. import os
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, title):
  10. wx.Frame.__init__(self, None, wx.ID_ANY, title, size=(500, 300))
  11. self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
  12.  
  13. # statusBar at the bottom of the window
  14. self.CreateStatusBar()
  15. self.SetStatusText(" Click on the icon")
  16.  
  17. # ToolBar at the top of the window
  18. toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.NO_BORDER)
  19. toolbar.SetToolBitmapSize(size=(24,24))
  20. toolbar.AddSimpleTool(wx.ID_OPEN, self.getBMP(wx.ART_FILE_OPEN),
  21. "Load", " Load a text file")
  22. toolbar.AddSimpleTool(wx.ID_SAVE, self.getBMP(wx.ART_FILE_SAVE),
  23. "Save", " Save the text file")
  24. toolbar.AddSimpleTool(wx.ID_ABOUT, self.getBMP(wx.ART_INFORMATION),
  25. "About"," About message")
  26. toolbar.AddSeparator()
  27. toolbar.AddSimpleTool(wx.ID_EXIT, self.getBMP(wx.ART_QUIT),
  28. "Exit"," Exit the program")
  29. toolbar.Realize()
  30. self.SetToolBar(toolbar)
  31.  
  32. # bind the various toolbar icon click events to some action
  33. self.Bind(wx.EVT_TOOL, self.onLoad, id=wx.ID_OPEN)
  34. self.Bind(wx.EVT_TOOL, self.onSave, id=wx.ID_SAVE)
  35. self.Bind(wx.EVT_TOOL, self.onAbout, id=wx.ID_ABOUT)
  36. self.Bind(wx.EVT_TOOL, self.onExit, id=wx.ID_EXIT)
  37.  
  38. def getBMP(self, pic_id):
  39. """get the bitmap image from the wxPython art provider"""
  40. return wx.ArtProvider.GetBitmap(pic_id, wx.ART_TOOLBAR, wx.Size(24, 24))
  41.  
  42. def onAbout(self, e):
  43. """ the about box """
  44. about = wx.MessageDialog( self, " A very simple text editor \n"
  45. " using the wxPython GUI toolkit", "About Simple Editor", wx.OK)
  46. about.ShowModal()
  47. about.Destroy()
  48.  
  49. def onLoad(self, e):
  50. """ open text file"""
  51. self.dirname = ''
  52. mask = "Text (.txt)|*.txt|All (.*)|*.*"
  53. dlg = wx.FileDialog(self, "Choose a file to load",
  54. self.dirname, "", mask, wx.OPEN)
  55. if dlg.ShowModal() == wx.ID_OK:
  56. self.filename = dlg.GetFilename()
  57. self.dirname = dlg.GetDirectory()
  58. f = open(os.path.join(self.dirname,self.filename),'r')
  59. self.control.SetValue(f.read())
  60. f.close()
  61. dlg.Destroy()
  62.  
  63. def onSave(self, e):
  64. """ Save text file"""
  65. self.dirname = ''
  66. mask = "Text (.txt)|*.txt|All (.*)|*.*"
  67. dlg = wx.FileDialog(self, "Choose or create a file to save to",
  68. self.dirname, self.filename, mask,
  69. wx.OVERWRITE_PROMPT|wx.OPEN)
  70. if dlg.ShowModal() == wx.ID_OK:
  71. self.filename = dlg.GetFilename()
  72. self.dirname = dlg.GetDirectory()
  73. f = open(os.path.join(self.dirname,self.filename),'w')
  74. f.write(self.control.GetValue())
  75. f.close()
  76. dlg.Destroy()
  77.  
  78. def onExit(self, e):
  79. self.Close(True)
  80.  
  81.  
  82. app = wx.App(0)
  83. # create instance of MyFrame and show it
  84. MyFrame(title="A Simple Editor").Show()
  85. app.MainLoop()
Last edited by vegaseat; Jul 31st, 2008 at 11:49 pm. Reason: small blemish, error in onSave
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #23
Jul 9th, 2008
This shows you how to use wxPython's wx.lib.fancytext to show super and subscripted text in a specified font, colour and size. The text coding is XML ...
  1. # wxPython's wx.lib.fancytext can show super and subscripted text
  2. # using XML code
  3.  
  4. import wx
  5. import wx.lib.fancytext as fancytext
  6.  
  7. class FancyText(wx.Panel):
  8. """display fancytext on a panel"""
  9. def __init__(self, parent):
  10. wx.Panel.__init__(self, parent, wx.ID_ANY)
  11. self.Bind(wx.EVT_PAINT, self.OnPaint)
  12.  
  13. def OnPaint(self, evt):
  14. """generate the fancytext on a paint dc canvas"""
  15. dc = wx.PaintDC(self)
  16. fancytext.RenderToDC(xml_str, dc, 0, 20)
  17.  
  18. # the XML code string
  19. xml_str = """\
  20. <font family="swiss" color="blue" size="20">
  21. H<sub>2</sub>O
  22. x<sup>3</sup> + y<sup>2</sup> - 15 = 0
  23. </font>
  24. """
  25.  
  26. app = wx.App(0)
  27. frame = wx.Frame(None, wx.ID_ANY, title='wxPython fancy text',
  28. pos=(100, 50), size=(500, 250))
  29. FancyText(frame)
  30. frame.Show(True)
  31. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #24
Jul 9th, 2008
Dealing with mouse events ...
  1. # get the position of the mouse when clicked or moved
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. """create a color frame, inherits from wx.Frame"""
  7. def __init__(self, parent):
  8. wx.Frame.__init__(self, parent, wx.ID_ANY, "Move or click mouse")
  9. self.SetBackgroundColour('Goldenrod')
  10. # give it a fancier cursor
  11. self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
  12.  
  13. # bind some mouse events
  14. self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  15. self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
  16. self.Bind(wx.EVT_MOTION, self.OnMotion)
  17.  
  18. def OnLeftDown(self, event):
  19. """left mouse button is pressed"""
  20. pt = event.GetPosition() # position tuple
  21. self.SetTitle('LeftMouse click at = ' + str(pt))
  22.  
  23. def OnRightDown(self, event):
  24. """right mouse button is pressed"""
  25. pt = event.GetPosition()
  26. self.SetTitle('RightMouse click at = ' + str(pt))
  27.  
  28. def OnMotion(self, event):
  29. """mouse in motion"""
  30. pt = event.GetPosition()
  31. self.SetTitle('Mouse in motion at = ' + str(pt))
  32.  
  33.  
  34. app = wx.App(0)
  35. MyFrame(None).Show()
  36. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #25
Jul 9th, 2008
Applying different fonts to a text display ...
  1. # test wx.Font() and wx.FontDialog on a given text
  2. # wx.Font(pointSize, family, style, weight, underline=false, faceName="",
  3. # encoding=wx.FONTENCODING_DEFAULT)
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent, title, data):
  9. wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(600, 400))
  10. panel = wx.Panel(self, wx.ID_ANY)
  11. button = wx.Button(panel, wx.ID_ANY, label='Change Font',
  12. pos=(3, 3))
  13. button.Bind(wx.EVT_BUTTON, self.changeFont)
  14.  
  15. # family: wx.DEFAULT, wx.DECORATIVE, wx.ROMAN, wx.SCRIPT,
  16. # wx.SWISS, wx.MODERN
  17. # style: wx.NORMAL, wx.SLANT or wx.ITALIC
  18. # weight: wx.NORMAL, wx.LIGHT or wx.BOLD
  19. font = wx.Font(16, wx.SCRIPT, wx.NORMAL, wx.LIGHT)
  20. # use additional fonts this way ...
  21. #face = u'Comic Sans MS'
  22. #font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False, face)
  23.  
  24. self.text = wx.StaticText(panel, wx.ID_ANY, data, pos=(10,35))
  25. self.text.SetFont(font)
  26.  
  27. def changeFont(self, event):
  28. dialog = wx.FontDialog(None, wx.FontData())
  29. if dialog.ShowModal() == wx.ID_OK:
  30. data = dialog.GetFontData()
  31. font = data.GetChosenFont()
  32. self.text.SetForegroundColour(data.GetColour())
  33. self.text.SetFont(font)
  34. dialog.Destroy()
  35.  
  36.  
  37. data = """\
  38. Al Gore: The Wild Years
  39. America's Most Popular Lawyers
  40. Career Opportunities for History Majors
  41. Different Ways to Spell "Bob"
  42. Ethiopian Tips on World Dominance
  43. Everything Men Know About Women
  44. Everything Women Know About Men
  45. Staple Your Way to Success
  46. The Amish Phone Book
  47. The Engineer's Guide to Fashion
  48. Ralph Nader's list of pleasures"""
  49.  
  50. app = wx.App(0)
  51. # create instance of MyFrame and show
  52. MyFrame(None, "Text and Font", data).Show()
  53. # start the event loop
  54. app.MainLoop()
Last edited by vegaseat; Jul 10th, 2008 at 9:50 am. Reason: more detail added
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #26
Jul 12th, 2008
You can use wxPython's ImageDialog to preview the image to be loaded. Also use a scrolled window to display oversized images ...
  1. # test the wx.lib.imagebrowser.ImageDialog()
  2. # and display the loaded image on a scrolled window
  3.  
  4. import wx
  5. import os
  6. import wx.lib.imagebrowser
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=(600,400),
  11. style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
  12.  
  13. # create a srolled window to put the image on
  14. self.scrollw = wx.ScrolledWindow(self, wx.ID_ANY)
  15. self.scrollw.SetBackgroundColour('green')
  16. # set EnableScrolling(bool x_scrolling, bool y_scrolling)
  17. self.scrollw.EnableScrolling(True, True)
  18. # create the scroll bars, set max width and height
  19. max_width = 1000
  20. max_height = 1000
  21. # SetScrollbars(pixelsPerUnitX, pixelsPerUnitY, noUnitsX, noUnitsY)
  22. self.scrollw.SetScrollbars(20, 20, max_width/20, max_height/20)
  23.  
  24. # create a statusbar at the bottom of the frame
  25. self.CreateStatusBar()
  26.  
  27. # create the menubar at the top of the frame
  28. menubar = wx.MenuBar()
  29. # setting up the menu
  30. filemenu = wx.Menu()
  31. # alt/o is hotkey, "Open file" shows up in statusbar
  32. filemenu.Append(wx.ID_OPEN, "&Open","Open image file")
  33. filemenu.AppendSeparator()
  34. # alt/x is hotkey
  35. filemenu.Append(wx.ID_EXIT,"E&xit","Exit program")
  36. # add the filemenu to the menubar
  37. menubar.Append(filemenu,"&File")
  38. # add the finished menubar to the frame/window
  39. self.SetMenuBar(menubar)
  40.  
  41. # bind event to an action
  42. self.Bind(wx.EVT_MENU, self.onExit, id=wx.ID_EXIT)
  43. self.Bind(wx.EVT_MENU, self.onOpen, id=wx.ID_OPEN)
  44.  
  45. def onExit(self,event):
  46. """close the frame"""
  47. self.Close(True)
  48.  
  49. def onOpen(self,event):
  50. """open an image file via wx.lib.imagebrowser.ImageDialog()"""
  51. dirname = ''
  52. dialog = wx.lib.imagebrowser.ImageDialog(self, dirname)
  53. if dialog.ShowModal() == wx.ID_OK:
  54. filename = dialog.GetFile()
  55. image = wx.Bitmap(filename)
  56. self.SetStatusText(filename)
  57. # bitmap upper left corner is in position (x, y) = (5, 5)
  58. wx.StaticBitmap(self.scrollw, wx.ID_ANY, image, pos=(5, 5),
  59. size=(image.GetWidth(), image.GetHeight()))
  60. dialog.Destroy()
  61.  
  62.  
  63. app = wx.App(0)
  64. # create MyFrame instance and show the frame
  65. MyFrame(None, "Test wx.lib.imagebrowser.ImageDialog()").Show()
  66. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #27
Jul 12th, 2008
Ever wanted to know how many named colours wxPython has? Here is a short demo code to find out ...
  1. # test the wx.Choice() widget and wx.lib.colourdb
  2. # wx.Choice(parent, id, pos, size, choices, style)
  3. # has no style options
  4.  
  5. import wx
  6. import wx.lib.colourdb as colourdb
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  11. # create a panel to show the selected colour
  12. self.panel = wx.Panel(self, wx.ID_ANY, pos=(0,40), size=(250, 130))
  13.  
  14. # create a sorted colour list from the wx colour data base
  15. colourdb.updateColourDB()
  16. colour_list = sorted(colourdb.getColourList())
  17. # create a choice widget
  18. self.choice = wx.Choice(self, wx.ID_ANY, choices=colour_list)
  19. # select item 0 (first item) in sorted colour list
  20. self.choice.SetSelection(0)
  21. # set the current frame color to the choice
  22. self.SetBackgroundColour(self.choice.GetStringSelection())
  23. # bind the checkbox events to an action
  24. self.choice.Bind(wx.EVT_CHOICE, self.onChoice)
  25.  
  26. def onChoice(self, event):
  27. bgcolour = self.choice.GetStringSelection()
  28. # change colour of the panel to the selected colour ...
  29. self.panel.SetBackgroundColour(bgcolour)
  30. self.panel.Refresh()
  31. # show the selected color in the frame title
  32. self.SetTitle(bgcolour.lower())
  33.  
  34.  
  35. app = wx.App(0)
  36. # create a MyFrame instance and show
  37. MyFrame(None, 'Select a colour', (250, 170)).Show()
  38. app.MainLoop()
Might as well get used to the British spelling of colour!
I left some US spellings in there for you to find.
Last edited by vegaseat; Jul 12th, 2008 at 5:06 pm. Reason: colour
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #28
Jul 13th, 2008
The wx.RadioBox() widget is just a convenient way to group a bunch of radio buttons and give them a title. Radio buttons are used if you want only one of the group selected. Here is an example ...
  1. # test the wx.RadioBox() widget
  2. # wx.RadioBox(parent, id, label, pos, size, choices, style)
  3. # combines a wx.StaticBox() with wx.RadioButton()
  4. # only one radiobutton can be selected
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  11. self.SetBackgroundColour("yellow")
  12.  
  13. self.options = ['now', 'later', 'much later', 'never']
  14. # create an input widget
  15. self.radiobox = wx.RadioBox(self, wx.ID_ANY, "Select one option",
  16. pos=(10, 10), choices=self.options, style=wx.VERTICAL)
  17. # set radio button 1 as selected (first button is 0)
  18. self.radiobox.SetSelection(1)
  19. # bind mouse click to an action
  20. self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)
  21. # create an output widget
  22. self.label = wx.StaticText(self, wx.ID_ANY, "" , pos=(10, 120))
  23. # show present selection
  24. self.onAction(None)
  25.  
  26. def onAction(self, event):
  27. """ some action code"""
  28. #index = self.radiobox.GetSelection()
  29. #s = "You selected option " + self.options[index]
  30. # better ...
  31. s = "You selected option " + self.radiobox.GetStringSelection()
  32. self.label.SetLabel(s)
  33.  
  34.  
  35. app = wx.App(0)
  36. # create a MyFrame instance and show the frame
  37. MyFrame(None, 'testing wx.RadioBox()', (300, 200)).Show()
  38. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #29
Jul 13th, 2008
The wx.SpinCtrl() is a graphical way to enter integers. You can click on the up or down arrows to change the value, or use the keyboard to type the value directly, once the spinbox has the focus. Just run the code to see what it does ...
  1. # test the wx.SpinCtrl() widget
  2. # wx.SpinCtrl(parent, id, value, pos, size, style, min, max, initial)
  3. # used for integer number input
  4. # style =
  5. # wx.SP_ARROW_KEYS can use arrow keys to change the value
  6. # wx.SP_WRAP value wraps at the minimum and maximum.
  7.  
  8. import wx
  9.  
  10. class MyFrame(wx.Frame):
  11. def __init__(self, parent, mytitle, mysize):
  12. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  13. self.SetBackgroundColour("yellow")
  14.  
  15. # create a spinctrl widget for inteer input
  16. self.spin = wx.SpinCtrl( self, wx.ID_ANY, value="",
  17. pos=(10, 20), size=(80, 25), min=-100, max=1000,
  18. initial=98, style=wx.SP_ARROW_KEYS)
  19. # bind mouse click on arrows to an action
  20. self.spin.Bind(wx.EVT_SPINCTRL, self.onAction)
  21. # you can edit the value directly
  22. self.spin.Bind(wx.EVT_TEXT, self.onAction)
  23.  
  24. # create an output widget
  25. s1 = "click on arrows to change the spinbox value \n"
  26. s2 = "or type the integer value directly"
  27. self.label = wx.StaticText(self, wx.ID_ANY, s1+s2, pos=(10, 60))
  28.  
  29. def onAction(self, event):
  30. """ some action code"""
  31. val = self.spin.GetValue()
  32. f = str(round(val * 9.0/5 + 32, 2))
  33. c = str(round((val - 32)*5/9.0, 2))
  34. v = str(val)
  35. s1 = v + " degree Fahrenheit is " + c + " degree Celcius \n"
  36. s2 = v + " degree Celcius is " + f + " degree Fahrenheit"
  37. self.label.SetLabel(s1 + s2)
  38.  
  39.  
  40. app = wx.App(0)
  41. # create a MyFrame instance and show the frame
  42. MyFrame(None, 'testing the wx.SpinCtrl()', (300, 150)).Show()
  43. app.MainLoop()
Last edited by vegaseat; Jul 13th, 2008 at 3:21 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 174
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Starting wxPython (GUI code)

 
1
  #30
Jul 15th, 2008
You can input a date the graphical way, let's call it the wxPythonian way, with a simple mouse point and click:
  1. # explore the wx.calendar.CalendarCtrl() control
  2. # allows for point and click date input
  3.  
  4. import wx
  5. import wx.calendar as cal
  6.  
  7. class MyCalendar(wx.Dialog):
  8. """create a simple dialog window with a calendar display"""
  9. def __init__(self, parent, mytitle):
  10. wx.Dialog.__init__(self, parent, wx.ID_ANY, mytitle)
  11. # use a box sizer to position controls vertically
  12. vbox = wx.BoxSizer(wx.VERTICAL)
  13.  
  14. # wx.DateTime_Now() sets calendar to current date
  15. self.calendar = cal.CalendarCtrl(self, wx.ID_ANY, wx.DateTime_Now())
  16. vbox.Add(self.calendar, 0, wx.EXPAND|wx.ALL, border=20)
  17. # click on day
  18. self.calendar.Bind(cal.EVT_CALENDAR_DAY, self.onCalSelected)
  19. # change month
  20. self.calendar.Bind(cal.EVT_CALENDAR_MONTH, self.onCalSelected)
  21. # change year
  22. self.calendar.Bind(cal.EVT_CALENDAR_YEAR, self.onCalSelected)
  23.  
  24. self.label = wx.StaticText(self, wx.ID_ANY, 'click on a day')
  25. vbox.Add(self.label, 0, wx.EXPAND|wx.ALL, border=20)
  26.  
  27. button = wx.Button(self, wx.ID_ANY, 'Exit')
  28. vbox.Add(button, 0, wx.ALL|wx.ALIGN_CENTER, border=20)
  29. self.Bind(wx.EVT_BUTTON, self.onQuit, button)
  30.  
  31. self.SetSizerAndFit(vbox)
  32. self.Show(True)
  33. self.Centre()
  34.  
  35. def onCalSelected(self, event):
  36. #date = event.GetDate()
  37. date = self.calendar.GetDate()
  38. day = date.GetDay()
  39. # for some strange reason month starts with zero
  40. month = date.GetMonth() + 1
  41. # year is yyyy format
  42. year = date.GetYear()
  43. s1 = "%02d/%02d/%d \n" % (month, day, year)
  44. # or just take a slice of the first 8 characters to show mm/dd/yy
  45. s2 = str(date)[0:8]
  46. self.label.SetLabel(s1 + s2)
  47.  
  48. def onQuit(self, event):
  49. self.Destroy()
  50.  
  51.  
  52. app = wx.App()
  53. MyCalendar(None, 'wx.calendar.CalendarCtrl()')
  54. app.MainLoop()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC