[Python-ideas] A GUI for beginners and experts alike
Mike Barnett
mike_barnett at hotmail.com
Fri Aug 24 20:20:51 EDT 2018
Nice Matt!
Thanks for taking the time to code it up! It’s great to see examples like these.
@mike<mailto:mike_barnett at hotmail.com>
From: Python-ideas <python-ideas-bounces+mike_barnett=hotmail.com at python.org> On Behalf Of Matthew Einhorn
Sent: Friday, August 24, 2018 7:51 PM
To: python-ideas at python.org
Subject: Re: [Python-ideas] A GUI for beginners and experts alike
Hi Mike
I'm not sure this thread is python-ideas appropriate, but since the challenge is out, here it is using Kivy. The code and result is at https://gist.github.com/matham/45c4f1fbd8c3fccf6557b3b48356cd50<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2Fmatham%2F45c4f1fbd8c3fccf6557b3b48356cd50&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345906031&sdata=Uwr%2FzUCh17hCHSpIsTzA36iVmFpkE%2FgWb7i8DeABhik%3D&reserved=0> (image https://gist.githubusercontent.com/matham/45c4f1fbd8c3fccf6557b3b48356cd50/raw/dbbf74f17ad4beab49f022bbf43fcc72ff725084/kivy_gui.png<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.githubusercontent.com%2Fmatham%2F45c4f1fbd8c3fccf6557b3b48356cd50%2Fraw%2Fdbbf74f17ad4beab49f022bbf43fcc72ff725084%2Fkivy_gui.png&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345916042&sdata=s22vKrCMVzvL76RNm%2FQtU9sJMafFp%2By3i1ZQqD3167E%3D&reserved=0>). The code is also inlined below.
I wrote it to be one file so I used the string version (load_string) for defining the GUI, but the GUI definition could have been written as a separate kv file. I could also have written it using pure python, but that is more verbose and less intuitive. Also, Kivy works on pretty much all platforms (windows, linux, osx, android, ios) and is written in pure python (and cython) on top of opengl.
All the best,
Matt
P.S. here's the inlined code:
from kivy.lang import Builder
from kivy.app import runTouchApp
runTouchApp(Builder.load_string('''
#:import Factory kivy.factory.Factory
BoxLayout:
orientation: 'vertical'
padding: "12dp"
spacing: "12dp"
Label:
text: "Please enter 3 numbers"
BoxLayout:
spacing: "10dp"
Entry:
id: a
hint_text: "A"
Entry:
id: b
hint_text: "B"
Entry:
id: c
hint_text: "C"
Label:
text: "The sum is {}".format(a.val + b.val + c.val)
<Entry at TextInput>:
input_filter: "float"
val: float(self.text) if self.text else 0
'''))
On Fri, Aug 24, 2018 at 5:28 PM Clément Pit-Claudel <cpitclaudel at gmail.com<mailto:cpitclaudel at gmail.com>> wrote:
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<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMikeTheWatchGuy%2FPySimpleGUI%2Fblob%2Fmaster%2FPySimpleGUI.py&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345926047&sdata=9XBPgby1X5GAJo%2BPTt4RVWzAtHxN0rjN2MfSw9ycKv0%3D&reserved=0>
>
> 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<mailto:mike_barnett at hotmail.com>>
> Sent: Friday, August 24, 2018 3:36 PM
> To: Chris Angelico <rosuav at gmail.com<mailto:rosuav at gmail.com>>; Python-Ideas <python-ideas at python.org<mailto: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<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F13696193%2F44604157-02a2ac00-a7b3-11e8-928b-f67c5f2b3961.jpg&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345936052&sdata=92w%2FrRVX5i05EC%2BaUrhYGVpUfF0XSwg8%2B1cLnC3Fn9c%3D&reserved=0>
>
> 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<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F13696193%2F44604220-2cf46980-a7b3-11e8-86c5-ad3051222eaf.jpg&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345946081&sdata=CyGFT%2F7%2FycMstybAAirly4GHFlfqSnoVNy93LWUxvdw%3D&reserved=0>
>
> 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
> Python-ideas at python.org<mailto:Python-ideas at python.org>
> https://mail.python.org/mailman/listinfo/python-ideas<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345956086&sdata=c3rmX6G8AcyDmHRG%2B%2BkQ8We1OgF0mJ0RVSCJLYdB%2FLY%3D&reserved=0>
> Code of Conduct: http://python.org/psf/codeofconduct/<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345966097&sdata=xSinnBACOmIv4y2Z8AQLGL7sxjjBts84L%2BEkWfBC68A%3D&reserved=0>
>
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org<mailto:Python-ideas at python.org>
https://mail.python.org/mailman/listinfo/python-ideas<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345986107&sdata=l0h7XMzPGMOBsm%2F7XOzlPkPgCgKHPJLY9iJDam1KhAk%3D&reserved=0>
Code of Conduct: http://python.org/psf/codeofconduct/<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=02%7C01%7C%7Ce79abd06c77544c33e7008d60a1c9d95%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636707515345996112&sdata=34iqzRJ7AfpiElm07U9HGv8sqvgzkwOuOxDj6tDQztk%3D&reserved=0>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180825/feb3e188/attachment-0001.html>
More information about the Python-ideas
mailing list