Button that opens a .PDF file

Issue

I’m working on a project, where I need the effect of clicking on a certain Button to immediately open the .PDF file, the front-end part is all ready, the back-end part end is basically this problem, the project is a mini software to help students, I even have a project for a software teaching step by step the functions of PySimpleGUI.

flower_base64 = b'iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAB89JREFUeJztnHtQVNcdgL+7dxeWp7zVmuACatT4qojGRCsmhuqAtCbWTjKmxjpj6kytMzoZx2nTyh9tYmdMM9CgpnXiVEfNpI1FBSExKUnwxaO2kDgl9YEKhoC8WRZ2793TPygbCXtwN65R4/3+2j3n/M75nW/unnPuXVjwAyHEHLvdXq5pmlPcozidzlxf5qr4IWW6rusVe998Uy0pKjL19vb64/SuYOSoUeTt2qVZLJYNiqLkD9fWZzEOh6Ok6MiRxzdv3Gi+9RTvHBlLlpC7Y4euqmqWoijFsnYmXzs0mUxzio4cuaelALx77Bjbt20zaZp2SAgxXdbOZzFBQUEjWltaApPdHeZPO3Yobx88aHG5XO8JIR7w1sZnMd82cl56Sa2qqIjq6+srFkKEfbX+vhWjaxrr1qyxNNTXj3M6ne8IIdQb6+9bMQDd3d38dOXKYIfDke5yuX5/Y919LQagob6e1c8+GwT8QgixbqD8vhcDUFNdzcb16826rucJIZ4EQ4yH4sJCcl991aRpWoEQYqoZYCtbk4F5gFUWWPVGFbFX4hlvn/hN5TosuqLTFNRIt7krYH3m5+YqtqQkS2Z2drGSQ84ygTgABAPY40PpSBwxKMDS4yKqrh2LQ/N5kMbpI2makoDbbGLE1U4Sy66gOnWvbVsmxNIXETSozNreS2RDF+Ze+Zi6ovNx7AfUWy/7nNfNmDptGu8UFmIWiG38XwrAueWTKczP9BoU3tjNxIJa5v/uY0Zc6fDa5tJCG8dyl9A0JWFQeUhbLwt//Q/SXi9HEYNjjuzKoi7dNqQv1eXGVlrHgpxSEk9c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ObZzLnLzyu3tX8kZYk50xFf3rwNnVM7y2casKlS+kAjDl4Cde15ivMmPPvwj/wo5mNXNy09xApOozAXsek5ZfAfQf3gYE3Mj7Ly+iaUoCFofG3D/4tsNYHBpzcs8AUPmzWfTEhgQq3ZsSMDHT91aT8l7/4e7oziwOHH6G0xvmcGrjXPaUPs+JFx8FIcjYVELM+Vaf+03LryC4y4kzPIgzGx4JVLo3xQQ03LSVDyhuwYqn32Lq/hoAapdOoPi1xZRsz6BuwVhCWxwse76AtB2VfvVrbe9l1s7+mPKfzx5y/rGrt2fdUXLI2SAQrw0UtKZE8/nM0YMeO/hL86Q4LmSk0DE2CkuPi4SaLxh/7Lz08FeXbsMeH0r8uWYSPm0eUt8TF+q5YU08cXXQCboi6iS14ee+Vp7eGHhQpQBsZWsGkAFEyAJS16aufbe4mLvl20iXycU1az2NwQG54D0MEuMLQgjxVGYmNdXVAU3kbmNAjPEtgQRDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjASfxTidzo6Y2NjbmctdQVxCAk6ns81nMW63+3RmdvY3+58Od4DMpUs1t9t92p8/Tpym63rl3j171JLCwnvyp5iGw2q1sjgry71y1SpdVdVZfgULIWbf6z/eJUPTNGdPT89pIUQawP8AMkNitAzTmWUAAAAASUVORK5CYII='
import PySimpleGUI as sg
file_base64 = b'tem uma imagem aqui'
layout = [  [sg.Text('Base64 Button Demo')],
            [sg.Button('', image_data=file_base64,
            button_color=(sg.theme_background_color(),sg.theme_background_color()),border_width=0, key='-PDF-')]  ]

window = sg.Window('Flowers!', layout, no_titlebar=True)
while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED):
        break
window.close()

Solution

I managed to solve the problem, I want to thank Jason Yang immensely for having the availability to help me.

the result was like this:

the cong.txt file is available for download here: https://www.mediafire.com/file/irtg21gi8zl52xf/cong.txt/file

import PySimpleGUI as sg
import os, sys, subprocess

#how to open a file with graphical interface in python?
#como abrir um arquivo com interface grafica em python?
#¿Cómo abrir un archivo con GUI en python?

file_base64 = b'iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAB89JREFUeJztnHtQVNcdgL+7dxeWp7zVmuACatT4qojGRCsmhuqAtCbWTjKmxjpj6kytMzoZx2nTyh9tYmdMM9CgpnXiVEfNpI1FBSExKUnwxaO2kDgl9YEKhoC8WRZ2793TPygbCXtwN65R4/3+2j3n/M75nW/unnPuXVjwAyHEHLvdXq5pmlPcozidzlxf5qr4IWW6rusVe998Uy0pKjL19vb64/SuYOSoUeTt2qVZLJYNiqLkD9fWZzEOh6Ok6MiRxzdv3Gi+9RTvHBlLlpC7Y4euqmqWoijFsnYmXzs0mUxzio4cuaelALx77Bjbt20zaZp2SAgxXdbOZzFBQUEjWltaApPdHeZPO3Yobx88aHG5XO8JIR7w1sZnMd82cl56Sa2qqIjq6+srFkKEfbX+vhWjaxrr1qyxNNTXj3M6ne8IIdQb6+9bMQDd3d38dOXKYIfDke5yuX5/Y919LQagob6e1c8+GwT8QgixbqD8vhcDUFNdzcb16826rucJIZ4EQ4yH4sJCcl991aRpWoEQYqoZYCtbk4F5gFUWWPVGFbFX4hlvn/hN5TosuqLTFNRIt7krYH3m5+YqtqQkS2Z2drGSQ84ygTgABAPY40PpSBwxKMDS4yKqrh2LQ/N5kMbpI2makoDbbGLE1U4Sy66gOnWvbVsmxNIXETSozNreS2RDF+Ze+Zi6ovNx7AfUWy/7nNfNmDptGu8UFmIWiG38XwrAueWTKczP9BoU3tjNxIJa5v/uY0Zc6fDa5tJCG8dyl9A0JWFQeUhbLwt//Q/SXi9HEYNjjuzKoi7dNqQv1eXGVlrHgpxSEk9cHVovVGa2p1E/KnBiBjAD47xVBHc5GVd8HgBHtJ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kAjDl4Cde15ivMmPPvwj/wo5mNXNy09xApOozAXsek5ZfAfQf3gYE3Mj7Ly+iaUoCFofG3D/4tsNYHBpzcs8AUPmzWfTEhgQq3ZsSMDHT91aT8l7/4e7oziwOHH6G0xvmcGrjXPaUPs+JFx8FIcjYVELM+Vaf+03LryC4y4kzPIgzGx4JVLo3xQQ03LSVDyhuwYqn32Lq/hoAapdOoPi1xZRsz6BuwVhCWxwse76AtB2VfvVrbe9l1s7+mPKfzx5y/rGrt2fdUXLI2SAQrw0UtKZE8/nM0YMeO/hL86Q4LmSk0DE2CkuPi4SaLxh/7Lz08FeXbsMeH0r8uWYSPm0eUt8TF+q5YU08cXXQCboi6iS14ee+Vp7eGHhQpQBsZWsGkAFEyAJS16aufbe4mLvl20iXycU1az2NwQG54D0MEuMLQgjxVGYmNdXVAU3kbmNAjPEtgQRDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjASfxTidzo6Y2NjbmctdQVxCAk6ns81nMW63+3RmdvY3+58Od4DMpUs1t9t92p8/Tpym63rl3j171JLCwnvyp5iGw2q1sjgry71y1SpdVdVZfgULIWbf6z/eJUPTNGdPT89pIUQawP8AMkNitAzTmWUAAAAASUVORK5CYII='

layout = [
    [sg.Push(),
     sg.Button('', image_data=file_base64, button_color= 
    (sg.theme_background_color(),sg.theme_background_color()), border_width=0, key='-PDF-'),
     sg.Push()],
    ]

window = sg.Window('File!', layout, finalize=True)
sg.theme('DarkBlue4')

while True:
    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == '-PDF-':
        path_dir_port=('C:/Users/your_name/dir/cong.txt')
        os.startfile(path_dir_port)


        """
        this part of the code is for a next project. Thanks!
        sg.Window('Base64 PDF', [[sg.Multiline(flower_base64.decode(), size=(40, 16), disabled=True)]], margins=(0, 0), modal=True).read(close=True)
        """
        
window.close()

Answered By – VcodePy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published