[Python-ideas] A GUI for beginners and experts alike

Mike Barnett mike_barnett at hotmail.com
Fri Aug 24 16:38:29 EDT 2018


I should have mentioned that you need the GitHub version of the code in order to get the keyboard events. Rather than do a pip install you can download this file and put it in your project folder:

https://github.com/MikeTheWatchGuy/PySimpleGUI/blob/master/PySimpleGUI.py

Sorry for any confusion.

I also got a question if this code blocks or is in a spin-loop.   The answer is that the posted version blocks until some kind of form input.  Should you want to turn the program into one that polls instead of blocks,  the loop changes slightly to enable form close detection.  It's basically the same with the Read call being replaced by ReadNonBlocking.

while True:
    button, values = form.ReadNonBlocking()
    if button is None and values is None:
        break
    a, b = values
    try:
        output.Update(int(a) + int(b))
    except:
        pass



@mike

-----Original Message-----
From: Mike Barnett <mike_barnett at hotmail.com> 
Sent: Friday, August 24, 2018 3:36 PM
To: Chris Angelico <rosuav at gmail.com>; Python-Ideas <python-ideas at python.org>
Subject: RE: [Python-ideas] A GUI for beginners and experts alike


So here's my alternative challenge:

Take two numbers as inputs. Add them together and display them in a third field. Whenever either input is changed, recalculate the output.

This requires proper event handling, so it's less likely to create a useless one-liner that has no bearing on real-world code.

------------------------------------



Sure thing... post yours. I'll go ahead and post mine first.

Here's the window this code produces.

https://user-images.githubusercontent.com/13696193/44604157-02a2ac00-a7b3-11e8-928b-f67c5f2b3961.jpg

And here's the code in a more readable form since the email formatting sucks.
https://user-images.githubusercontent.com/13696193/44604220-2cf46980-a7b3-11e8-86c5-ad3051222eaf.jpg

It's rather, uhm, simple to do....


import PySimpleGUI as gui

output = gui.Text('')

layout = [ [gui.Text('Enter 2 numbers')],
           [gui.Text('A'), gui.InputText()],
           [gui.Text('B'), gui.InputText()],
           [gui.Text('Answer = '), output],
           ]

form = gui.FlexForm('Realtime Updates', return_keyboard_events=True)
form.LayoutAndRead(layout)
while True:
    button, (a,b) = form.Read()
    try:
        answer = int(a) + int(b)
        output.Update(answer)
    except:
        pass


@mike


More information about the Python-ideas mailing list