By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

How to pass variable between forms?

P: 20
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
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 20
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.