简体   繁体   English

我正在尝试在 python 中使用其 function 之外的变量

[英]I'm trying to use a variable outside of its function in python

I'm not sure how I could get my ans_label to access my ans_string as it is defined within the function ssInterpret().我不确定如何让我的 ans_label 访问我的 ans_string,因为它是在 function ssInterpret() 中定义的。

def ssInterpret():
    #region=(x, y, width, height)
    time.sleep(5)
    myScreenshot = pyautogui.screenshot(region=(400, 320, 800, 500))
    myScreenshot.save(imgPath)

    #reads the image

    img = cv2.imread(imgPath)
    text = pytesseract.image_to_string(img)

    q = text

    #completes the answer search

    results = brainlypy.search(q)
    question = results.questions[0]
    print('URL: '+'https://brainly.com/task/'+str(question.databaseid))
    print('QUESTION:',question)
    print('ANSWER 1:', question.answers[0])
    if question.answers_count == 2:
        print('ANSWER 2:', question.answers[1])

    ans_string = str(question.answers[0])

answer_label = Label(root, text=ans_string)

First, your function needs to return the answer:首先,你的function需要返回答案:

def ssInterpret():
    ...  # most of function elided.
    return ans_string

#then call the function 
ans = ssInterpret()
answer_label = Label(root, text=ans)

Initialize ans_string as ans_string = "" at the top of your code.在代码顶部将ans_string初始化为ans_string = "" Then add a line global ans_string prior to ans_string = str(question.answers[0]) inside the function ssInterpret() .然后在 function ssInterpret()内的ans_string = str(question.answers[0])之前添加一行global ans_string Call ssInterpret() before answer_label = Label(root, text=ans_string) .answer_label = Label(root, text=ans_string) ) 之前调用ssInterpret() ) 。 Your code should now work as expected.您的代码现在应该可以按预期工作。

Complete code with modification:修改后的完整代码:

ans_string = ""

def ssInterpret():
    #region=(x, y, width, height)
    time.sleep(5)
    myScreenshot = pyautogui.screenshot(region=(400, 320, 800, 500))
    myScreenshot.save(imgPath)

    #reads the image

    img = cv2.imread(imgPath)
    text = pytesseract.image_to_string(img)

    q = text

    #completes the answer search

    results = brainlypy.search(q)
    question = results.questions[0]
    print('URL: '+'https://brainly.com/task/'+str(question.databaseid))
    print('QUESTION:',question)
    print('ANSWER 1:', question.answers[0])
    if question.answers_count == 2:
        print('ANSWER 2:', question.answers[1])

    global ans_string
    ans_string = str(question.answers[0])

ssInterpret()
answer_label = Label(root, text=ans_string)

暂无
暂无

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

相关问题 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 我想在 python 中的一个变量上使用 2 种数据类型? - I'm trying to use 2 data types on one variable in python? 我正在尝试在 powershell 中使用 python - I'm trying to use python in powershell 我正在尝试在 python 中使用多处理进行压缩 - I'm trying to use multiprocessing for compression in python Python类变量在其回调函数之外延迟 - Python class variable delayed outside of its callback function Python-如果在函数内部创建变量,那么如何在函数外部使用它? - Python - If I create a variable inside a function, how can I then use it outside the function? function 内部的变量如何在 python 的 function 之外使用该变量 - variable inside of function how to use that variable outside of function in python 我正在尝试定义一个函数及其导数,但它似乎不起作用 - I'm trying to define a function an its derivative but it seems to not be working Python Tkinter 访问 function 内部变量,供 function 外部使用 - Python Tkinter access variable inside function for use outside of function 如何使用在函数内部,函数外部定义的变量? - How do i use a variable that is defined inside of a function, outside of a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM