472,980 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

Using variables from my main module in my imported modules.

2
Hi guys. I'm new to python, so go easy.
Heres my problem. I've got my main file, Python Paint.py
Expand|Select|Wrap|Line Numbers
  1. #Python Paint.py
  2. from graphics import *
  3. import time
  4. import functions
  5.  
  6. win = GraphWin("Python Paint",600,600)
  7. win.setCoords(-100.0,-100.0,100.0,100.0)
  8. mycolor = "red"
  9. center = Point(0,95)
  10.  
  11. functions.tri()
  12.  
  13. as you can see, I import another file I've written, called functions.py
  14. here it is:
  15.  
  16. #functions.py
  17. from graphics import *
  18. import time
  19.  
  20. def tri():
  21.     direc = Text(center, "Click three points to form a triangle")
  22.     direc.draw(win)
  23.     time.sleep(2)
  24.     direc.undraw()
  25.     click1 = win.getMouse()
  26.     click2 = win.getMouse()
  27.     click3 = win.getMouse()
  28.     triang = Polygon(click1,click2,click3)
  29.     triang.setFill(mycolor)
  30.     triang.draw(win)
  31. tri()
I assign variables in my Python Paint file, but when i try to call these variables in my functions file, I get an error. Is there any way I can use variables from my main file in my functions file? I hope I've explained this properly.
Sep 24 '07 #1
3 2123
bartonc
6,596 Expert 4TB
Hi guys. I'm new to python, so go easy.
Heres my problem. I've got my main file, Python Paint.py
Expand|Select|Wrap|Line Numbers
  1. #Python Paint.py
  2. from graphics import *
  3. import time
  4. import functions
  5.  
  6. win = GraphWin("Python Paint",600,600)
  7. win.setCoords(-100.0,-100.0,100.0,100.0)
  8. mycolor = "red"
  9. center = Point(0,95)
  10.  
  11. functions.tri()
  12.  
  13. as you can see, I import another file I've written, called functions.py
  14. here it is:
  15.  
  16. #functions.py
  17. from graphics import *
  18. import time
  19.  
  20. def tri():
  21.     direc = Text(center, "Click three points to form a triangle")
  22.     direc.draw(win)
  23.     time.sleep(2)
  24.     direc.undraw()
  25.     click1 = win.getMouse()
  26.     click2 = win.getMouse()
  27.     click3 = win.getMouse()
  28.     triang = Polygon(click1,click2,click3)
  29.     triang.setFill(mycolor)
  30.     triang.draw(win)
  31. tri()
I assign variables in my Python Paint file, but when i try to call these variables in my functions file, I get an error. Is there any way I can use variables from my main file in my functions file? I hope I've explained this properly.
Expand|Select|Wrap|Line Numbers
  1. #Python Paint.py
  2. from graphics import *
  3. import time
  4. import functions
  5.  
  6. win = GraphWin("Python Paint",600,600)
  7. win.setCoords(-100.0,-100.0,100.0,100.0)
  8. mycolor = "red"
  9. center = Point(0,95)
  10.  
  11. functions.tri(win)
  12.  
  13. as you can see, I import another file I've written, called functions.py
  14. here it is:
  15.  
  16. #functions.py
  17. from graphics import *
  18. import time
  19.  
  20. def tri(win):
  21.     direc = Text(center, "Click three points to form a triangle")
  22.     direc.draw(win)
  23.     time.sleep(2)
  24.     direc.undraw()
  25.     click1 = win.getMouse()
  26.     click2 = win.getMouse()
  27.     click3 = win.getMouse()
  28.     triang = Polygon(click1,click2,click3)
  29.     triang.setFill(mycolor)
  30.     triang.draw(win)
  31. if __name__ == "__main__":
  32.     win = GraphWin("Python Paint",600,600)
  33.     tri(win)
  34. #
Pass what you need in the parameters.
That last part eliminates import problems and allows you to test the module.
Sep 25 '07 #2
GIJosh
2
I still get this error when I try that:

NameError: name 'win' is not defined
Sep 25 '07 #3
bartonc
6,596 Expert 4TB
I still get this error when I try that:

NameError: name 'win' is not defined
That can happen if you are trying to run in an interactive window using "import".
If you are using

>>> import themodule

The try

>>> reload(themodule)

Then we'll discuss better way to run your programs.

Or perhaps, simply post your modified code here (instructions for using CODE tags are on the right hand side of the page).
Sep 25 '07 #4

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

Similar topics

4
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
3
by: Michael Brenner | last post by:
Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
3
by: cjt22 | last post by:
Hi I am new to Python (I have come from a large background of Java) and wondered if someone could explain to me how I can access variables stored in my main module to other functions within...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.