471,313 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 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 2047
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

Post your reply

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

Similar topics

4 posts views Thread by Torsten Bronger | last post: by
3 posts views Thread by Michael Brenner | last post: by
1 post views Thread by jmalone | last post: by
4 posts views Thread by icarus | last post: by
reply views Thread by rosydwin | last post: by

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.