Hi Mike, Thanks, this code is nice and short. Is adding 'if button is None: break' to the 'Read' version of your code the right way to make it exit when the main window is closed? (On my machine it enters an infinite loop after I close the main window).
For comparison, I tried doing this with PyGObject.
I created the UI with glade, which auto-generated the attached XML file. Then
I had to write the following code (I had never used Glade or PyGObject before,
so apologies if there are mistakes in the following):
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def main():
builder = Gtk.Builder()
builder.add_from_file("sum.glade")
get = builder.get_object
a, b, answer, window = get("a"), get("b"), get("answer"), get("window")
def update_sum(_entry):
try:
tanswer = int(a.get_text()) + int(b.get_text())
answer.set_text(str(tanswer))
except ValueError:
pass
a.connect("changed", update_sum)
b.connect("changed", update_sum)
window.connect("destroy", Gtk.main_quit)
window.show_all()
Gtk.main()
if __name__ == '__main__':
main()
Having a visual editor for the UI feels like a plus, and I find the resulting
XML verbose but acceptably readable.
On the other hand, I like the conciseness of your UI specs.
Cheers,
Clément.
On 2018-08-24 16:38, Mike Barnett wrote:
> 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 <[email protected]>
> Sent: Friday, August 24, 2018 3:36 PM
> To: Chris Angelico <[email protected]>; Python-Ideas <[email protected]>
> 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
> _______________________________________________
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
sum.glade
Description: application/glade
_______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
