How to execute code from other tab

edited October 2017 in Python Mode

Hello. I'm typing code in other tab, from processing, however a need to import the functions for me to execute. When I typing "import", the processing return error.

Example:

Tab 1 (main) import tab2

foo();

Tab 2

def foo(): print("Hello world! =)")


How I can to do this?

Answers

  • edited October 2017

    Ignore me. I apparently don't know anything about Python mode.

  • edited October 2017 Answer ✓

    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    • Under Python Mode, only the 1st main tab has the extension ".pyde".
    • All additional tabs use extension ".py" instead.
    • Therefore, we 1st need to import them into our main ".pyde" tab before they become available.
    • Let's say your tab 2 is called "My_Other_Stuff.py".
    • And inside that ".py" tab, you've got a function called loadStuff(): def loadStuff():
    • A class called Stuff: class Stuff:
    • And even a constant variable called HOUR: HOUR = 60 * 60 * 1000

    My_Other_Stuff.py

    HOUR = 60 * 60 * 1000
    
    def loadStuff(): pass
    
    class Stuff: pass
    
    • Now, in order to import everything from "My_Other_Stuff.py" tab into the ".pyde" main tab, you can use the following: from My_Other_Stuff import *
    • You can even specify which 1s you actually want.
    • Let's say you need loadStuff() & HOUR, but not Stuff: from My_Other_Stuff import loadStuff, HOUR
    • And sometimes, you might prefer to access them all via a prefix:

    My_Main_Tab.pyde:

    import My_Other_Stuff as Stuff
    
    print Stuff.HOUR
    things = Stuff.loadStuff()
    stuff = Stuff.Stuff()
    
  • Thank you GoToLoop for your answer about my question and about the format text on post.

Sign In or Register to comment.