简体   繁体   English

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

[英]Passing returned value from one function to another with Python

I am trying to pass the inputted value from one function to another function.我试图将输入的值从一个函数传递到另一个函数。 How it works is the user click the Tkinter button which runs the chk(): function.它的工作原理是用户单击运行chk():函数的 Tkinter 按钮。 Once the button is click the user will have to scan their tag (rfig tag) which will read the user's tagID and give the idTag variable a value.一旦按钮点击,用户将需要扫描的标签(标签rfig),将读取用户的tagID和赋予idTag变量的值。 When the idTag is returned the dataCheck(): function will be called and check if the idTag value match one of the values in the userID column of my sqlite3 database.idTag返回dataCheck():函数将被调用,并检查idTag在我的sqlite3数据库的用户ID列中的值的值匹配之一。

My issue is I keep getting the Error : name 'idTag' is not defined我的问题是我不断收到错误未定义名称“idTag”

The command reader.read() acts like a input function because the user actually has to scan (or input) their tag before they can continue.命令reader.read()就像一个输入函数,因为用户实际上必须扫描(或输入)他们的标签才能继续。 I think the problem is the function is being call as soon as the button is being click which is causing the idTag variable to still be empty due to the user not having inputted the value yet.我认为问题是只要点击按钮就会调用函数,这导致idTag变量仍然为空,因为用户还没有输入值。 Any thoughs?有什么想法吗?

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)

Several issues here这里有几个问题

First, get rid of global idTag as I think that's just creating scope issues.首先,摆脱global idTag因为我认为这只会造成范围问题。 You don't need a global variable.您不需要全局变量。

tagScanner() does not make use of its only input argument, so get rid of it. tagScanner()不使用它唯一的输入参数,所以去掉它。 You're getting the id from reader , so this function does not need another input.您从reader获取 id ,因此此函数不需要其他输入。

You're comparing the input idTag in dateCheck(idTag) to your query string instead of whatever's returned by the query, that's probably not your intention.您将dateCheck(idTag)的输入idTag与您的查询字符串进行比较,而不是查询返回的任何内容,这可能不是您的意图。

When the interpreter reaches a return in a function, the function exits.当解释器在函数中return时,函数退出。 The last line of tagScanner() will never execute, because it returns right before that. tagScanner()的最后一行永远不会执行,因为它在此之前返回。 Try calling dataCheck(idTag) before you return idTag在返回idTag之前尝试调用dataCheck(idTag)

Your sqlite query is always querying for "idTag", eg the string of characters "idTag" rather than the value that you read in and assigned to the variable idTag .您的 sqlite 查询总是查询“idTag”,例如字符串“idTag”而不是您读入并分配给变量idTag Use ?使用? to designate that you want to provide a value during the query, see the docs here: https://docs.python.org/2/library/sqlite3.html要指定要在查询期间提供值,请参阅此处的文档: https : //docs.python.org/2/library/sqlite3.html

Putting it all together:把它们放在一起:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM