使用Python将返回值从一个函数传递到另一个函数

使用Python将返回值从一个函数传递到另一个函数,python,sqlite,raspberry-pi,rfid,Python,Sqlite,Raspberry Pi,Rfid,我试图将输入值从一个函数传递到另一个函数。它的工作原理是用户单击Tkinter按钮,该按钮运行chk():函数。单击按钮后,用户必须扫描其标签(rfig标签),该标签将读取用户的tagID,并为idTag变量提供一个值。返回idTag时,将调用dataCheck():函数,并检查idTag值是否与我的sqlite3数据库的userID列中的一个值匹配 我的问题是我一直收到错误:未定义名称“idTag” 命令reader.read()的作用类似于一个输入函数,因为用户实际上必须扫描(或输入)他们的

我试图将输入值从一个函数传递到另一个函数。它的工作原理是用户单击Tkinter按钮,该按钮运行
chk():
函数。单击按钮后,用户必须扫描其标签(rfig标签),该标签将读取用户的
tagID
,并为
idTag
变量提供一个值。返回
idTag
时,将调用
dataCheck():
函数,并检查
idTag
值是否与我的sqlite3数据库的userID列中的一个值匹配

我的问题是我一直收到错误:未定义名称“idTag”

命令
reader.read()
的作用类似于一个输入函数,因为用户实际上必须扫描(或输入)他们的标签才能继续。我认为问题在于,只要点击按钮,函数就会被调用,这会导致
idTag
变量仍然为空,因为用户尚未输入值。有什么想法吗

from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

global idTag
   
# Tkinter button click Command 
def chk():

    # Function that handels getting User ID 
    def tagScanner(idTag):
        #Get user id value
        tagID = reader.read()
        #If tag is scanned 
        if tagID:
            idTag= (tagID,)
            return idTag
            #call Database function to check returned idTag
            dataCheck(idTag)
    
    # Function handels SQLite3 Database     
    def dataCheck(idTag):
        Database = sql.connect('MedaDataBase.db')
        # cursor
        c= Database.cursor()
        #Check if the idTag maches a value in the userID column of sqlite DB
        query = 'SELECT userID FROM Users "WHERE" userID = "idTag"'

        c.execute(query)
        c.fetchone()
           
        if query == idTag: #if userID is returned
            print('User Verified')
        else:
            print('Denied')
        Database.close()

    #Call scanning function 
    tagScanner(idTag)
这里有几个问题

首先,摆脱
global-idTag
,因为我认为这只会造成范围问题。您不需要全局变量

tagScanner()
没有使用其唯一的输入参数,因此请将其删除。您正在从
读卡器
获取id,因此此函数不需要其他输入

您正在将
dateCheck(idTag)
中的输入
idTag
与查询字符串进行比较,而不是将查询返回的内容进行比较,这可能不是您的意图

当解释器到达函数中的
return
时,函数退出。
tagScanner()
的最后一行永远不会执行,因为它会在该行之前返回。返回
idTag

sqlite查询总是查询“idTag”,例如字符串“idTag”,而不是您读入并分配给变量
idTag
的值。使用
指定要在查询期间提供值,请参阅此处的文档:

总而言之:

from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

# Tkinter button click Command 
def chk():

    # Function that handels getting User ID 
    def tagScanner():
        # Get user id value
        idTag = reader.read()
        # If tag is scanned 
        if idTag:
            # call Database function to check returned idTag
            dataCheck(idTag)
            return idTag
    
    # Function handles SQLite3 Database     
    def dataCheck(idTag):
        Database = sql.connect('MedaDataBase.db')
        # cursor
        c = Database.cursor()
        # Check if the idTag maches a value in the userID column of sqlite DB
        query = 'SELECT userID FROM Users WHERE userID = ?'

        c.execute(query, (idTag,))
        row_returned = c.fetchone()
        if row_returned is not None:
            # Found a matching row
            print('User Verified')
        else:
            print('Denied')
        Database.close()

    # Call scanning function 
    tagScanner()
这里有几个问题

首先,摆脱
global-idTag
,因为我认为这只会造成范围问题。您不需要全局变量

tagScanner()
没有使用其唯一的输入参数,因此请将其删除。您正在从
读卡器
获取id,因此此函数不需要其他输入

您正在将
dateCheck(idTag)
中的输入
idTag
与查询字符串进行比较,而不是将查询返回的内容进行比较,这可能不是您的意图

当解释器到达函数中的
return
时,函数退出。
tagScanner()
的最后一行永远不会执行,因为它会在该行之前返回。返回
idTag

sqlite查询总是查询“idTag”,例如字符串“idTag”,而不是您读入并分配给变量
idTag
的值。使用
指定要在查询期间提供值,请参阅此处的文档:

总而言之:

from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

# Tkinter button click Command 
def chk():

    # Function that handels getting User ID 
    def tagScanner():
        # Get user id value
        idTag = reader.read()
        # If tag is scanned 
        if idTag:
            # call Database function to check returned idTag
            dataCheck(idTag)
            return idTag
    
    # Function handles SQLite3 Database     
    def dataCheck(idTag):
        Database = sql.connect('MedaDataBase.db')
        # cursor
        c = Database.cursor()
        # Check if the idTag maches a value in the userID column of sqlite DB
        query = 'SELECT userID FROM Users WHERE userID = ?'

        c.execute(query, (idTag,))
        row_returned = c.fetchone()
        if row_returned is not None:
            # Found a matching row
            print('User Verified')
        else:
            print('Denied')
        Database.close()

    # Call scanning function 
    tagScanner()

谢谢你的帮助,我确实学到了一些新东西。然而,我得到了一个错误。错误发生在
c.execute(query,(idTag,)
处,错误消息为:
sqlite3.interface错误:错误绑定参数0-可能不支持类型
@tibaH\u lluN可能未正确设置idTag;在查询之前打印它,它的值是多少?我想我已经修复了它,直到我这样做并发现了问题的根源
idTag
正在返回一个值元组。感谢您的帮助,肯定学到了一些新东西。然而,我得到了一个错误。错误发生在
c.execute(query,(idTag,)
处,错误消息为:
sqlite3.interface错误:错误绑定参数0-可能不支持类型
@tibaH\u lluN可能未正确设置idTag;在查询之前打印它,它的值是多少?我想我已经修复了它,直到我这样做并发现了问题的根源
idTag
正在返回一个值元组。