”;
First Window using PySimpleGUI
To check whether PySimpleGUI along with its dependencies are properly installed, enter the following code and save it as “hello.py”, using any Python-aware editor.
import PySimpleGUI as psg layout = [[psg.Text(text=''Hello World'', font=(''Arial Bold'', 20), size=20, expand_x=True, justification=''center'')], ] window = psg.Window(''HelloWorld'', layout, size=(715,250)) while True: event, values = window.read() print(event, values) if event in (None, ''Exit''): break window.close()
The above code constructs a window with a Text element (equivalent of a Label in TKinter) and displays the “Hello World” message placed centrally across the width of the window.
Run this program from the command terminal as −
Python hello.py
The output generated by the program should be similar to the one displayed below −
Equivalent Tkinter Code
To obtain similar output using pure Tkinter code, we would require the following Python script −
from tkinter import * window=Tk() lbl=Label(window, text="Hello World", fg=''white'', bg=''#64778D'', font=("Arial Bold", 20)) lbl.place(x=300, y=15) window.title(''HelloWorld Tk'') window[''bg'']=''#64778D'' window.geometry("715x250+10+10") window.mainloop()
All other functionalities remain same, except we use the serve() function off waitress module to start the WSGI server. On visiting the ”/” route in the browser after running the program, the Hello World message is displayed as before.
Instead of a function, a callable class can also be used as a View. A callable class is the one which overrides the __call__() method.
from pyramid.response import Response class MyView(object): def __init__(self, request): self.request = request def __call__(self): return Response(''hello world'')
PySimpleGUIQt
The object model of PySimpleGUI API has been made compatible with the widgets as defined in PySide2 package (which is the Python port for Qt graphics toolkit). The Qt version of PySimpleGui is called PySimpleGUIQt. It can be similarly installed with following PIP command −
pip3 install PySimpleGUIQt
Since this package depends on PySide2, the same will also be installed.
>>> import PySide2 >>> PySide2.__version__ ''5.15.2.1'' >>> import PySimpleGUIQt >>> PySimpleGUIQt.version ''0.35.0 Released 6-Jun-2020''
As mentioned earlier, the most important feature of PySimpleGui projects is that the code written for one package is completely compatible with the other. Hence, the hello.py program used earlier can be used as it is for the Qt version. The only change needed is import PySimpleGUIQt instead of PySimpleGui.
import PySimpleGUIQt as psg layout = [[psg.Text(text=''Hello World'', font=(''Arial Bold'', 20), justification=''center'')], ] window = psg.Window(''HelloWorldQt'', layout, size=(715,250)) while True: event, values = window.read() print(event, values) if event in (None, ''Exit''): break window.close()
The output is fairly similar.
Equivalent PySide2 Code
The pure PySide2 code to achieve the same result is as follows −
import sys from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * def window(): app = QApplication(sys.argv) w = QWidget() w.setStyleSheet("background-color: #64778D;") b = QLabel(w) b.setText("Hello World!") b.setFont(QFont(''Arial Bold'', 20)) b.setAlignment(Qt.AlignCenter) b.setStyleSheet("color: white;") b.setGeometry(100, 100, 715, 250) b.move(50, 20) w.setWindowTitle("HelloWorldQt") w.show() sys.exit(app.exec_()) if __name__ == ''__main__'': window()
It will produce the same output window.
PySimpleGUIWx
This module encapsulates the functionality of GUI widgets as defined in WxPython toolkit. WxPython is a Python port for the widely used WxWidgets library originally written in C++. Obviously, PySimpleGUIWx depends on WxPython package, and hence the latter will get automatically installed by the following PIP command −
pip3 install PySimpleGUIWx
To confirm that both PySimpleGUIWx and WxPython are properly installed, enter following statements in Python terminal.
>>> import PySimpleGUIWx >>> PySimpleGUIWx.version ''0.17.1 Released 7-Jun-2020'' >>> import wx >>> wx.__version__ ''4.0.7''
Not much of change is required in the “hello.py” script. We need to just replace PySimpleGUI with PySimpleGUIWx module in the “import” statement.
import PySimpleGUIWx as psg layout = [[psg.Text(text=''Hello World'', font=(''Arial Bold'', 20), size=(500, 5), justification=''center'')], ] window = psg.Window(''HelloWorldWx'', layout, size=(715, 250)) while True: event, values = window.read() print(event, values) if event in (None, ''Exit''): break window.close()
It will produce the following output:
Note that you’ll need a little more complex code to obtain the similar output with pure WxPython code as follows −
import wx app = wx.App() window = wx.Frame(None, title="WxPython", size=(715, 250)) panel = wx.Panel(window) panel.SetBackgroundColour((100, 119, 141)) label = wx.StaticText(panel, -1, style=wx.ALIGN_CENTER) label.SetLabel("Hello World") label.SetForegroundColour((255, 255, 255)) font = wx.Font() font.SetFaceName("Arial Bold") font.SetPointSize(30) label.SetFont(font) window.Show(True) app.MainLoop()
It will display a top level window with a text label having Hello World as the caption.
PySimpleGUIWeb
Remi (REMote Interface) is a GUI library for Python applications that are rendered in a web browser. PySimpleGUIWeb package ports the original PySimpleGui library to Remi so that its apps can be run in a browser. Following PIP command installs both PySimpleGUIWeb and Remi in the current Python environment −
pip3 install PySimpleGUIWeb
Check for their proper installation before writing an app.
>>> import PySimpleGUIWeb >>> PySimpleGUIWeb.version ''0.39.0 Released 6-Jun-2020''
Following script is the PySimpleGUIWeb version of the original Hello World program.
import PySimpleGUIWeb as psg layout = [[psg.Text(text=''Hello World'', font=(''Arial Bold'', 20), justification=''center'')]] window = psg.Window(''HelloWorldWeb'', layout) while True: event, values = window.read() print(event, values) if event in (None, ''Exit''): break window.close()
To obtain similar output using pure Remi library’s functionality is a little complex, as the following code shows:
import remi.gui as gui from remi import start, App class HelloWeb(App): def __init__(self, *args): super(HelloWeb, self).__init__(*args) def main(self): wid = gui.VBox(style={"background-color": "#64778D"}) self.lbl = gui.Label(''Hello World'', width=''100%'', height=''100%'', style={ "color":"white", "text-align": "center", "font-family": "Arial Bold", "font-size": "20px"} ) wid.append(self.lbl) return wid if __name__ == "__main__": start(HelloWeb, debug=True, address=''0.0.0.0'', port=0)
When we run these programs, the Remi server starts, a browser window automatically opens and the Hello World message is displayed.
Here we have seen the Hello World program written in the PySimpleGUI, PySimpleGUIQt, PySimpleGUIWx and PySimpleGUIWeb libraries. We can see that the widget library remains the same. Moreover, the same Hello world program, when written in pure Tkinter, PySide, WxPython and Remi respectively, becomes far more complex and tedious than the PySimpleGUI versions.
”;