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 does not seem to work at the point where is imported.
How can I modify the scripts to allow the variables to be imported to file2?

My first python script, is:

#!/usr/bin/env python

import signal
import time
import os

import touchphat

variable= "X"

def handle_touch(event):
	variable = "A"
	print("variable = A")
	variable= "X"

def handle_touch(event):
	variable = "B"
	print("variable = B")
	variable= "X"

def handle_touch(event):
	variable = "C"
	print("variable = C")
	variable= "X"

def handle_touch(event):
	variable= "D"
	print("variable= D")
	variable = "X"

def handle_touch(event):
	variable = "Back"
	print("variable = Back")
	variable= "X"
def handle_touch(event):
	variable = "Enter"
	print("variable_x = Enter")
	variable = "X"


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

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


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.


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.


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"


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


The signal.pause() in 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.


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 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 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. works like an input script, which will relay the variables to 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 on its own it would run and then close right away, not being much use.

But, since you’re importing into you don’t need the signal.pause(), your 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:

def handle_touch(event):
    global variable
    variable =

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.