问题描述
我正在尝试从另一个Imported
的python文件中获取变量及其值。我有文件 Main .py和 Write .py。在Main文件中,我试图获取变量user_id
等于new_id
导入文件写入 .py中的变量。new_id
正在为新用户扫描其标签(rfid)创建哈希。问题是在扫描标签后,变量user_id
仍然为空。我想我可能是在扫描标签之前抓取了变量,有什么想法吗?我已经在下面的代码中发布了一些评论。
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()
# issue is here
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)
写 .py
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()
解决方法
不确定这是实际问题还是此处的格式,但是您的try:语句缩进在主文件中是错误的。