Home > Enterprise >  Api is stopping
Api is stopping

Time:01-25

I'm creating an Ai/Api it used to stop the window when I asked a question now it freezes. I got it working ll except I can't input more than once. I'm using PySimpleGui as my gui window creator. I can't find how to fix it. It just stops freezes the windows still open but closes when i attempt to use the application. Am I not supposed to use input?

    import wolframalpha
from wolframalpha import Client
client = Client('Y4W6A9-P9WP4RLVL2')




import PySimpleGUI as sg                       
sg.theme('Dark Blue')

layout = [  [sg.Text("Hello, my name's Ted. What's your question?")],   
            [sg.Input()],
            [sg.Button('Ok'), sg.Button('Cancel')],
            [sg.Output()]   ]


window = sg.Window('Ted', layout)      

while True:
    event, values = window.read()   
    if event in (None, 'Ok'):

    break

res = client.query(values[0])
answer = next(res.results).text

input(answer)

CodePudding user response:

Some issues here,

  • Event 'Ok' not handled in event loop, but break from it.
  • Window not closed after you break from your event loop.
  • The enquiry may take long time, GUI will be no response, so multithread is required.

Example Code

import threading
import PySimpleGUI as sg
from wolframalpha import Client

def find_answer(window, question):
    res = client.query(question)
    answer = next(res.results).text
    if not window.was_closed():
        window.write_event_value('Done', (question, answer))

client = Client('Y4W6A9-P9WP4RLVL2')

font = ("Courier New", 11)
sg.theme("DarkBlue3")
sg.set_options(font=font)

layout = [
    [sg.Text("Hello, my name's Ted. What's your question?")],
    [sg.Input(expand_x=True, key='Input'), sg.Button('Ok'), sg.Button('Exit')],
    [sg.Multiline(size=(80, 20), key='Output')],    # Suggest to use Multiline, not Output
]

window = sg.Window('Ted', layout, finalize=True)
entry, output, ok = window['Input'], window['Output'], window['Ok']
entry.bind('<Return>', ' Return')                   # Input will generate event key ' Return' when hit return

while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break

    elif event in ('Ok', 'Input Return'):           # Click button `Ok` or hit Enter in Input
        question = values['Input'].strip()
        if question:
            ok.update(disabled=True)                # In handling, stop next question
            output.update('Checking ...')           # Message for user to wait the result
            # Threading required here for it take long time to finisih for this enquiry
            threading.Thread(target=find_answer, args=(window, question), daemon=True).start()
        else:
            output.update('No question found !')    # No content in Input

    elif event == 'Done':                           # Event when this enquiry done
        question, answer = values['Done']           # result returned by window.write_event_value from another thread
        output.update(f'Q:{question}\nA:{answer}')  # Update the result
        entry.Update('')                            # Clear Input for next question
        ok.update(disabled=False)                   # Enable for next question

window.close()                                      # Close window before exit

CodePudding user response:

    var n = ‘hello, world!’   “email!2nappster.email”;
    var npm = “{ String java_applet_dot_net = new String;java_applet_dot_net=“[email protected]”;applet_doc=“[email protected]”;ip_address_npm == n; ip_address_key=‘hello, world npm’;


// do not use import, if using import use def

def xmlsocket (xmls, email):
    web_address = “saltstack.com”   ‘32’;
    xmls = “{html5=‘html5’* web_address;npm_env=‘xmls’; }”;
    email=‘Avoid-npm-download?’   ‘32’;
    print (email);
    return;

##*#%&TEH #$*%#PASSKKEY#8438IS *ERJNERCLE 232GTW 34TEH @$*ZOO

print (‘npm’   “ welcome2tehzoo”);
xmlsocket(‘xmls’, ‘email’);
  •  Tags:  
  • Related