Calling function from another Python file

tibaH_lluN

I am trying to call a function from another Python file after a button is click. I have imported the file and used the FileName.fuctionName() to run the function. The problem is my exception keeps catching. I am guessing that the data from the function being called is not being grabbed.What I am trying to do is have a user fill out a Tkinter gui then click a button. Once the button is click the user will then be asked to scan their tag (rfid) and that data will then be sent to a firebase real time database which will store the user's inputted info along with the card_id and user_id that was created when the tag was scanned.

Im kinda at a loss because other than the exception catching I am not getting any other errors, any thoughts? I have posted the code below along with comments.

error : local variable 'user_id' referenced before assignment

from tkinter import *
#Second File
import Write
from tkcalendar import DateEntry
from firebase import firebase

data = {}

global user_id

# Firebase 
firebase= firebase.FirebaseApplication("https://xxxxxxx.firebaseio.com/",None)

# button click
def sub ():
    global user_id

    #setting Variables from user input
    name = entry_1.get()
    last = entry_2.get()
    number = phone.get()
 
    try:
        #Calling Function from other file
        Write.scan()
        if Write.scan():
            #getting the New User Id
            user_id= new_id

        
            #User Info being sent to the Database 
            data = {
            'Name #': name,
            'Last': last,
            'Number': number,
            'Card #':user_id
            }
        results = firebase.post('xxxxxxxx/User',data)
               
    except Exception as e:
        print(e)    

# setting main frame
root = Tk()
root.geometry('850x750')
root.title("Registration Form")

label_0 = Label(root, text="Registration form",width=20,font=("bold", 20))
label_0.place(x=280,y=10)

label_1 = Label(root, text="First Name",width=20,font=("bold", 10))
label_1.place(x=80,y=65)

entry_1 = Entry(root)
entry_1.place(x=240,y=65)

label_2 = Label(root, text="Last Name",width=20,font=("bold", 10))
label_2.place(x=68,y=95)

entry_2 = Entry(root)
entry_2.place(x=240,y=95)

phoneLabel = Label(root, text="Contact Number : ",width=20,font=("bold", 10))
phoneLabel.place(x=400,y=65)

phone = Entry(root)
phone.place(x=550,y=65)

Button(root, text='Submit',command = sub,width=20,bg='brown',fg='white').place(x=180,y=600)

root.mainloop()

Write.py file being Imported

import string
from random import*
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

#Function being called
def scan():
    try:
        #Creating user hash
        c = string.digits + string.ascii_letters
        new_id = "".join(choice(c) for x in range(randint(25,25)))
        print("Please Scan tag")
    
        #Writing to tag
        reader.write(new_id)
        if reader.write(new_id):
            print("Tag Scanned")
        
        else:
            print("Scan Tag First")
        print("Scanning Complete")
    
    finally:
        GPIO.cleanup()
CryptoFool

I see that the value new_id in one file isn't going to influence the value with the same name in the other file, for a similar reason as for the first problem. In both places it appears, new_id is a local variable that only exists in the enclosing function.

Another issue I see is that you're calling Write.scan() twice in a row. Do you mean to be calling it twice? I expect not.

Also, you're testing the return value of Write.scan(), but that function doesn't return a value. So I think that the code in the if block in the first file will never run.

Globals are a bad idea in general, as they're easy to get wrong and they tend to obscure what the code is really doing. "Never say never", but I'll say that I very rarely find the need for a global variable in Python. In your case, I think it would be much better to have Write.scan() return the value of the new user id instead of passing it back as a global. Since you're testing the value of Write.scan(), maybe this is what you were thinking of doing already. Here are the changes I'd make to address these three issues and hopefully get your code working the way you want...

...

def sub ():
    
    ...
    
    try:
        #Calling Function from other file
        new_id = Write.scan()
        if new_id:
            #getting the New User Id
            user_id= new_id
    
    ...

...

def scan():
    try:
        
        ...
        
        new_id = "".join(choice(c) for x in range(randint(25,25)))
        
        ...

        return new_id

    finally:
        GPIO.cleanup()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Python function from another file

From Dev

Python Calling Function from Another File

From Dev

Python calling a function from another file

From Dev

Calling a Python function from another file and using the DF of that function

From Dev

Calling Python function that requires libraries from another file

From Dev

Python calling function in an function from another function

From Dev

Python calling a function from another function in a class

From Dev

PHP calling function from another file not working

From

Calling a function from another file fails

From Dev

Calling an assembly function from another file

From Dev

Octave calling function from another m file

From Dev

Calling ViewController function from another .swift file

From Dev

Calling a function from another file to use in templates

From Dev

React Calling function from another file and useEffect

From Dev

Button Function Not calling From Another file in React

From Dev

Calling a function from another file into another function C++

From Dev

python - calling variable from another function

From Dev

Calling variable from another function in python

From Dev

Python - Calling a function from another class

From Dev

Calling another GUI from a file in python

From Dev

Calling text from another file in python

From Dev

Calling unspecified function from another file and another class

From Dev

Calling a variable from another function inside another file

From Dev

Matlab: Calling a function of a .m file from another .m file

From Dev

Calling a function from HTML to Python file

From Dev

Python: calling function from imported file

From Dev

Calling a function from an external file in Python

From Dev

Python calling function from other file

From Dev

Dynamic Function Calling From a Different File - Python

Related Related

  1. 1

    Calling a Python function from another file

  2. 2

    Python Calling Function from Another File

  3. 3

    Python calling a function from another file

  4. 4

    Calling a Python function from another file and using the DF of that function

  5. 5

    Calling Python function that requires libraries from another file

  6. 6

    Python calling function in an function from another function

  7. 7

    Python calling a function from another function in a class

  8. 8

    PHP calling function from another file not working

  9. 9

    Calling a function from another file fails

  10. 10

    Calling an assembly function from another file

  11. 11

    Octave calling function from another m file

  12. 12

    Calling ViewController function from another .swift file

  13. 13

    Calling a function from another file to use in templates

  14. 14

    React Calling function from another file and useEffect

  15. 15

    Button Function Not calling From Another file in React

  16. 16

    Calling a function from another file into another function C++

  17. 17

    python - calling variable from another function

  18. 18

    Calling variable from another function in python

  19. 19

    Python - Calling a function from another class

  20. 20

    Calling another GUI from a file in python

  21. 21

    Calling text from another file in python

  22. 22

    Calling unspecified function from another file and another class

  23. 23

    Calling a variable from another function inside another file

  24. 24

    Matlab: Calling a function of a .m file from another .m file

  25. 25

    Calling a function from HTML to Python file

  26. 26

    Python: calling function from imported file

  27. 27

    Calling a function from an external file in Python

  28. 28

    Python calling function from other file

  29. 29

    Dynamic Function Calling From a Different File - Python

HotTag

Archive