使用 Python 将返回值从一个函数传递到另一个函数
Passing returned value from one function to another with Python
我正在尝试将输入值从一个函数传递到另一个函数。它的工作原理是用户单击运行 chk():
函数的 Tkinter 按钮。单击按钮后,用户将必须扫描他们的标签(rfig 标签),该标签将读取用户的 tagID
并为 idTag
变量赋值。当返回 idTag
时,将调用 dataCheck():
函数并检查 idTag
值是否与我的 userID 列中的值之一匹配sqlite3 数据库。
我的问题是我不断收到 错误:name 'idTag' is not defined
命令 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()
没有使用它唯一的输入参数,所以去掉它。您正在从 reader
获取 ID,因此此函数不需要其他输入。
您正在将 dateCheck(idTag)
中的输入 idTag
与您的查询字符串进行比较,而不是查询 return 编辑的任何内容,这可能不是您的意图。
当解释器到达函数中的 return
时,函数退出。 tagScanner()
的最后一行永远不会执行,因为它 return 就在那之前。尝试在 return idTag
之前调用 dataCheck(idTag)
您的 sqlite 查询总是查询“idTag”,例如字符串“idTag”,而不是您读入并分配给变量 idTag
的值。使用 ?
指定您要在查询期间提供一个值,请参阅此处的文档:
https://docs.python.org/2/library/sqlite3.html
综合起来:
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()
我正在尝试将输入值从一个函数传递到另一个函数。它的工作原理是用户单击运行 chk():
函数的 Tkinter 按钮。单击按钮后,用户将必须扫描他们的标签(rfig 标签),该标签将读取用户的 tagID
并为 idTag
变量赋值。当返回 idTag
时,将调用 dataCheck():
函数并检查 idTag
值是否与我的 userID 列中的值之一匹配sqlite3 数据库。
我的问题是我不断收到 错误:name 'idTag' is not defined
命令 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()
没有使用它唯一的输入参数,所以去掉它。您正在从 reader
获取 ID,因此此函数不需要其他输入。
您正在将 dateCheck(idTag)
中的输入 idTag
与您的查询字符串进行比较,而不是查询 return 编辑的任何内容,这可能不是您的意图。
当解释器到达函数中的 return
时,函数退出。 tagScanner()
的最后一行永远不会执行,因为它 return 就在那之前。尝试在 return idTag
dataCheck(idTag)
您的 sqlite 查询总是查询“idTag”,例如字符串“idTag”,而不是您读入并分配给变量 idTag
的值。使用 ?
指定您要在查询期间提供一个值,请参阅此处的文档:
https://docs.python.org/2/library/sqlite3.html
综合起来:
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()