469,150 Members | 1,975 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,150 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. FORM 1 FINISH BUTTON CODE
  2. def FINISH(self, sender, e):
  3. #----Passing Variable to MainForm----
  4.    MainForm._a = int(self._txtbox1.Text)
  5.    MainForm._b = int(self._txtbox2.Text)
  6.    MainForm._c = int(self._txtbox3.Text)
  7.    MainForm._d = int(self._txtbox4.Text)
  8.    MainForm._e = float(self._txtbox5.Text)
  9.    self.close() #CLOSE FORM1
  10.  
  11. FUNCTION TO RECEIVE VARIABLE in MAINFORM
  12. #--Receive variable from Form1----
  13. def variables(self):
  14.    self._a = 0
  15.    self._b = 0
  16.    self._c  = 0
  17.    self._d = 0
  18.    self._e = 0
  19.  
  20.  
  21. BUTTON B
  22. #----Passing Variable to FORM2----
  23. def Model(self, sender, e):
  24.    self.Hide()  #hide MainForm
  25.    form2 = Form2.Form2()
  26.  
  27.    ##-----Pass Variable to Form 2
  28.    Form2._a  = self._a
  29.    Form2._b  = self._b
  30.    Form2._c  = self._c
  31.    Form2._d  = self._d
  32.    Form2._e  = self._e
  33.  
  34.    form2.ShowDialog()
  35.    self.Show()
  36.  
  37. FUNCTION TO RECEIVE VARIABLE in FORM2
  38. def variables(self):
  39.         self._a = 0
  40.         self._b = 0
  41.         self._c  = 0
  42.         self._d = 0
  43.          self._e= 0
  44.  
help! Can anyone spot my mistake?
Sep 9 '10 #1
2 2415
bvdet
2,851 Expert Mod 2GB
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().
Expand|Select|Wrap|Line Numbers
  1. self.var1 = Tkinter.StringVar()
  2. self.var1.set("some_value")
  3. self._a = self.another_var.get()
Are you encapsulating the main form and the sub forms in one application?
Sep 9 '10 #2
im using SharpDevelop to create my GUI.
Sep 10 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by jongyoo | last post: by
1 post views Thread by dollar | last post: by
1 post views Thread by drec | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.