send the value of a variable from one program to another - python

Asked

Viewed 146 times

0

Good afternoon. I am making a program in python that will use the numeric keys to open applications and(or) websites.

I created 2 codes: 1 to configure (set the links that the user will want to access), and the other to run.

The setup code is this:

def setup_init():
    hotkeys_num = int(input(('Quantos atalhos vc vai querer (0-9)? ')))

    lista = []
    i = 1
    while i <= hotkeys_num:
    a = input('digite o link/diretório: ')
    lista.append(a)
    i += 1
    print('='*51)
    print('Configuração concluida. Stream deck de pobre ativo.')
    print('='*51)

setup_init()

main:

print(a)

For now, I just want the setup to send the list of links/directories to the other program. Someone knows how I do it ?

  • Or by configuration file, OS Pipes, in Windows by registry or in Intranet by Broadcasting or Web via API.

1 answer

1

You can import the function by doing:
main file

from nomeDoOutroArquivoSemExtensão import setup_init

# Caso queira importar somente a variavel a, basta fazer
from nomeDoOutroArquivoSemExtensão import a

However, you are trying to print a local variable (a). In order to be able to print it in the main file, you must declare it as global within the function. However, it is worth mentioning that this can generate a problem if the function is not called, since the variable will not be declared and will be used in the main file anyway. Therefore, you can declare 'a' outside the function to prevent this. For example:

a = 0
def setup_init():
    hotkeys_num = int(input(('Quantos atalhos vc vai querer (0-9)? ')))

    lista = []
    i = 1
    while i <= hotkeys_num:
    global a
    a = input('digite o link/diretório: ')
    lista.append(a)
    i += 1
    print('='*51)
    print('Configuração concluida. Stream deck de pobre ativo.')
    print('='*51)
  • Thank you, I’ll be tomorrow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.