我不確定如何讓我的 ans_label 訪問我的 ans_string,因為它是在函式 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)
uj5u.com熱心網友回復:
首先,您的函式需要回傳答案:
def ssInterpret():
... # most of function elided.
return ans_string
#then call the function
ans = ssInterpret()
answer_label = Label(root, text=ans)
uj5u.com熱心網友回復:
初始化ans_string為ans_string = ""代碼頂部。global ans_string然后ans_string = str(question.answers[0])在函式內部添加一行ssInterpret()。打電話ssInterpret()之前answer_label = Label(root, text=ans_string)。您的代碼現在應該可以按預期作業。
修改后的完整代碼:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/486365.html
下一篇:結構和變數回圈C語言
