[Tutor] Oops concepts class vs methods.
mhysnm1964 at gmail.com
mhysnm1964 at gmail.com
Thu Apr 20 05:05:19 EDT 2023
All,
In the past I sent some code through related to WX python that I am working
on to see if I can make an accessible home book-shelf library program. Thus
the reasons for these OOPS questions. I am trying to work out if I should
create the following code as a generic method or class. My simplistic
understanding of Oops.
* A class is an object such as creating a GUI window.
* Method is the action you want to do Such as creating an edit control
or performing a database query.
* Properties are the variables of the class / method.
I have a range of WX methods that creates a control on the panel or window.
For example:
class RecordPanel(wx.Panel):
def __init__(self, parent, app):
wx.Panel.__init__(self, parent=parent)
# defining the book object.
self.book = None
# Create the controls for the panel
# adding controls to a sizer.
#sizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.FlexGridSizer(rows=0, cols=2, vgap=10, hgap=10)
self.title_label = wx.StaticText(self, label="Title:")
self.title_text = wx.TextCtrl(self, size=(300, -1))
sizer.Add(self.title_label, flag=wx.ALIGN_LEFT)
sizer.Add(self.title_text, flag=wx.EXPAND)
sizer.Add(wx.StaticText(self, label="About Authors:"),
flag=wx.ALIGN_LEFT)
self.about_author_text = wx.TextCtrl(self, size=(300, 100),
style=wx.TE_MULTILINE)
sizer.Add(self.about_author_text, flag=wx.EXPAND)
# Set the publisher field
self.publisher_label = wx.StaticText(self, label="Publisher:")
self.publisher_text = wx.TextCtrl(self, size=(300, -1))
sizer.Add(self.publisher_label, flag=wx.ALIGN_LEFT)
sizer.Add(self.publisher_text, flag=wx.EXPAND)
# Set the series field
self.series_label = wx.StaticText(self, label="Series:")
self.series_text = wx.TextCtrl(self, size=(300, -1))
sizer.Add(self.series_label, flag=wx.ALIGN_LEFT)
sizer.Add(self.series_text, flag=wx.EXPAND)
. more declaring of controls and events to be added to the page.
self.SetSizer(sizer) # applies the controls to the page.
AS you can tell, the creation of the edit fields and the list boxes use the
same syntax with different variables. Is it possible to create a class with
a couple of methods to reduce the lines above to simplify the debugging and
readability of the code? Below is my attempt and could be completely wrong:
Class WindowControls(sizer, Label, label_flags, control_flags, xPosition,
yPosition):
# sizer is the wx sizer object
# label - the label to be used for the control
# label_flags - the flags used to style and align the control.
# control_flags - flag parameters for the control.
# xPosition and yPosition - the size of the control.
Def EditControl(self)
self.control_label = wx.StaticText(self, label=f"{label}:")
self.control_text = wx.TextCtrl(self, size=(xPosition, yPosition))
sizer.Add(self.control_label, flag=flags)
sizer.Add(self.title_text, flag=control_flags)
return sizer
WindowsControls. EditControl(self.sizer, "title", wx.ALIGN_LEFT, wx.EXPAND,
300, -1)
I hope the above makes sense. As I really an unsure if I am on the right
path here or using Oops in the right way. As I do get confused when to use
the "self" instance.
Sean
More information about the Tutor
mailing list