Starting wxPython (GUI code)

Reply

Join Date: Oct 2004
Posts: 3,856
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: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #41
Jul 21st, 2008
Just a quick look at wx.Timer(), wxPython's own timer action. There are two options a one_shot type and a continuous restart type action. Let's take a look at each ...
  1. # a wx.Timer() one_shot example (delays for a set time) ...
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  8. self.SetBackgroundColour('red')
  9.  
  10. self.timer = wx.Timer(self)
  11. # interval = 2000ms (2 seconds)
  12. # default is oneShot=False, timer keeps restarting
  13. self.timer.Start(2000, oneShot=True)
  14. # bind EVT_TIMER event to self.onTimer() action
  15. self.Bind(wx.EVT_TIMER, self.onTimer)
  16.  
  17. def onTimer(self, event):
  18. # establish new color
  19. self.SetBackgroundColour('green')
  20. # clear old color, set to new color
  21. self.ClearBackground()
  22.  
  23.  
  24. app = wx.App(0)
  25. # create a MyFrame instance and show the frame
  26. MyFrame(None, 'change colour from red to green after 2 seconds',
  27. (640, 180)).Show()
  28. app.MainLoop()
... and ...
  1. # a wx.Timer() continuous restart example ...
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  8. self.SetBackgroundColour('red')
  9. self.red_flag = True
  10.  
  11. self.timer = wx.Timer(self)
  12. # interval = 2000ms (2 seconds)
  13. # default is oneShot=False, timer keeps restarting
  14. # stop the timer action with self.timer.Stop()
  15. self.timer.Start(2000, oneShot=False)
  16. # bind EVT_TIMER event to self.onTimer() action
  17. self.Bind(wx.EVT_TIMER, self.onTimer)
  18.  
  19. def onTimer(self, event):
  20. if self.red_flag:
  21. # establish new color
  22. self.SetBackgroundColour('green')
  23. self.red_flag = False
  24. else:
  25. self.SetBackgroundColour('red')
  26. self.red_flag = True
  27. # clear old color, set to new color
  28. self.ClearBackground()
  29.  
  30.  
  31. app = wx.App(0)
  32. # create a MyFrame instance and show the frame
  33. MyFrame(None, 'flip colour from red to green every 2 seconds',
  34. (640, 180)).Show()
  35. app.MainLoop()
Last edited by vegaseat; Jul 21st, 2008 at 6:32 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Starting wxPython (GUI code)

 
1
  #42
Jul 22nd, 2008
This short code sample shows how to add an exit confirm to your wxPython program, from either a menu exit or the title bar exit:
  1. # confirm exit of a wxPython program
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  8.  
  9. # create a status bar at the bottom
  10. self.CreateStatusBar()
  11. self.SetStatusText("Click on File")
  12.  
  13. menu = wx.Menu()
  14. # just one item to test exit confirm
  15. # the optional & allows you to use alt/x
  16. # the last string argument shows in the status bar on mouse_over
  17. menu_exit = menu.Append(wx.ID_ANY, "E&xit", "Exit the program")
  18.  
  19. # create a menu bar at the top
  20. menuBar = wx.MenuBar()
  21. # the & allows you to use alt/f
  22. menuBar.Append(menu, "&File")
  23. self.SetMenuBar(menuBar)
  24.  
  25. # bind the menu events to an action
  26. self.Bind(wx.EVT_MENU, self.onMenuExit, menu_exit)
  27. # responds to exit symbol x on frame title bar
  28. self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
  29.  
  30. def onMenuExit(self, event):
  31. # triggers wx.EVT_CLOSE event and hence onCloseWindow()
  32. self.Close(True)
  33.  
  34. def onCloseWindow(self, event):
  35. # dialog to verify exit
  36. dlg = wx.MessageDialog(self, "Want to exit?", "Exit",
  37. wx.YES_NO|wx.ICON_QUESTION)
  38. if dlg.ShowModal() == wx.ID_YES:
  39. self.Destroy()
  40. dlg.Destroy()
  41.  
  42.  
  43. app = wx.App(0)
  44. # create the MyFrame instance and then show the frame
  45. MyFrame(None, 'Confirm exit', (400, 300)).Show()
  46. app.MainLoop()
Zoe
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
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: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #43
Jul 23rd, 2008
The wx.TextCtrl() widget is used to enter single line text, password text, or multiline text. It can also be used to display multline text as a scrolled output widget, editable (default) or readonly. Under Windows you can also output text in the richedit format. Here is a short example ...
  1. # testing wxPython's
  2. # wx.TextCtrl(parent, id, value, pos, size, style)
  3. # a widget used for written text data input or output
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent, mytitle, mysize):
  9. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  10. self.SetBackgroundColour("yellow")
  11.  
  12. s = "Enter your name below:"
  13. edit_label = wx.StaticText(self, wx.ID_ANY, s)
  14. self.edit = wx.TextCtrl(self, wx.ID_ANY, value="",
  15. size=(200, 20))
  16. # respond to enter key when focus is on edit1
  17. self.edit.Bind(wx.EVT_TEXT_ENTER, self.onAction)
  18.  
  19. s = "Enter your password below:"
  20. pw_label = wx.StaticText(self, wx.ID_ANY, s)
  21. self.pw = wx.TextCtrl(self, wx.ID_ANY, value="",
  22. size=(200, 20), style=wx.TE_PASSWORD)
  23. self.pw.SetBackgroundColour("green")
  24. # respond to enter key when focus is on edit1
  25. self.pw.Bind(wx.EVT_TEXT_ENTER, self.onAction)
  26.  
  27. # create another text control for scrolled multiline output
  28. self.text_out = wx.TextCtrl(self, wx.ID_ANY, value="",
  29. size=(200, 70),
  30. style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
  31.  
  32. sizer_v = wx.BoxSizer(wx.VERTICAL)
  33. sizer_v.Add(edit_label, 0, flag=wx.LEFT|wx.TOP|wx.EXPAND, border=10)
  34. sizer_v.Add(self.edit, 0, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND,
  35. border=10)
  36. sizer_v.Add(pw_label, 0, flag=wx.LEFT|wx.TOP|wx.EXPAND, border=10)
  37. sizer_v.Add(self.pw, 0, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND,
  38. border=10)
  39. sizer_v.Add(self.text_out, 0, flag=wx.ALL|wx.EXPAND, border=10)
  40.  
  41. self.edit.SetFocus()
  42.  
  43. self.SetSizer(sizer_v)
  44.  
  45. def onAction(self, event):
  46. """ some action code"""
  47. name = self.edit.GetValue()
  48. s1 = "You entered the name: " + name + '\n'
  49. pw = self.pw.GetValue()
  50. if pw:
  51. s2 = "Your entered password: " + pw + '\n'
  52. s3 = "..... done!"
  53. else:
  54. s2 = s3 = ""
  55. self.pw.SetFocus()
  56. # text output to first, second and third line
  57. # play with this to your satisfaction
  58. self.text_out.ChangeValue(s1)
  59. self.text_out.AppendText(s2)
  60. self.text_out.AppendText(s3)
  61.  
  62.  
  63. app = wx.App(0)
  64. # create a MyFrame instance and show the frame
  65. MyFrame(None, 'testing wx.TextCtrl() part1', (300, 250)).Show()
  66. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
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: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #44
Jul 25th, 2008
Just a few experiments with wxPython's label widget. It goes under the name of wx.StaticText ...
  1. # experiment with wxPython's
  2. # wx.StaticText(parent, id, label, pos, size, style)
  3. # used for labels to display information or results
  4. """
  5. style:
  6. wx.ALIGN_LEFT Align the text to the left (default)
  7. wx.ALIGN_RIGHT Align the text to the right
  8. wx.ALIGN_CENTRE Center the text (horizontally)
  9. wx.ST_NO_AUTORESIZE Cancel the default of autosizing label to fit text
  10. """
  11.  
  12. import wx
  13.  
  14. class MyFrame(wx.Frame):
  15. def __init__(self, parent, mytitle, mysize):
  16. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  17.  
  18. # note: label will try to autosize to fit text
  19. text = "What time does the two o'clock bus leave?"
  20. label1 = wx.StaticText(self, wx.ID_ANY, text)
  21.  
  22. # set background and text colour of label1
  23. label1.SetBackgroundColour("yellow")
  24. label1.SetForegroundColour("red")
  25.  
  26. # note: text that does not fit on one line will wrap
  27. label2 = wx.StaticText(self, wx.ID_ANY, text)
  28.  
  29. # set background and text colour label2
  30. label2.SetBackgroundColour("blue")
  31. label2.SetForegroundColour("yellow")
  32.  
  33. # set the font of label2
  34. # type: wxDEFAULT, wxDECORATIVE, wxROMAN, wxSCRIPT, wxSWISS, wxMODERN
  35. # slant: wxNORMAL, wxSLANT or wxITALIC
  36. # weight: wxNORMAL, wxLIGHT or wxBOLD
  37. label2.SetFont(wx.Font(22, wx.MODERN, wx.SLANT, wx.BOLD))
  38.  
  39. label3 = wx.StaticText(self, wx.ID_ANY, "", style=wx.ALIGN_CENTRE)
  40. # set the background colour
  41. # text colour defaults to black
  42. label3.SetBackgroundColour("white")
  43. # set text for label3 to label2's text
  44. # does not copy colours or font
  45. label3.SetLabel(label2.GetLabel())
  46.  
  47. label4 = wx.StaticText(self, wx.ID_ANY, text, style=wx.ALIGN_RIGHT)
  48. # set the foreground/text colour
  49. # background defaults to system setting
  50. label4.SetForegroundColour("white")
  51.  
  52. # use a box sizer to position the labels vertically
  53. sizer_v = wx.BoxSizer(wx.VERTICAL)
  54. # label1 and label2 proportions are set to fill space ratio 1:2
  55. # also keeps ratio on frame stretch
  56. sizer_v.Add(label1, proportion=1, flag=wx.EXPAND)
  57. sizer_v.Add(label2, proportion=2, flag=wx.EXPAND)
  58. sizer_v.Add(label3, proportion=0, flag=wx.EXPAND)
  59. sizer_v.Add(label4, proportion=0, flag=wx.EXPAND)
  60. self.SetSizer(sizer_v)
  61.  
  62.  
  63. app = wx.App(0)
  64. # create a MyFrame instance and show the frame
  65. MyFrame(None, 'test wx.StaticText()', (400, 300)).Show()
  66. app.MainLoop()
Last edited by vegaseat; Jul 25th, 2008 at 3:50 pm.
May 'the Google' be with you!
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
  #45
Jul 27th, 2008
Shows you how to drag one of wxPython's widgets with the mouse pointer:
  1. # use mouse to drag the wx.Panel() widget
  2. # wx.Panel(parent, id, pos, size, style)
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent, mytitle, mysize):
  8. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  9. # needed for better control
  10. panel = wx.Panel(self)
  11. panel.SetBackgroundColour("green")
  12.  
  13. self.drag_panel = wx.Panel(panel, size=(230, 100))
  14. self.drag_panel.SetBackgroundColour("yellow")
  15. text1 = "point the mouse on the yellow panel \n"
  16. text2 = "press the left mouse button and drag it"
  17. wx.StaticText(panel, wx.ID_ANY,
  18. text1+text2, pos=(10, 150))
  19.  
  20. # bind mouse events to some actions
  21. self.drag_panel.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
  22. self.drag_panel.Bind(wx.EVT_LEFT_UP, self.on_left_up)
  23. self.drag_panel.Bind(wx.EVT_MOTION, self.on_motion)
  24.  
  25. def on_left_down(self, event):
  26. self.drag_panel.CaptureMouse()
  27. x, y = self.drag_panel.ClientToScreen(event.GetPosition())
  28. originx, originy = self.drag_panel.GetPosition()
  29. dx = x - originx
  30. dy = y - originy
  31. self.mp_delta = ((dx, dy))
  32.  
  33. def on_left_up(self, event):
  34. if self.drag_panel.HasCapture():
  35. self.drag_panel.ReleaseMouse()
  36.  
  37. def on_motion(self, event):
  38. if event.Dragging() and event.LeftIsDown():
  39. x, y = self.drag_panel.ClientToScreen(event.GetPosition())
  40. move_xy = (x - self.mp_delta[0], y - self.mp_delta[1])
  41. self.drag_panel.Move(move_xy)
  42.  
  43.  
  44. app = wx.App()
  45. # create the MyFrame instance and show the frame
  46. MyFrame(None, 'drag the panel with the mouse', (400, 300)).Show()
  47. app.MainLoop()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
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
  #46
Jul 29th, 2008
The information a statusbar can display can come handy:
  1. # set up a customized statusbar with three display fields
  2. # a statusbar is typically an information line at the bottom of a window
  3.  
  4. import wx
  5. import time
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent, mytitle, mysize):
  9. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  10. # fill the top part with a panel
  11. panel = wx.Panel(self, wx.ID_ANY, style=wx.SUNKEN_BORDER)
  12. panel.SetBackgroundColour("blue")
  13.  
  14. self.sb = wx.StatusBar(self, wx.ID_ANY)
  15. # set the status bar with three fields
  16. self.sb.SetFieldsCount(3)
  17. # set an absolute status field width in pixels
  18. # (negative indicates a variable width field)
  19. self.sb.SetStatusWidths([-1, -1, -1])
  20. self.SetStatusBar(self.sb)
  21. # put some text into field 0 (most left field)
  22. self.sb.SetStatusText("some text in field 0", 0)
  23. self.sb.SetStatusText("some text in field 1", 1)
  24.  
  25. # use a timer to drive a date/time string in field 3
  26. # here the most right field of the statusbar
  27. self.timer = wx.PyTimer(self.onUpdate)
  28. # update every 1000 milliseconds
  29. self.timer.Start(1000)
  30. self.onUpdate()
  31.  
  32. def onUpdate(self):
  33. t = time.localtime(time.time())
  34. st = time.strftime("%d-%b-%Y %I:%M:%S", t)
  35. # put date/time display string into field 2
  36. self.sb.SetStatusText(st, 2)
  37.  
  38.  
  39. app = wx.App(0)
  40. # create a MyFrame instance and show the frame
  41. MyFrame(None, 'test the wx.StatusBar', (460, 300)).Show()
  42. app.MainLoop()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
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: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #47
Jul 31st, 2008
The wx.media.MediaCtrl() is wxPython's answer to playing a fair number of sound and video file formats. Here is an example of how to use this versatile widget ...
  1. # experiment with wxPython's
  2. # wx.media.MediaCtrl(parent, id, pos, size, style, szBackend)
  3. # the backend is usually figured out by the control itself
  4. # wx.MEDIABACKEND_DIRECTSHOW Windows
  5. # wx.MEDIABACKEND_QUICKTIME Mac OS X
  6. # wx.MEDIABACKEND_GSTREAMER Linux
  7.  
  8. import wx
  9. import wx.media
  10. import os
  11.  
  12. class MyFrame(wx.Frame):
  13. def __init__(self, parent, mytitle, mysize):
  14. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  15. self.SetBackgroundColour("green")
  16.  
  17. # check if the media control is implemented
  18. try:
  19. self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
  20. except NotImplementedError:
  21. self.Destroy()
  22. raise
  23.  
  24. loadButton = wx.Button(self, wx.ID_ANY, "Load File")
  25. self.Bind(wx.EVT_BUTTON, self.onLoadFile, loadButton)
  26.  
  27. playButton = wx.Button(self, wx.ID_ANY, "Play")
  28. playButton.SetToolTip(wx.ToolTip("load a file first"))
  29. self.Bind(wx.EVT_BUTTON, self.onPlay, playButton)
  30.  
  31. pauseButton = wx.Button(self, wx.ID_ANY, "Pause")
  32. pauseButton.SetToolTip(wx.ToolTip("press Play to resume"))
  33. self.Bind(wx.EVT_BUTTON, self.onPause, pauseButton)
  34.  
  35. stopButton = wx.Button(self, wx.ID_ANY, "Stop")
  36. stopButton.SetToolTip(wx.ToolTip("also resets to start"))
  37. self.Bind(wx.EVT_BUTTON, self.onStop, stopButton)
  38.  
  39. self.slider = wx.Slider(self, wx.ID_ANY, 1000000, 0, 1000000,
  40. size=(380, -1),
  41. style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
  42. self.slider.Bind(wx.EVT_SLIDER, self.onSeek)
  43.  
  44. ext = " load .mp3 .mpg .mid .wav .au or .avi files"
  45. self.st_info = wx.StaticText(self, wx.ID_ANY, ext, size=(300,-1))
  46.  
  47. # use a gridbagsizer to layout widgets
  48. sizer = wx.GridBagSizer(vgap=5, hgap=5)
  49. # pos=(row, column)
  50. # span=(rowspan, columnspan)
  51. sizer.Add(loadButton, pos=(1,1))
  52. sizer.Add(self.st_info, pos=(1,2), span=(1,2))
  53. sizer.Add(self.slider, pos=(2,1), span=(2,3))
  54. sizer.Add(playButton, pos=(4,1))
  55. sizer.Add(pauseButton, pos=(4,2))
  56. sizer.Add(stopButton, pos=(4,3))
  57. # for .avi .mpg video files use this lower grid area
  58. sizer.Add(self.mc, pos=(5,1), span=(7,3))
  59. self.SetSizer(sizer)
  60.  
  61. self.timer = wx.Timer(self)
  62. self.Bind(wx.EVT_TIMER, self.onTimer)
  63. # update every 100 milliseconds
  64. self.timer.Start(100)
  65.  
  66. def onLoadFile(self, evt):
  67. mask = "MP3 (.mp3)|*.mp3|MPG (.mpg)|*.mpg|All (.*)|*.*"
  68. dlg = wx.FileDialog(self, message="Choose a media file",
  69. defaultDir=os.getcwd(), defaultFile="",
  70. wildcard=mask, style=wx.OPEN|wx.CHANGE_DIR)
  71. if dlg.ShowModal() == wx.ID_OK:
  72. path = dlg.GetPath()
  73. self.doLoadFile(path)
  74. dlg.Destroy()
  75.  
  76. def doLoadFile(self, path):
  77. if not self.mc.Load(path):
  78. wx.MessageBox("Unable to load %s: Unsupported format?" % path,
  79. "ERROR", wx.ICON_ERROR|wx.OK)
  80. else:
  81. folder, self.filename = os.path.split(path)
  82. self.st_info.SetLabel(" %s" % self.filename)
  83. self.mc.SetInitialSize()
  84. self.GetSizer().Layout()
  85. # set the slider range min to max
  86. self.slider.SetRange(0, self.mc.Length())
  87. self.mc.Play()
  88.  
  89. def onPlay(self, evt):
  90. self.slider.SetRange(0, self.mc.Length())
  91. s1 = " %s" % self.filename
  92. s2 = " size: %s ms" % self.mc.Length()
  93. s3 = " (%d seconds)" % (self.mc.Length()/1000)
  94. self.st_info.SetLabel(s1+s2+s3)
  95. self.mc.SetInitialSize()
  96. self.GetSizer().Layout()
  97. self.mc.Play()
  98.  
  99. def onPause(self, evt):
  100. self.mc.Pause()
  101.  
  102. def onStop(self, evt):
  103. self.mc.Stop()
  104.  
  105. def onSeek(self, evt):
  106. """allows dragging the slider pointer to this position"""
  107. offset = self.slider.GetValue()
  108. self.mc.Seek(offset)
  109.  
  110. def onTimer(self, evt):
  111. """moves the slider pointer"""
  112. offset = self.mc.Tell()
  113. self.slider.SetValue(offset)
  114.  
  115.  
  116. app = wx.App(0)
  117. # create a MyFrame instance and show the frame
  118. MyFrame(None, 'Play Audio and Video', (420, 400)).Show()
  119. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
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: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #48
Aug 2nd, 2008
If you need to enter a date, wxPython has a special widget for such a thing. It's quite versatile and safeguards against the entry of inappropriate dates. This little code example lets you find out how many shopping days you have till xmas, or how many days old you are amongst other things. Read the displayed text and play with the wx.DatePickerCtrl() to get familiar with its capabilities ...
  1. # test wxPython's
  2. # wx.DatePickerCtrl(parent, id, dt, pos, size, style)
  3. # dt = initial date (today by default)
  4.  
  5. import wx
  6. import datetime as dt
  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. s1 = "You can click on the month, day or year portion \n"
  14. s2 = "of the displayed date and use the keyboard up down \n"
  15. s3 = "arrows to change the highlighted part. \n"
  16. s4 = " Or simply type the change in to replace the high- \n"
  17. s5 = "lighted part (will not acccept impossible dates). \n"
  18. s6 = " Or you can click on the right side drop marker to \n"
  19. s7 = "show a monthly calendar and change the date that way."
  20. info = s1+s2+s3+s4+s5+s6+s7
  21. info_label = wx.StaticText(self, wx.ID_ANY, info, pos=(20, 10))
  22.  
  23. # create input widgets
  24. self.datepick = wx.DatePickerCtrl(self, wx.ID_ANY,
  25. pos=(20, 110),
  26. style=wx.DP_DROPDOWN|wx.DP_SHOWCENTURY)
  27.  
  28. # bind mouse click to an action
  29. self.datepick.Bind(wx.EVT_DATE_CHANGED, self.onAction)
  30.  
  31. # create an output widget
  32. self.label = wx.StaticText(self, wx.ID_ANY,
  33. "", pos=(20, 140))
  34.  
  35. def onAction(self, event):
  36. """ some action code"""
  37. # returned object is type wx._misc.DateTime
  38. selected = self.datepick.GetValue()
  39. month = selected.Month + 1
  40. day = selected.Day
  41. year = selected.Year
  42. # you could do this ...
  43. # date_str = "%02d/%02d/%4d" % (month, day, year)
  44. # ... or do some date math with module datetime
  45. date = dt.date(year, month, day)
  46. date_str = date.strftime("%m/%d/%Y")
  47. today = dt.date.today()
  48. age = date - today
  49. s = " (%d days from today)" % age.days
  50. self.label.SetLabel("Date selected = " + date_str + s)
  51.  
  52.  
  53. app = wx.App(0)
  54. # create a MyFrame instance and show the frame
  55. MyFrame(None, 'testing wx.DatePickerCtrl()', (320, 210)).Show()
  56. app.MainLoop()
Last edited by vegaseat; Aug 2nd, 2008 at 11:02 pm. Reason: a
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
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)

 
0
  #49
Aug 3rd, 2008
This is an example how to create a wx.Frame() that fills the entire display screen:
  1. # wx.DisplaySize() gives (width, height) tuple of your display screen
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self):
  7. wx.Frame.__init__(self, None, wx.ID_ANY, pos=(0, 0),
  8. size=wx.DisplaySize())
  9. self.SetBackgroundColour('blue')
  10. s = "Frame has full display size of %d x %d" % wx.DisplaySize()
  11. self.SetTitle(s)
  12.  
  13.  
  14. app = wx.App(0)
  15. # instantiate class MyFrame and show
  16. MyFrame().Show(True)
  17. app.MainLoop()
drink her pretty
Reply With Quote Quick reply to this message  
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)

 
0
  #50
Aug 4th, 2008
Here is a way to fill your full display screen with a wx.Frame() widget, that does not show a border nor a title bar. You have to give it an exit button of your own. To be mildly playful, lets cycle through the wxPython colour data base at random:
  1. # a full screen-display wx.Frame without border and title bar
  2. # wxPython's wx.lib.colourdb contains about 630 different
  3. # colour names to cycle through at random
  4.  
  5. import wx
  6. import random
  7. import wx.lib.colourdb as cdb
  8.  
  9. class MyFrame(wx.Frame):
  10. def __init__(self, parent):
  11. wx.Frame.__init__(self, parent, wx.ID_ANY)
  12. # create a full screen display frame, no title bar
  13. self.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)
  14.  
  15. w, h = wx.DisplaySize()
  16. # create a panel to easily refresh colours
  17. self.panel = wx.Panel(self, wx.ID_ANY, size=(w, h))
  18. # supply an exit button in the upper right corner
  19. self.exitbutton = wx.Button(self.panel, wx.ID_ANY, label='X',
  20. pos=(w - 25, 5), size=(20, 15))
  21. self.exitbutton.Bind(wx.EVT_BUTTON, self.exitbuttonClick)
  22.  
  23. cdb.updateColourDB()
  24. # create a list of all the colours in the colour data base
  25. self.colours = cdb.getColourList()
  26.  
  27. self.timer = wx.Timer(self)
  28. self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)
  29. # change colour every second (1000ms)
  30. self.timer.Start(1000)
  31.  
  32. def onTimer(self, event):
  33. bg = random.choice(self.colours)
  34. #print bg # test
  35. self.panel.SetBackgroundColour(bg)
  36. self.panel.Refresh()
  37.  
  38. def exitbuttonClick(self, event):
  39. self.Close(True)
  40.  
  41.  
  42. app = wx.App(0)
  43. MyFrame(None).Show(True)
  44. app.MainLoop()
drink her pretty
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