首页 > 解决方案 > 有人知道处理 Wolfram Alpha 和 Python 吗?

问题描述

下面是我的脚本:

import wolframalpha
client = wolframalpha.Client("LXP5A5-62XJKEY85U")
import PySimpleGUI as sg


sg.theme('DarkPurple')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Enter a command'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Sunia', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    res = client.query(values[0])
    try:
        print (next(res.results).text)
    except:
        print ("No results") 


window.close()

这是我的错误:

res = client.query(values[0])
  File "C:\Users\jonathan b\AppData\Local\Programs\Python\Python38-32\lib\site-packages\wolframalpha\__init__.py", line 68, in query
    assert resp.headers.gettype() == 'text/xml'
AttributeError: 'HTTPMessage' object has no attribute 'gettype'

标签: pythonxmlwolframalpha

解决方案


我想我理解你得到的错误非常简单,你只需将
client = wolframalpha.Client("LXP5A5-62XJKEY85U")放在sg.theme('DarkPurple')之前

import wolframalpha
import PySimpleGUI as sg

client = wolframalpha.Client("LXP5A5-62XJKEY85U")
sg.theme('DarkPurple')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Enter a command'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Sunia', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    res = client.query(values[0])
    try:
        print (next(res.results).text)
    except:
        print ("No results") 


window.close()

我隐藏了我的客户 ID


推荐阅读