1

First let me preface this by saying that I have been reading through PySimpleGUI docs page and searched high and low on the web, but I can't seem to find a tutorial on how actually get a window to do stuff. I got the GUI basics down, but now I don't know how to insert the code in the window, so it actually does things.

I'm trying to create a basic distance converter (from miles to kilometers). The layout works and so does the function, but I have no clue on how to integrate both.

import PySimpleGUI as sg

layout = [[sg.Text("Enter miles"), sg.InputText(key='-IN-')],
          [sg.Text('Kilometers ->'),sg.Text(key='-OUT-')],
          [sg.Button('Calculate'),sg.Button('Exit')]]

window = sg.Window('Miles to Kilometers',layout)

while True:
    event,values = window.read()
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break
    elif event == 'Calculate':
        window['-OUT-'].update(converter(values['-IN-']))

def converter():
    miles = int(input("Enter miles: "))
    res = miles * 1.609344
    print(round(res,2))
converter()

window.close()
2
  • Lemme help with that search... Cookbook.PySimpleGUI.org is full of Recipes that show "how actually get a window to do stuff". If you prefer video tutorials, you'll find 17 of them here YouTube.PySimpleGUI.org. All of your code to interact with the window, both input and output go inside your event loop. Make your call from there. Remove your call to "input" and add miles as a parameter like you're already calling it. Commented Oct 11, 2020 at 21:27
  • Also, try rewriting your function to accept miles as an input parameter and move the function above your layout. Commented Oct 12, 2020 at 16:40

1 Answer 1

2

Couple of things I noticed...

(1) There is no space for the '-OUT-' text to be displayed. By creating a space with "\t\t\t" I've allowed the output a place to be printed.

(2) When 'Calculate' is pressed, it needs to call the function. You'll see I've changed the function converter() to receive the value entered by the user in the '-IN-' box.

import PySimpleGUI as sg

def converter(miles):
    res = miles * 1.609344
    return res


layout = [[sg.Text("Enter miles"), sg.InputText(key='-IN-')],
          [sg.Text('Kilometers ->'),sg.Text("\t\t\t", key='-OUT-')],
          [sg.Button('Calculate'),sg.Button('Exit')]]

window = sg.Window('Miles to Kilometers',layout)

while True:
    event,values = window.read()
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break
    if event == 'Calculate':
        res = converter(float(values['-IN-']))
        window['-OUT-'].update(value=round(res,2))


window.close()

This should give you what you're looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

You can add a size parameter to Text elements that are to be output elements. This is a very common occurrence in examples in the PySimpleGUI docs: sg.Text(size=(12,1), key='-OUT-')
@MacItaly Thank you so much!! I've been studying this thing for the past two weeks. I'm devouring the cookbook, but I still get stuck sometimes.
I know this is way too late but just wanted to leave this here for future users. Go to pysimplegui.readthedocs.io/en/latest and click on the little magnifying glass on the top right. Then type in the things you wanna search for. For example, you can search "Text" and it will take you to the function call page. That will give you all the info you need. Shoutout to @MikeyB for developing and maintaining this amazing package. It's a lifesaver :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.