Ok, let’s explore this python module to make graphic user interfases to interact with users. This is so good and simple. You know I love tkinter and I always used it, but let’s see what this module has to offer to use. First of all, let’s install it.
pip install pysimplegui
It’s very fast to install and lightweight.
Let’s see the first example from their documentation.
import PySimpleGUI as sg sg.theme('DarkAmber') # Add a touch of color # All the stuff inside your window. layout = [ [sg.Text('Some text on Row 1')], [sg.Text('Enter something on Row 2'), sg.InputText()], [sg.Button('Ok'), sg.Button('Cancel')] ] # Create the Window window = sg.Window('Window Title', layout) # Event Loop to process "events" and get the "values" of the inputs while True: event, values = window.read() if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel break print('You entered ', values[0]) window.close()
So:
- you choose a theme
- define a list of widgets:
- a text in the first row
- a text and an InputText in the second
- 2 buttons in the third
Then you create the window, giving the title and assigning the layout (the list of widgets)
Then you need to create your loop to make the window ‘active’.
The user event are read with window.read() (where window is the istance of Window class defined before).
If you type something into the InputText it will be memorized into values[0] apparently.
See you in the next post for other discoveries about this module that seems nice.
GUI to make the pc talk with gtts (google text-to-speech API)
This is from the repository of pysimplegui. I made some script like this, but with tkinter. So let’s see this interesting app to use the google text-to-speech api to convert text to mp3. In this case the author of the code used pygame to make the speech audible.
#!/usr/bin/env python import PySimpleGUI as sg from gtts import gTTS from pygame import mixer import time import os ''' Simple demonstration of using Google Text to Speech Get a multi-line string Convert to speech Play back the speech Note that there are 2 temp files created. The program tries to delete them but will fail on one of them ''' layout = [[sg.Text('What would you like me to say?')], [sg.MLine(size=(60,10), enter_submits=True)], [sg.Button('Speak', bind_return_key=True), sg.Exit()]] window = sg.Window('Google Text to Speech', layout) i = 0 mixer.init() while True: event, values = window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # Get the text and convert to mp3 file tts = gTTS(text=values[0], lang='en',slow=False) tts.save('speech{}.mp3'.format(i%2)) # playback the speech mixer.music.load('speech{}.mp3'.format(i%2)) mixer.music.play() # wait for playback to end while mixer.music.get_busy(): time.sleep(.1) mixer.stop() i += 1 window.close() # try to remove the temp files. You'll likely be left with 1 to clean up try: os.remove('speech0.mp3') except: pass try: os.remove('speech1.mp3') except: pass
Twitter: @pythonprogrammi - python_pygame