Module question

T

Tuvas

I know this is probably a very simple question, but I am building a
program that is now at about 2400 lines of code in the main module. I
need to break it up, however, there are certain variables that I would
like to use among all of them, namely the TKinter background. It's
build using tkinter, so, to do anything useful requires having the
master=Tk() variable portable in every interface. I can't just pass
this variable, the real way to do it is to have a global variable. How
can I do this? Thanks!
 
M

Mikalai

Tuvas said:
I know this is probably a very simple question, but I am building a
program that is now at about 2400 lines of code in the main module. I
need to break it up, however, there are certain variables that I would
like to use among all of them, namely the TKinter background. It's
build using tkinter, so, to do anything useful requires having the
master=Tk() variable portable in every interface. I can't just pass
this variable, the real way to do it is to have a global variable. How
can I do this? Thanks!

I will write an example with comments:

# say, this is a file/module foo
x = 1 # this is our variable
# end of module foo
----------------------
# other module, where x is needed
import foo
foo.x # here you have it
# end
----------------------

It is all simple, but you need to have proper hierarchical import-ing.
You are splitting a file, which has a hierarchical order (count lines),
so writting proper importing is possible at least in principle.

Mikalai
 
T

Tuvas

Could I just do this then?

from foo import x?

One more question now that I've tried this. In my main function, I have
alot of init code. I don't want this code to be re-ran when the second
module imports the first. Is there any way around this? Thanks!
 
K

Kent Johnson

Tuvas said:
I know this is probably a very simple question, but I am building a
program that is now at about 2400 lines of code in the main module. I
need to break it up, however, there are certain variables that I would
like to use among all of them, namely the TKinter background. It's
build using tkinter, so, to do anything useful requires having the
master=Tk() variable portable in every interface. I can't just pass
this variable, the real way to do it is to have a global variable. How
can I do this? Thanks!

You don't say what the program does, but try to identify code that does
the actual work and put it in separate modules that implement a data
model or business logic for the program. These modules should be
independent of Tkinter.

Kent
 
T

Tuvas

Most of these are indeed independed of Tkinter, except for a status
report message that is often sent in response. I' have a few small
files already that do this, however, I would ike to be able to put
several that do in fact need to post status messages as well. There are
about 2400 lines of code in the main module right now, and about 1000
in smaller modules, but I would like to move some more, the problem is,
most of the function in my tkinter function use somehow the master tk()
variable. I don't know if there's any way around it or not, just trying
to find a way. Mine is a somewhat unusual program wherein that the
majority of the funcionality is in fact as a GUI, it mostly runs
programs from other mediums, it controls an external device, so there
isn't much in the line of real gruntwork that the program does. Thanks
for the help!
 
G

Gary Herron

Tuvas said:
Could I just do this then?

from foo import x?
Yes, you can do it that way. (f you have your modules importing each
other in a circular fashion, then this can cause trouble as x may not be
defined yet, so best to avoid that case.)
One more question now that I've tried this. In my main function, I have
alot of init code. I don't want this code to be re-ran when the second
module imports the first. Is there any way around this? Thanks!
It won't be run twice. Module import is a two phase thing:

1. Read/compile the module and run it, creating a module object. (This
is done only once -- the first time a module is imported.)

2. Bind values in the importing code to the module or its attributes.
(This is done every time a module is imported.)

So... Your init code will be executed once -- in phase 1, no matter how
many times an import causes phase two to be performed.

Gary Herron
 
T

Tuvas

Ahhh. Actually, I realized my problem was the fact that not everything
had been loaded yet. Circular loading can be a bit difficult I can
see... I guess I need to import the new module after x has been
declared? Ei, I need this.

Mod1.py
x=1
from mod2.py import *
=================
Mod2.py
from mod1.py import x

Will this work right? Thanks!
 
D

Dennis Lee Bieber

Ahhh. Actually, I realized my problem was the fact that not everything
had been loaded yet. Circular loading can be a bit difficult I can

Always...

Typically recommendation has always been to move the "common" items
into a third file that both others import.
see... I guess I need to import the new module after x has been
declared? Ei, I need this.

Mod1.py
x=1
from mod2.py import *

from ... import *

is also much frowned upon...
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top