Module question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tuvas

    #1

    Module question

    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!

  • Mikalai

    #2
    Re: Module question

    Tuvas wrote:[color=blue]
    > 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![/color]

    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

    Comment

    • Tuvas

      #3
      Re: Module question

      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!

      Comment

      • Kent Johnson

        #4
        Re: Module question

        Tuvas wrote:[color=blue]
        > 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![/color]

        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

        Comment

        • Tuvas

          #5
          Re: Module question

          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!

          Comment

          • Gary Herron

            #6
            Re: Module question

            Tuvas wrote:
            [color=blue]
            >Could I just do this then?
            >
            >from foo import x?
            >
            >[/color]
            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.)
            [color=blue]
            >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!
            >
            >
            >[/color]
            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

            Comment

            • Tuvas

              #7
              Re: Module question

              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!

              Comment

              Working...