473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 2132
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.