0

This def is my script that scans a website for active links. In this case, I wanted to know how to get this data and put it on the Pysimplegui OUTPUT screen.

import PySimpleGUI as sg
import requests
from bs4 import BeautifulSoup
import re

sg.theme( 'Reddit' )


def prog() :
    r = requests.get( 'http://www.google.com' )
    html = r.text
    soup = BeautifulSoup( html , 'lxml' )

    links = soup.find_all( 'a' , attrs={'href' : re.compile( '^http://)' )} )
    for link in links :
        href = link['href']
        return href


        layout = [
[sg.Output( size=(30 , 20) , key='For_EXIT' )] ,
[sg.Button( 'Read' ) , sg.Exit( )]

]

window = sg.Window( 'SCRIPT' ).layout( layout )

Button , values , event = window.read( )

enter = prog( ) and enter in values['For_EXIT']

print(f'enter: ['-IN-']')

2 Answers2

1

The purpose of an Output element is so that you can "print" and have the output go there. You don't need to call update on these elements. Calling print will put whatever you print there.

You want to be extremely careful with any operation that is lengthy. These should be run as threads.

The result is that if you want to display the result of your call to prog(), then add this line:

print(prog())

Mike from PSG
  • 5,312
  • 21
  • 39
0

Try this :

import PySimpleGUI as sg
import requests
from bs4 import BeautifulSoup
import re

sg.theme( 'Reddit' )


def prog() :
    r = requests.get( 'http://www.google.com' )
    html = r.text
    soup = BeautifulSoup( html , 'lxml' )

    links = soup.find_all( 'a' , attrs={'href' : re.compile('^http://')} )
    href = []
    for link in links :
        href.append(link['href'])
    return href


layout = [
[sg.Output( size=(38, 20) , key='For_EXIT' )] ,
[sg.Button('Read',key='Read') , sg.Button('Exit')]
]

window = sg.Window('SCRIPT',layout=layout)
while True:
    event, values = window.read()
    if event == 'Read' :
        return_value = prog()
        window['For_EXIT'].update(value=return_value)
    if event == 'Exit' or event == None:
        break    
Bhargav Desai
  • 941
  • 1
  • 5
  • 17