Search code examples
pythonuser-interfaceif-statementuser-controls

How to generate file.txt if pop-up click on sg.Submit() from PySimpleGUI?


I'm trying to put this line of code inside this gui ...


try:
    name_file = input('Name:')
    file= open(name_file, 'r+')
except FileNotFoundError:
    file= open(name_file, 'w+')
    file.writelines(u'file!')
file.close()

import PySimpleGUI as sg

layout = [
    [sg.Text('Name1', size=(15, 1), background_color="white" ), sg.InputText()],
    [sg.Text('Name2', size=(15, 1), background_color="white" ), sg.InputText()],
    [sg.Text('Name3', size=(15, 1), background_color="white" ), sg.InputText()],

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

window = sg.Window('Test', layout, background_color="white")
event, values = window.Read()
window.Close()
# print(event, values[0])

try:
    name_file = input('Name:')
    file= open(name_file, 'r+')
except FileNotFoundError:
    file= open(name_file, 'w+')
    file.writelines(u'file!')
file.close()

  • python 3.7 ( import PySimpleGUI as sg )

Solution

  • You don't have to put it in GUI. You can use it after GUI.

    You can use event to check what button was pressed and then you can ask for filename and write data in file.

    import PySimpleGUI as sg
    
    layout = [
        [sg.Text('Name1', size=(15, 1), background_color="white" ), sg.InputText()],
        [sg.Text('Name2', size=(15, 1), background_color="white" ), sg.InputText()],
        [sg.Text('Name3', size=(15, 1), background_color="white" ), sg.InputText()],
    
        [sg.Submit(), sg.Cancel()]
    ]
    
    window = sg.Window('Test', layout, background_color="white")
    event, values = window.Read()
    window.Close()
    
    if event == 'Submit':
        try:
            name_file = input('Name:')
            file= open(name_file, 'r+')
        except FileNotFoundError:
            file= open(name_file, 'w+')
    
        all_values = values.values() # values from dictionary
        text = "\n".join(all_values) # put values in separated lines
        file.write(text)             # write all as one string
    
        file.close()
    

    You could create GUI to ask for filename.


    EDIT: I used GUI to ask for filename.

    import PySimpleGUI as sg
    
    layout = [
        [sg.Text('Name1', size=(15, 1), background_color="white" ), sg.InputText()],
        [sg.Text('Name2', size=(15, 1), background_color="white" ), sg.InputText()],
        [sg.Text('Name3', size=(15, 1), background_color="white" ), sg.InputText()],
    
        [sg.Submit(), sg.Cancel()]
    ]
    
    window = sg.Window('Test', layout, background_color="white")
    event, values = window.Read()
    window.Close()
    
    if event == 'Submit':
        # create before next GUI because I want to use the same name for variable `values`
        all_values = values.values() # values from dictionary
        text = "\n".join(all_values) # put values in separated lines
    
        layout = [
            [sg.Text('Filename', size=(15, 1), background_color="white" ), sg.InputText()],
    
            [sg.Submit(), sg.Cancel()]
        ]
    
        window = sg.Window('Test', layout, background_color="white")
        event, values = window.Read()
        window.Close()
    
        if event == 'Submit':
            name_file = values[0]
    
            try:
                fh = open(name_file, 'r+')
            except FileNotFoundError:
                fh = open(name_file, 'w+')
    
            fh.write(text) # write all as one string
    
            fh.close()