How to pass variable between forms?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peachdot
    New Member
    • Aug 2010
    • 20

    How to pass variable between forms?

    hi,

    The MainForm will have 2 buttons:
    1.) Button A :
    User click button A, hide Mainform then go to form1. User enter data in the textbox.Click finish button,form1 close then go back to MainForm.

    2.) Button B :
    User Click Button B,hide Mainform then go to form2. Click a button & some mathematical operations will be done using all parameters that have been entered in form1.

    My method of passing variable is:
    * Before Finish button is click, Form1 pass variables to Mainform
    * a function created in MainForm to receive variables
    * Click button B, Mainform pass variables to form2
    * a function created in Form2 to receive.


    My problem now is the Mainform doesnt received any value from form1.Here's my code:

    Code:
    [B]FORM 1 FINISH BUTTON CODE[/B]
    def FINISH(self, sender, e):
    #----Passing Variable to MainForm----
       MainForm._a = int(self._txtbox1.Text)
       MainForm._b = int(self._txtbox2.Text)
       MainForm._c = int(self._txtbox3.Text)
       MainForm._d = int(self._txtbox4.Text)
       MainForm._e = float(self._txtbox5.Text)
       self.close() #CLOSE FORM1
    
    [B]FUNCTION TO RECEIVE VARIABLE in MAINFORM[/B]
    #--Receive variable from Form1----
    def variables(self):
       self._a = 0
       self._b = 0
       self._c  = 0
       self._d = 0
       self._e = 0
    
    
    [B]BUTTON B[/B]
    #----Passing Variable to FORM2----
    def Model(self, sender, e):
       self.Hide()  #hide MainForm
       form2 = Form2.Form2()
    		
       ##-----Pass Variable to Form 2
       Form2._a  = self._a
       Form2._b  = self._b
       Form2._c  = self._c
       Form2._d  = self._d
       Form2._e  = self._e
    
       form2.ShowDialog()
       self.Show()
    
    [B]FUNCTION TO RECEIVE VARIABLE in FORM2[/B]
    def variables(self):
    		self._a = 0
    		self._b = 0
    		self._c  = 0
    		self._d = 0
     		self._e= 0
    help! Can anyone spot my mistake?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Without seeing all the code, I can only speculate.

    What GUI are you using?

    Are you using control variables? In Tkinter, that would be either DoubleVar(), IntVar(), BooleanVar() or StringVar(). The values would be set and retrieved with the control variable methods set() and get().
    Code:
    self.var1 = Tkinter.StringVar()
    self.var1.set("some_value")
    self._a = self.another_var.get()
    Are you encapsulating the main form and the sub forms in one application?

    Comment

    • peachdot
      New Member
      • Aug 2010
      • 20

      #3
      im using SharpDevelop to create my GUI.

      Comment

      Working...