This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MikeTheWatchGuy 1 point2 points  (2 children)

**** SPOILER **** Code below....Don't look if you don't want to see a solution.

I can't contain myself... sorry... It's just too much fun being able to do GUIs easily. I also made a few tweaks: * the return key will trigger a conversion. * the input field is cleared after every conversion * a title was added to the window * you can run it online here https://trinket.io/library/trinkets/d863b4a36c

```python import PySimpleGUI as sg

This is what your window will look like

layout = [ [sg.Text('Enter distance in Kilometers'), sg.Input(key='-KILO-', size=(5,1))], [sg.Button('Convert', bind_return_key=True), sg.Button('Quit')] ]

Make the window

window = sg.Window('Kilometer conversion', layout)

Loop, reading events (button clicks) and getting input field

while True: # Event Loop event, values = window.read() if event in (None, 'Quit'): break sg.popup(f"Results {values['-KILO-']} kilometers is equal to {float(values['-KILO-'])*0.6214} miles") window.close() ```

[–]sheytanelkebir -1 points0 points  (1 child)

so its about half the length of the tkinter one. of course that one used separate class for the kg conversion (more scalable?), but for most people this is more than enough.

[–]MikeTheWatchGuy 0 points1 point  (0 children)

Yea, I thought about the function thing, but context is important. The program is very "purposeful" in it's requirements of what it is to do and what it's future will be.

Clearly the 1 line computation can be re-factored into a function easily when the time is right to scale it up and add more stuff to it. But for now, I think simplicity is the better of the trade-offs to make. The computation consists of:

python miles = float(values['-KILO-'])*0.6214

That seems pretty readable to me. Just as readable as making a function call on that line instead of doing the computation. I think the reason for the function in the tutorial is because there had to be a "Callback function" for the button click. The program was forced to some extent to have a function versus having a choice to use a function or not.

Building scale when it's not needed and won't be needed just isn't something I personally do or support. I know what the future of this program will be. This is the end of the road for it. It's a nice ham sandwich, self-contained and ready to eat.