http://www.techradar.com/pro/linux May 2019LXF249 53
Search Google TuTorialS
At the end
it chooses a
random website
to visit from
the suggestions
stored.
When the project starts, it instructs the user on how it will work,
using a msgbox.
EasyGUIvsGUIzEro
reaches three, then it will stop. Each time the loop goes
round it will use an enterbox from the easygui library,
which is a dialogue box into which we can type, to
capture the user input. This is then appended to our
things list, which means it is added to the list.
for i in range(3):
things.append(eg.enterbox(msg=”READY >>> “))
We now have the items to search for, but we need to
convert them from list into a string – this is for our
Google search later. To do this we create a variable
called inspiration and in it we store the converted
items from the things list, starting with 0, then 1 and 2.
This is all wrapped inside a string-formatting function
that ensures the text is saved correctly. We then print
this to the Python shell for debug.
inspiration = str(things[0]+” “+things[1]+” “+things[2])
print(inspiration) #Debug
Round and round we go
Time for another for loop, and this time we use it
to perform a Google search. For this we use the
inspiration variable, since it contains the search terms.
We then instruct the search to use the Google TLD (top
level domain) for our country, in our case co.uk.
The num value limits the number of links returned.
The stop is a Boolean value tells our search to stop
when it has enough links. Lastly, the pause value is set
to two seconds between each search, otherwise Google
may block our computer from using its service. Each
time the for loop goes round, it prints the returned
search item (the URL) and also appends the search
item to the suggestion list we created earlier.
for item in search(inspiration, tld=”co.uk”, num=3,
stop=1, pause=2):
print(item)
suggestion.append(item)
Moving along, we have another for loop and this
time we use it to convert our suggestion list into a
multi-line string, a paragraph. We do this for the length
( len ) of the list. Creating a variable called links to
store the multi-line string, we instruct Python to insert a
new line \n and then join the text together, starting
from index 0 in our suggestion list. This is called slicing,
and by passing the value 0: we instruct the slice to
start at 0.
for i in range(len(suggestion)):
links = “\n”.join(suggestion[0:])
Now that we have the multi-line string we can use it
in easygui’s textbox function. This function takes a
message ( msg ) and the text that we wish to show, in
this case the contents of the links variable.
eg.textbox(msg=”Hey I found these that might be of
interest, I’ll open one at random after you close this
message”, text=links)
The final line of code opens the default web browser
for Raspbian, but the link it opens will be a random
choice from the suggestion list we created earlier.
webbrowser.open(choice(suggestion))
With the code completed, click Run > Run Module to
start. You will now have no excuses when asked to
devise a new project. Have fun!
If you have been hacking around with the Pi for a while, you will know
of guizero, written by Laura Sach who works for the Raspberry Pi
Foundation(https://lawsie.github.io/guizero).It’sagreatlibrary
whichsimplifiesthewayinwhichwecancreateTkinter-basedlayouts
formanytypesofGUIapplication.WeuseditinLxF242 to create our
barcode scanner/Amazon book search app. So you might be
thinking, why did we choose easygui over guizero for this project?
Well, it comes down to our needs. We just needed a few dialogue
boxes to capture user input, and display the output from the Google
search. We can do this with guizero, but it would not be three simple
lines – there would be a few more lines, but still with simple code.
We have used easygui for a few years, and yes, it is rather old and
a trifle too simple for most projects. But for a really simple ‘Ask a
question, return an answer’ project, it cannot be beaten. To learn
more about easygui and how you can use it via a series of simple
tutorials, head over to its Read The Docs page and follow their work:
https://easygui.readthedocs.io/en/latest.
GEt yoUr piFILLInGhErE Subscribe now at http://bit.ly/LinuxFormat
Whenusingthe
easyguilibrary,
starttyping
thenameofa
function,for
example‘eg.
enterbox(‘anda
helpfultooltip
willappearto
showthesyntax
forthatfunction.
thisis common
forthemajority
ofpython
librariesandis
veryhelpful.