Import variable from touch phat looping script

I would like to import a variable from a slightly modified version of the touch phat demo script. However, since it seems to be a looping script, my other python script cannot obtain the variable. The script for file2.py does not seem to work at the point where file.py is imported.
How can I modify the scripts to allow the variables to be imported to file2?

My first python script, file1.py is:

#!/usr/bin/env python

import signal
import time
import os

import touchphat


variable= "X"


@touchphat.on_touch(['A'])
def handle_touch(event):
	variable = "A"
	print("variable = A")
	variable= "X"

    
@touchphat.on_touch(['B'])
def handle_touch(event):
	variable = "B"
	print("variable = B")
	variable= "X"

@touchphat.on_touch(['C'])
def handle_touch(event):
	variable = "C"
	print("variable = C")
	variable= "X"

@touchphat.on_touch(['D'])
def handle_touch(event):
	variable= "D"
	print("variable= D")
	variable = "X"

@touchphat.on_touch(['Back'])
def handle_touch(event):
	variable = "Back"
	print("variable = Back")
	variable= "X"
	
@touchphat.on_touch(['Enter'])
def handle_touch(event):
	variable = "Enter"
	print("variable_x = Enter")
	variable = "X"

signal.pause()

My other python script, file2.py (that imports the variable from file 1) is:

#!/usr/bin/env python
import signal
import time


import file1.py
variable_recieved=0

while True:
	if  file1.variable == "A":
		variable_recieved += 1
		print("variable_recieved =", variable_recieved)

I’m guessing you’re separating into two files to gain a better understanding of Python modules and how they might inter-operate?

You’re really close, but you’re making a few mistakes that will prevent your code doing what you intend.

1

When you put variable = "C" inside your handle_touch functions, Python does - in fact - set variable to "C", unfortunately this variable is within the scope of the handle_touch function only. That means your code enters handle_touch, variable is set, and then it’s discarded again when the code flow exits handle_touch.

You need to use the magic keyword global which tells Python to use the variable from the global scope, rather than the local scope. Just adding global variable to the top of each of your handle_touch functions should fix this.

2

In all cases, your variable will be set to “C” or “D” and then immediately set back to “X”. Python is fast and single-threaded so your while True loop is never going to see file1.variable == "A" because it will effectively always be “X”. You must handle setting this variable back to “X” only when you’ve read the value as something else. For example, after print("variable_received... you would put file1.variable = "X"

3

You need to sleep in your while True loop, or:

  1. It’ll be really tricky to exit your program, because it will be too busy doing stuff to handle SIGINT
  2. Similarly, it might be too busy doing stuff to handle the button events too, and you might never get any button presses

4

The signal.pause() in file1.py will stop Python execution at that point, so when you import it your code will simply halt and the while True will never be reached anyway.

5

Less of an issue- this pattern is odd for passing a variable from an included Python module into its parent. This is not a pattern you’re likely to see in the real world, but it’ll at least teach you a few things about modules.

Thanks gadgetoid. I’ve amended my script based on your advice above, however I do not know how to resolve issue #4. I suspected that signal.pause() might be the reason that the import function in file2.py cause the script to stop. Any ways that I can overcome this? Should I be looking at other options rather than using import? I understand that signal.pause() is also required for file1.py to continue working and hence should not be removed.

The reason for splitting the files is because I want to create a menu and submenu on a OLED display which have options I can select from pressing the buttons on touch phat, and based on the options selected, audio file might be played, or a certain image be displayed. My idea for the menu is kind of different from the original touch phat script (which assumes each button as a menu option). I want button A and button B to be up or down buttons, to allow me to scroll through the menu options. Separating the files, I think, will help me in avoiding an overly cluttered script, hence I decided to have separate scripts and use the import function. File1.py works like an input script, which will relay the variables to file2.py which will effect changes like, a scroll down in the menu options.

For #4 it’s not so tricky- you don’t actually need the signal.pause() in that file unless you’re running it by itself.

The signal.pause() prevents the script from immediately exiting, it tells Python to sit and wait for something to happen. Without it, if you ran file1.py on its own it would run and then close right away, not being much use.

But, since you’re importing file1.py into file2.py you don’t need the signal.pause(), your file2.py is doing something else to prevent the script from immediately exiting.

In the case of Touch pHAT you could probably use some library features to simplify your code quite a lot, since you can handle all of the buttons like so:

@touchphat.on_touch(['Back','A','B','C','D','Enter'])
def handle_touch(event):
    global variable
    variable = event.name
    print(event.name)

Thanks for your help! Indeed, it continued to wrok when I removed the signal.pause() and imported the file into the other script. I’ve implemented the increment and decrement with button A and B and it seems to be working well so far on my display when I press the buttons.