User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,720 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,559 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 5651 | Replies: 72
Reply
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Starting wxPython (GUI code)

  #51  
25 Days Ago
You can use several classes in your wxPython program. This example shows you how to cross reference a widget (or variable) from one class to another:
  1. # experiments with wxPython, reference across class instances
  2. # from one panel class to another panel class
  3.  
  4. import wx
  5. import time
  6.  
  7. class MakePanel1(wx.Panel):
  8. def __init__(self, Parent, *args, **kwargs):
  9. wx.Panel.__init__(self, Parent, *args, **kwargs)
  10. self.SetBackgroundColour('yellow')
  11.  
  12. self.label = wx.StaticText(self, -1, " panel1 label ")
  13.  
  14. self.hbox = wx.BoxSizer(wx.HORIZONTAL)
  15. self.hbox.Add(self.label, 0, wx.ALL, 20)
  16. self.SetSizer(self.hbox)
  17.  
  18.  
  19. class MakePanel2(wx.Panel):
  20. def __init__(self, Parent, *args, **kwargs):
  21. wx.Panel.__init__(self, Parent, *args, **kwargs)
  22. self.SetBackgroundColour('green')
  23.  
  24. self.button = wx.Button(self, label=" panel2 button ")
  25. self.button.Bind(wx.EVT_BUTTON, self.buttonClick )
  26.  
  27. self.hbox = wx.BoxSizer(wx.HORIZONTAL)
  28. self.hbox.Add(self.button, 0, wx.ALL, 20)
  29. self.SetSizer(self.hbox)
  30.  
  31. def buttonClick(self, event=None):
  32. # reference panel1's label via frame.panel1
  33. s1 = "panel1 label ..... \n"
  34. s2 = "the time is " + time.strftime("%H:%M:%S", time.localtime())
  35. frame.panel1.label.SetLabel(s1 + s2)
  36.  
  37.  
  38. class MakeFrame(wx.Frame):
  39. def __init__(self, *args, **kwargs):
  40. wx.Frame.__init__(self, *args, **kwargs)
  41.  
  42. self.panel1 = MakePanel1(self)
  43. self.panel2 = MakePanel2(self)
  44.  
  45. vbox = wx.BoxSizer(wx.VERTICAL)
  46. vbox.Add(self.panel1,1,wx.EXPAND);
  47. vbox.Add(self.panel2,1,wx.EXPAND);
  48. self.SetSizer(vbox)
  49. # select a frame size so everything fits
  50. self.Fit()
  51.  
  52.  
  53. app = wx.App(0)
  54. # instance frame is needed for later reference
  55. frame = MakeFrame(None)
  56. frame.Show(True)
  57. app.MainLoop()
drink her pretty
Reply With Quote  
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Starting wxPython (GUI code)

  #52  
25 Days Ago
If you like to draw things, here is an example how to draw some basic shapes on wxPython's canvas:
  1. # draw a few well known shapes on a wx.PaintDC() canvas
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent=None, title=None):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, title)
  8. self.panel = wx.Panel(self, size=(350, 450))
  9. # this sets up the painting canvas
  10. self.panel.Bind(wx.EVT_PAINT, self.on_paint)
  11. # set frame size to fit panel
  12. self.Fit()
  13.  
  14. def on_paint(self, event):
  15. # establish the painting canvas
  16. dc = wx.PaintDC(self.panel)
  17.  
  18. # draw some lines using the given pen specs
  19. # from points (x1, y1) to (x2, y2)
  20. x1 = 50
  21. y1 = 20
  22. x2 = 300
  23. y2 = 20
  24. # first pen colour is red (thickness = 5)
  25. dc.SetPen(wx.Pen('red', 5))
  26. dc.DrawLine(x1, y1, x2, y2)
  27. # second pen colour is white
  28. dc.SetPen(wx.Pen('white', 5))
  29. dc.DrawLine(x1, y1+5, x2, y2+5)
  30. # third pen colour is blue
  31. dc.SetPen(wx.Pen('blue', 5))
  32. dc.DrawLine(x1, y1+10, x2, y2+10)
  33.  
  34. # draw a red rounded-rectangle
  35. # upper left corner coordinates x, y and width, height
  36. # make the pen colour red (thickness = 1)
  37. # default brush (fill) is white
  38. dc.SetPen(wx.Pen('red', 1))
  39. x = 50
  40. y = 50
  41. w = 100
  42. h = 100
  43. rect = wx.Rect(x, y, w, h)
  44. # corners are quarter-circles using a radius of 8
  45. dc.DrawRoundedRectangleRect(rect, 8)
  46.  
  47. # draw a red circle with yellow fill
  48. # center coordinates x, y and radius r
  49. dc.SetBrush(wx.Brush('yellow'))
  50. x = 250
  51. y = 100
  52. r = 50
  53. dc.DrawCircle(x, y, r)
  54.  
  55. # draw an arc from point x1, y1 to point x2, y2 counter
  56. # clockwise along a circle with center xc, yc
  57. # use previous pen and brush settings
  58. xc = 100
  59. yc = 220
  60. x1 = xc
  61. y1 = 170
  62. x2 = xc
  63. y2 = 270
  64. dc.DrawArc(x1, y1, x2, y2, xc, yc)
  65.  
  66. # draw a polygon connecting the (x, y) points in a list
  67. # will also connect first and last points
  68. point_list = [(10, 10), (120, 20), (100, 80), (50, 100)]
  69. dc.DrawPolygon(point_list, xoffset=180, yoffset=170)
  70.  
  71. # draw 2 gradient filled rectangles
  72. # upper left corner coordinates x, y and width, height
  73. x = 50
  74. y = 300
  75. w = 100
  76. h = 100
  77. dc.DrawRectangle(x, y, w, h)
  78. dc.GradientFillLinear((x, y, w, h), 'red', 'blue')
  79. # just a little different fill
  80. dc.DrawRectangle(x+150, y, w, h)
  81. dc.GradientFillConcentric((x+150, y, w, h), 'red', 'blue', (50, 50))
  82.  
  83.  
  84. # test it ...
  85. app = wx.App(0)
  86. MyFrame(title='put some shapes on a canvas').Show()
  87. app.MainLoop()
Last edited by Ene Uran : 25 Days Ago at 3:57 pm. Reason: added corner radius comment
drink her pretty
Reply With Quote  
Join Date: Oct 2006
Posts: 1,193
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 38
sneekula's Avatar
sneekula sneekula is offline Offline
Veteran Poster

Re: Starting wxPython (GUI code)

  #53  
25 Days Ago
I am surprised nobody has brought up the old wx.ListBox() widget yet. Well, here it is, a fun thing to experiment with (stole the name_list from vegaseat):
  1. # load, sort and clear a wxPython
  2. # wx.ListBox(parent, id, pos, size, choices, style)
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent, mytitle, name_list):
  8. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle)
  9. self.SetBackgroundColour("green")
  10. self.name_list = list(name_list)
  11.  
  12. self.listbox = wx.ListBox(self, wx.ID_ANY, choices=[])
  13. self.listbox.Bind(wx.EVT_LISTBOX, self.listboxClick)
  14.  
  15. # create action widgets
  16. self.load_button = wx.Button(self, wx.ID_ANY, "load ListBox")
  17. self.clear_button = wx.Button(self, wx.ID_ANY, "clear ListBox")
  18. self.sort_button = wx.Button(self, wx.ID_ANY, "sort ListBox")
  19. # bind mouse event to an action
  20. self.load_button.Bind(wx.EVT_BUTTON, self.load_buttonClick)
  21. self.clear_button.Bind(wx.EVT_BUTTON, self.clear_buttonClick)
  22. self.sort_button.Bind(wx.EVT_BUTTON, self.sort_buttonClick)
  23. # create an output widget
  24. self.label = wx.StaticText(self, wx.ID_ANY, "")
  25.  
  26. sizer = wx.GridBagSizer(vgap=5, hgap=5)
  27. # pos=(row, column) span=(rowspan, columnspan)
  28. # wx.ALL puts the specified border on all sides
  29. sizer.Add(self.load_button, pos=(0, 0), flag=wx.ALL, border=5)
  30. # listbox spans 6 rows and 2 columns
  31. sizer.Add(self.listbox, pos=(1, 0), span=(6, 2),
  32. flag=wx.ALL|wx.EXPAND, border=5)
  33. sizer.Add(self.clear_button, pos=(7, 1), flag=wx.ALL, border=5)
  34. sizer.Add(self.sort_button, pos=(7, 0), flag=wx.ALL, border=5)
  35. sizer.Add(self.label, pos=(8, 0), flag=wx.ALL, border=5)
  36. self.SetSizer(sizer)
  37.  
  38. # size the frame so all the widgets fit
  39. self.Fit()
  40.  
  41. def load_buttonClick(self, event):
  42. """load the name list into the bistbox"""
  43. # use self.listbox.Set(name_list) or ...
  44. for name in self.name_list:
  45. self.listbox.Append(name)
  46.  
  47. def clear_buttonClick(self, event):
  48. """clear all items from the listbox"""
  49. self.listbox.Clear()
  50. self.label.SetLabel("")
  51.  
  52. def sort_buttonClick(self, event):
  53. """sort the items in the listbox"""
  54. # GetItems() is new in wxPython2.8
  55. # puts the listbox items into a list
  56. name_list = self.listbox.GetItems()
  57. name_list.sort()
  58. # Set() clears and reloads the listbox
  59. self.listbox.Set(name_list)
  60.  
  61. def listboxClick(self, event):
  62. """display the selected ListBox item"""
  63. selected_item = self.listbox.GetStringSelection()
  64. s = "You selected " + selected_item
  65. self.label.SetLabel(s)
  66.  
  67.  
  68. name_list = [
  69. "Andreas",
  70. "Erich",
  71. "Udo",
  72. "Jens",
  73. "Bjorn",
  74. "Heidrun",
  75. "Klaus",
  76. "Ulla",
  77. "Volger",
  78. "Helmut",
  79. "Freja",
  80. "Larry",
  81. "Harry"
  82. ]
  83.  
  84. app = wx.App(0)
  85. # create the MyFrame instance and then show the frame
  86. MyFrame(None, 'listbox test', name_list).Show()
  87. app.MainLoop()
Last edited by sneekula : 25 Days Ago at 9:46 pm. Reason: darn spelling
No one died when Clinton lied.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Starting wxPython (GUI code)

  #54  
22 Days Ago
Thanks Snee for your nice wx.ListBox() example. There is a real high tech listbox in the gizmo section of wxPython. With this one you can edit a selected item including cut, copy, paste portions of it. You can delete, or move selected items up or down in the list. Additionally you can insert, write or paste a new item into the list. You simply have to test it out to get a feel of it ...
  1. # wxPython's amazing wx.gizmos.EditableListBox()
  2. # you can edit (also cut, copy, paste), delete, move selected
  3. # items up or down, and insert (also paste) a new item
  4. # tested with Python25 and wxPython28 vegaseat 07aug2008
  5.  
  6. import wx
  7. import wx.gizmos
  8.  
  9. class MyFrame(wx.Frame):
  10. def __init__(self, parent, mytitle, name_list):
  11. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle)
  12. self.panel = wx.Panel(self, wx.ID_ANY)
  13. self.panel.SetBackgroundColour("green")
  14. self.name_list = list(name_list)
  15.  
  16. self.elistbox = wx.gizmos.EditableListBox(self.panel, wx.ID_ANY,
  17. label="List of Names")
  18. # get the actual control portion of the EditableListBox
  19. self.actual_listctrl = self.elistbox.GetListCtrl()
  20. # binds to any newly focused/selected item
  21. self.actual_listctrl.Bind(wx.EVT_LIST_ITEM_FOCUSED,
  22. self.listctrlClick)
  23.  
  24. # create action widgets
  25. load_button = wx.Button(self.panel, wx.ID_ANY, "load ListBox")
  26. clear_button = wx.Button(self.panel, wx.ID_ANY, "clear ListBox")
  27. sort_button = wx.Button(self.panel, wx.ID_ANY, "sort ListBox")
  28. # bind mouse click event to an action
  29. load_button.Bind(wx.EVT_BUTTON, self.load_buttonClick)
  30. clear_button.Bind(wx.EVT_BUTTON, self.clear_buttonClick)
  31. sort_button.Bind(wx.EVT_BUTTON, self.sort_buttonClick)
  32. # create an output widget
  33. self.label = wx.StaticText(self.panel, wx.ID_ANY, "")
  34.  
  35. sizer = wx.GridBagSizer(vgap=5, hgap=5)
  36. # pos=(row, column) span=(rowspan, columnspan)
  37. # wx.ALL puts the specified border on all sides
  38. sizer.Add(load_button, pos=(0, 0), flag=wx.ALL, border=5)
  39. # listbox spans 6 rows and 2 columns
  40. sizer.Add(self.elistbox, pos=(1, 0), span=(6, 2),
  41. flag=wx.ALL|wx.EXPAND, border=5)
  42. sizer.Add(clear_button, pos=(7, 1), flag=wx.ALL, border=5)
  43. sizer.Add(sort_button, pos=(7, 0), flag=wx.ALL, border=5)
  44. sizer.Add(self.label, pos=(8, 0), flag=wx.ALL, border=5)
  45. # set the sizer and fit all its widgets
  46. self.panel.SetSizerAndFit(sizer)
  47.  
  48. # size the frame so all its widgets fit inside
  49. self.Fit()
  50.  
  51. def load_buttonClick(self, event):
  52. """load the name list into the bistbox"""
  53. self.elistbox.SetStrings(name_list)
  54.  
  55. def clear_buttonClick(self, event):
  56. """clear all items from the elistbox"""
  57. self.elistbox.SetStrings([])
  58. self.label.SetLabel("")
  59.  
  60. def sort_buttonClick(self, event):
  61. """sort the items in the elistbox"""
  62. # GetStrings() puts the elistbox items into a list
  63. name_list = self.elistbox.GetStrings()
  64. name_list.sort()
  65. # SetStrings() clears and reloads the elistbox
  66. self.elistbox.SetStrings(name_list)
  67.  
  68. def listctrlClick(self, event):
  69. """display the selected listctrl item of the elistbox"""
  70. for n in range(self.actual_listctrl.GetItemCount()):
  71. state = wx.LIST_STATE_SELECTED
  72. if self.actual_listctrl.GetItemState(n, state):
  73. selected_item = self.actual_listctrl.GetItemText(n)
  74. s = "You selected " + selected_item
  75. self.label.SetLabel(s)
  76.  
  77.  
  78. name_list = [
  79. "Erich",
  80. "Udo",
  81. "Jens",
  82. "Bjorn",
  83. "Heidrun",
  84. "Klaus",
  85. "Zoe",
  86. "Ulla",
  87. "Volger",
  88. "Helmut",
  89. "Lisa",
  90. "Larry",
  91. "Andreas",
  92. "Harry"
  93. ]
  94.  
  95. app = wx.App(0)
  96. # create a MyFrame instance and show the frame
  97. MyFrame(None, 'elistbox test', name_list).Show()
  98. app.MainLoop()
Last edited by vegaseat : 22 Days Ago at 11:08 am. Reason: or
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Starting wxPython (GUI code)

  #55  
21 Days Ago
The wxPython GUI toolkit has the Open Graphics Library (OGL) integrated in its later versions. This avoids the often cumbersome download and installation of OGL. Here is an example how you can create a number of shapes and drag them around the canvas ...
  1. # the Open Graphics Library (OGL) is now part of wxPython
  2. # also test use of a custom class derived from ogl.PolygonShape
  3. # tested with Python25 and wxPython28 vegaseat 08aug2008
  4.  
  5. import wx
  6. import wx.lib.ogl as ogl
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self):
  10. wx.Frame.__init__( self, None, wx.ID_ANY,
  11. "wx.lib.ogl Demo2", size=(400,300))
  12.  
  13. canvas = ogl.ShapeCanvas(self)
  14. canvas.SetBackgroundColour("yellow")
  15.  
  16. diagram = ogl.Diagram()
  17. # marry the two ...
  18. canvas.SetDiagram(diagram)
  19. diagram.SetCanvas(canvas)
  20.  
  21. # draw some standard shapes ...
  22. # (check how creation order affects overlap)
  23. circle = ogl.CircleShape(80.0) # radius
  24. circle.SetX(125.0) # center x
  25. circle.SetY(125.0)
  26. # use preset pen and brush types
  27. # border ...
  28. circle.SetPen(wx.RED_PEN)
  29. # fill ...
  30. circle.SetBrush(wx.GREEN_BRUSH)
  31. canvas.AddShape(circle)
  32.  
  33. rectangle = ogl.RectangleShape(100, 80) # (w, h)
  34. rectangle.SetX(55.0) # center x
  35. rectangle.SetY(45.0) # center y
  36. # use wx.Pen and wx.Brush for more options
  37. # wx.Pen(colour, width=1, style=wx.SOLID)
  38. # also wx.DOT, wx.DOT_DASH, wx.TRANSPARENT --> no border
  39. rectangle.SetPen(wx.Pen("blue", 2))
  40. # wxBrush(colour, style=wx.SOLID)
  41. # also wx.CROSS_HATCH, wx.CROSSDIAG_HATCH, wx.TRANSPARENT
  42. rectangle.SetBrush(wx.Brush("blue", wx.CROSSDIAG_HATCH))
  43. canvas.AddShape(rectangle)
  44.  
  45. text = ogl.TextShape(250, 30) # (w, h)
  46. text.SetX(180) # center x
  47. text.SetY(240)
  48. text.AddText("you can drag the shapes or the text")
  49. canvas.AddShape(text)
  50.  
  51. # if you have an image file
  52. # you can also create a shape with the image
  53. image_file = 'victory.jpg'
  54. bmp = wx.Bitmap(image_file)
  55. picture = ogl.BitmapShape()
  56. picture.SetBitmap(bmp)
  57. picture.SetX(330) # center x
  58. picture.SetY(160)
  59. canvas.AddShape(picture)
  60.  
  61. # use the custom class ...
  62. circle = DiamondShape(130, 100) # (w, h)
  63. circle.SetX(225.0) # center x
  64. circle.SetY(70.0)
  65. circle.SetPen(wx.BLACK_PEN)
  66. # use no fill ...
  67. circle.SetBrush(wx.TRANSPARENT_BRUSH)
  68. canvas.AddShape(circle)
  69.  
  70. # all the shapes are in, so show them
  71. diagram.ShowAll(True)
  72.  
  73. # use a sizer
  74. sizer = wx.BoxSizer(wx.VERTICAL)
  75. # canvas will grow as frame is stretched
  76. sizer.Add(canvas, 1, wx.GROW)
  77. self.SetSizer(sizer)
  78.  
  79.  
  80. class DiamondShape(ogl.PolygonShape):
  81. """create a diamond shaped object using a polygon"""
  82. def __init__(self, w=60.0, h=60.0):
  83. ogl.PolygonShape.__init__(self)
  84. points = [(0, -h/2.0), (w/2.0, 0), (0,h/2.0), (-w/2.0, 0)]
  85. self.Create(points)
  86.  
  87.  
  88. app = wx.App(0)
  89. ogl.OGLInitialize()
  90. MyFrame().Show()
  91. app.MainLoop()
Last edited by vegaseat : 21 Days Ago at 3:23 pm. Reason: notes
Attached Images
File Type: jpg Victory.jpg (4.3 KB, 13 views)
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Starting wxPython (GUI code)

  #56  
21 Days Ago
To allow just one instance of your wxPython program to run, you need to modify your application class slightly ...
  1. # allow only one instance of a wxPython program to run
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self,parent, mytitle):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle,
  8. size=(300, 90))
  9. self.SetBackgroundColour("red")
  10. self.Centre()
  11. # more of your program code here ...
  12.  
  13.  
  14. class SingleApp(wx.App):
  15. def OnInit(self):
  16. name = "SingleApp-%s" % (wx.GetUserId())
  17. self.instance = wx.SingleInstanceChecker(name)
  18. # create a title
  19. mytitle = name
  20. if self.instance.IsAnotherRunning():
  21. message = "Another instance of %s is running" % mytitle
  22. wx.MessageBox(message, "Error")
  23. return False
  24. MyFrame(None, mytitle).Show()
  25. return True
  26.  
  27.  
  28. app = SingleApp()
  29. app.MainLoop()
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Starting wxPython (GUI code)

  #57  
20 Days Ago
Radio buttons give you a fixed input, one choice at a time. If you have a series of radio buttons it's easiest to group them together with a wx.RadioBox(). The code example shows you how to do this, and also is a good example for the wx.BoxSizer() layout manager ...
  1. # test the wx.RadioBox() widget
  2. # wx.RadioBox(parent, id, label, pos, size, choices, majorDimension, style)
  3. # combines a wx.StaticBox() with a wx.RadioButton()
  4. # only one radiobutton can be selected at a time in the radiobox
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle)