I work with Raspberry Pi. I wrote a program for displaying a list of files on a text screen and paging it with clock buttons. I connected my program to the program Pronterface. Pronterface starts up, during the work it calls my program, my program writes the name of the selected file to a variable, Pronterface gets this variable and works with it. In the trial version, the value of this variable is written to the txt file.

A piece of code for my program (program.py)

............... if GPIO.input(16) == False: tex=images[i] tex=str(tex) GPIO.cleanup() exit() ........... 

A piece of Pronterface code:

 import program ...... program.main() .... def getText(): return program.tex .... zz=getText() my=open("File.txt", "w") my.write(zz) my.close() 

After clicking on the button hanging on the GPIO16, everything closes. And if you remove exit (), then nothing happens - Pronterface loading is in the same place where my program was called

Just in case, I post the full code of my program and Pronterface below. http://rghost.ru/64bHTWYxj

  • The context of the module is important here. is tex global? that code in which you assign tex, you would add globals tex . And is it so important to store the variable? Maybe it's better to stir up the function, which will return the value and pull the function? - gil9red
  • By the way, do not upload the code to file sharing services, especially if there are few files. The same pastebin is more suitable. And you write that the program is yours, but judging by the licenses there are no source codes. For such cases, I add my own (for (L) GPL) after the author's copyright, but do not delete it - gil9red
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Create a global variable tex , above tex=images[i] , before assigning tex , write global text . Without globals in the if scope, a new tex link will be created, which will be destroyed immediately after exiting the if .

A few examples:

Here it is not necessary to use global , because text within module area:

 text = "DFDF" print(text) if True: text = "AAA" print(text) print(text) 

However, if we want to use the global variable text in the module function:

 text = "DFDF" print(text) # DFDF def f(): if True: text = "AAA" print(text) # AAA f() print(text) # DFDF 

For changes to occur, you need to add global :

 text = "DFDF" print(text) # DFDF def f(): if True: global text text = "AAA" print(text) # AAA f() print(text) # AAA