Link to home
Create AccountLog in
Avatar of Dolamite Jenkins
Dolamite JenkinsFlag for United States of America

asked on

How do I call a function from a class

I am trying to use a validator to make sure that some input is placed in
 self.email3 = wx.TextCtrl(self.dlg1a,-1,'',pos=(10,80), size=(175, -1),validator = TextObjectValidator())

I have my validator class but how so I call it to check and see if something was inputed
 in the OnSend module how do I call my validator class to check for input in the self.email3 wx.Textctrl ?


def emailsend(self,event):
        self.dlg1a = wx.Dialog(self.frame, -1, "Patient ReporT Email",pos=(50,50), size=(500,500),style=wx.DEFAULT_DIALOG_STYLE |wx.CANCEL)
        
	   
        #panel=wx.Panel(dlg1)
	self.f=''
        self.email2 = wx.TextCtrl(self.dlg1a, -1,'',pos=(10,30), size=(175, -1))
	
        self.email3 = wx.TextCtrl(self.dlg1a,-1,'',pos=(10,80), size=(175, -1),validator = TextObjectValidator())
        self.email4 = wx.TextCtrl(self.dlg1a, -1,'',pos=(10,130), size=(225, -1))


        #self.emailtxt1 = wx.StaticText(dlg1, -1, 'From',(10,10))
        self.emailtxt2 = wx.StaticText(self.dlg1a, -1, 'To ',(10,60))
        self.emailtxt3 = wx.StaticText(self.dlg1a, -1, 'Subject',(10,110))
        self.email5 = wx.TextCtrl(self.dlg1a, -1, '',(10,160),(450,200),style=wx.TE_MULTILINE)
        self.sendbutton = wx.Button(self.dlg1a, 1, 'Send',(200,400))
        self.sendbutton.Bind(wx.EVT_BUTTON,self.OnSend,self.sendbutton)
	self.cancelbutton = wx.Button(self.dlg1a, 1, 'Cancel',(300,400))
        self.cancelbutton.Bind(wx.EVT_BUTTON,self.OnCancel,self.cancelbutton)
	self.pdf=wx.CheckBox(self.dlg1a,-1,"Attache PDF",(10,450))
	self.pdf.Bind(wx.EVT_CHECKBOX,self.AttachePDF,self.pdf)
	self.filepath = wx.TextCtrl(self.dlg1a, -1,self.f,pos=(100,450), size=(225, -1))
	                      
			      
	if self.dlg1a==wx.ID_CANCEL:
		self.dlg1a.Destroy() 
		return False		      
        
        self.dlg1a.Show()
        return True

Open in new window


 
def OnSend(self,event):
how do I test for the validator ?
	self.emailconfirmation(self)
	try:
	    pdfmime=''
	    self.filename=''
	    self.sender =self.email2.GetValue()
	    self.recipient = self.email3.GetValue()
	    self.subject = self.email4.GetValue()
	    self.text = self.email5.GetValue()

Open in new window

        
          



class TextObjectValidator(wx.PyValidator):
    """ This validator is used to ensure that the user has entered something
	into the text object editor dialog's text field.
    """
    def __init__(self):
	""" Standard constructor.
	"""
	wx.PyValidator.__init__(self)



    def Clone(self):
	""" Standard cloner.

	    Note that every validator must implement the Clone() method.
	"""
	return TextObjectValidator()


    def Validate(self, win):
	""" Validate the contents of the given text control.
	"""
	textCtrl = self.GetWindow()
	text = textCtrl.GetValue()

	if len(text) == 0:
	    wx.MessageBox("A text object must contain some text!", "Error")
	    textCtrl.SetBackgroundColour("pink")
	    textCtrl.SetFocus()
	    textCtrl.Refresh()
	    return False
	else:
	    textCtrl.SetBackgroundColour(
	        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
	    textCtrl.Refresh()
	    return True


    def TransferToWindow(self):
	""" Transfer data from validator to window.

	    The default implementation returns False, indicating that an error
	    occurred.  We simply return True, as we don't do any data transfer.
	"""
	return True # Prevent wxDialog from complaining.


    def TransferFromWindow(self):
	""" Transfer data from window to validator.

	    The default implementation returns False, indicating that an error
	    occurred.  We simply return True, as we don't do any data transfer.
	"""
	return True # Prevent wxDialog from complaining.

Open in new window



Avatar of mish33
mish33
Flag of United States of America image

I don't think you get it. Validators used to check single field input/formatting (ie. date, email).
Inside OnSend you just check it's not empty:

 self.recipient = self.email3.GetValue()
 if not self.recipient:
   # display error
   return
 # do same checks for other fields
 # if all check pass do send
Avatar of Dolamite Jenkins

ASKER

Mish I am very new ... could you explain your response ....Do I need the validator class ? Im confused
If you want you input to any ONE field be validated, then you need/use one of the predefined or your own validator.
In your case when you need to check several fields to ensure form / dialog validity, it's being done on form action like OnSend.
Thats what is confusing me... validator is a class that I can call without writing my own class ?... all that I have read makes it seem like I have to write the class first ... that there is no wxvalidator... so to check one line of code to see if it where empty ... what would that look like ?
ASKER CERTIFIED SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Ive come along way in the last couple months with my programming and the only thing that has given me difficulty understanding is validator
thanks