1

I want the user to be able to save their work as an image in a Paint app I'm making with Pygame. Just wanted to know if there's a way to have a file browser pop up so the user could choose the save location manually each time?

I am currently using an absolute path to have the images saved on my desktop:

def generate_file_name(num):
    """
    Recursively looks for a file name that doesn't exist in folder for saving a screenshot
    :param num: Suffix for the file name
    :return: Valid file name
    """
    file_name = 'C:/Users/yargr/Desktop/screenshot'
    if num == 0:    # First call
        file_name += '.jpg'
    else:
        file_name += str(num) + '.jpg'

    try:    # Look for a file by that name
        pygame.image.load(file_name)
    except:    # File doesn't exist - name is valid
        return file_name
    else:    # Name exists - try again with new suffix
        file_name = generate_file_name(num + 1)

    return file_name
[...]
def main():
[...]
    screenshot = screen.subsurface(canvas)
    file_name = generate_file_name(0)
    pygame.image.save(screenshot, file_name)

Any suggestions? Thanks!

Yaron Grushka
  • 235
  • 2
  • 10
  • PyGame is low level module and it doesn't have widgets like Buttons, Labels, Windows to create file browser. You would have to use other modules with widgets for PyGame - like `pgu`, `OcempGUI` - see https://www.pygame.org/wiki/gui - but they may need changes in code because they need to run own mainloop to control and update widgets. – furas Sep 29 '19 at 22:46
  • some people use `tkinter` to open separated window with `tkinter.filedialog.askopenfilename`. It doesn't look nice but it can resolve problem. – furas Sep 29 '19 at 22:53

0 Answers0