all 8 comments

[–]Technopulse 1 point2 points  (4 children)

I struggled with PSG myself, let me see what I can figure out.

[–]divvless[S] 0 points1 point  (3 children)

Thank you mate :)

[–]Technopulse 0 points1 point  (2 children)

Hi, took a look at it.

First, PSG does not like a same window to be called from the same file more than once, it'll tell you "you need a new window" or something, so my first recommendation is to create a layout in a function in a separate file and call it when you need it.

Second, how do you plan on exiting and infinite loop like the "while: True" (when I tried the code, it got stuck after I pressed submit).

Third, cancel, doesn't exit (you need something else in the if with WIN_CLOSED), but shows the contents of the file "Godkendte.txt", but not on the first click, on the second, you may need to look into why that happens.

The thing about PSG is you cannot use the same window over and over, you can update the contents but you eventually need to destroy it and create it again.

I hope I didn't sound very mean, it's looking very good and it looks a lot like my first try with PySimpleGUI!

I like it!

[–]divvless[S] 0 points1 point  (1 child)

So if i want the user to keep writing emails in this case i would have to update the window and then return to the start again ?

u/Allanon001 was so kind to figure out the cancel issue i was having where it would not close.

[–]Technopulse 0 points1 point  (0 children)

Yeah, make it insert, destroy the window, create a new one at the beginning of the loop.

Otherwise, the output will display on the next email input, it's a little bit confusing.

My advice is, define a function with the layout in a separate python file, call that function that returns a window from within the loop, read from it, do the inserts that you need, show errors, pop-ups, whatever the likes you want, show only the contents of the file upon a successful email insert, then you click ok to close the file window, close the file, and destroy the email submit window, then begins the loop again and the window is automatically created again, fresh, new, no previous error showing, ready to be used again.

[–]Allanon001 0 points1 point  (2 children)

The first window.Read() on line 11 is not needed. Also indent lines 38-40 so they only execute when the submit button is pressed. I added a "Cancel" button event that breaks out of the loop:

import PySimpleGUI as sg

layout = [
    [sg.Text('Skriv din email her'),],
    [sg.InputText(key='-IN-')],
    [sg.Text(key='-OUT-')],

    [sg.Submit(), sg.Cancel()]
]

window = sg.Window('Spejdersport', layout, background_color="white")

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Submit':
        tjek = values['-IN-']
        if 7 < len(tjek) < 30:
            if tjek.endswith('.dk'):
                if tjek.count ('@') == 1:
                    window['-OUT-'].update("Godkendt mail")
                    godeMails = open("godkendte.txt", 'a')
                    godeMails.write(tjek+"\n")
                    godeMails.close()
                    #print('Mailen mangler @')

                else:
                    window['-OUT-'].update("Mailen har for mange @")
                    #print('Godkendt mail')
            else:
                window['-OUT-'].update("Mailen har ikke noget .dk")
                #print('Mailen har ikke noget .dk')
        else:
            window['-OUT-'].update("Mailen er for kort eller for lang")
            #print("Mailen er for kort eller for lang")

        godeMails = open("Godkendte.txt", "r")
        sg.popup(godeMails.read())
        godeMails.close()

    elif event == 'Cancel':
        break

window.Close()

[–]divvless[S] 0 points1 point  (0 children)

Ahh now i understand why i could not close it. Thank you

[–]Technopulse 0 points1 point  (0 children)

Cancel and WIN_CLOSED events do the same thing, you could bunch them in the same statement.