Python Forum

Full Version: Combine two programs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
For my masters thesis, I need to program a bit in Python (on a raspberry pi). Since I have never done this before, I do not have a lot of experience. Now, I am stuck on the last bit of my programming, because I really can't find how to let this work, I hope someone of you does. (ps: sorry for bad english)

So basically, now I have 2 completely seperate programs, which run perfectly. But, I need them to be in one single program and be able to activate the second program with the push of a button while the first program runs always whenever this button is not pushed.

Program 1 has the next structure:

def main():
    #My Program

if __name__ == '__main__':
    while True:
        main()
Program 2 has the next structure:

def loop a():
    #My program
def loop b():
    #My program
def loop c():
    #My program
def loop d():
    #My program
def loop e():
    #My program
def loop f():
    #My program

if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()
    Process(target=loop_c).start()
    Process(target=loop_d).start()
    Process(target=loop_e).start()
    Process(target=loop_f).start()
What I now need, is one program which does the next:
#When pressing run, always start with program 1

#When key 'x' is pressed on the keyboard
    #Run program 2
    #When key 'x' is pressed again, stop this program and go back to program 1
Hopefully one of you guys can solve my problem!
Thanks in advance!
def main():
        
    if kp.getKey('a'):
	#code for happy emotion

    elif kp.getKey('z'):
	#code for angry emotion
        
    elif kp.getKey('e'):
	#code for sad emotion

    ...
        
    else:
	#code for neutral emotion
        
if __name__ == '__main__':
    while True:
        main()