Pysimplegui 简明教程
PySimpleGUI - Hello World
First Window using PySimpleGUI
要检查 PySimpleGUI 及其依赖关系是否已正确安装,请输入以下代码并使用任何 Python 感知编辑器将其保存为 "hello.py"。
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()
上述代码构建一个窗口,其中包含一个文本元素(相当于 TKinter 中的标签),并在窗口的宽度上居中显示 "Hello World" 消息。
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
该程序生成的 output 应该与下面显示的类似 -
The output generated by the program should be similar to the one displayed below −

Equivalent Tkinter Code
要使用纯 Tkinter 代码获得类似的输出,我们需要以下 Python 脚本 -
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()
除了我们使用 waitress 模块中的 serve() 函数来启动 WSGI 服务器以外,所有其他功能都保持不变。在运行程序后访问浏览器中的 '/' 路由时,Hello World 消息会像以前一样显示。
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.
可以将可调用类用作视图,而不要使用函数。可调用类是覆盖 call() 方法的那个类。
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
PySimpleGUI API 的对象模型已与 PySide2 包(这是 Qt 图形工具包的 Python 移植)中定义的小部件兼容。PySimpleGui 的 Qt 版本称为 PySimpleGUIQt。它可以通过以下 PIP 命令进行类似安装:
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
由于此包依赖于 PySide2,因此也会安装它。
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'
如前所述,PySimpleGui 项目最重要的特性是,为一个包编写的代码与另一个包完全兼容。因此,较早使用的 hello.py 程序可以按原样用于 Qt 版本。唯一需要的更改是导入 PySimpleGUIQt 而不是 PySimpleGUI。
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()
output 非常相似。
The output is fairly similar.

Equivalent PySide2 Code
实现相同结果的纯 PySide2 代码如下:
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
此模块封装了 WxPython 工具包中定义的 GUI 小部件的功能。WxPython 是针对广泛使用的 WxWidgets 库(最初用 C++ 编写)的 Python 端口。显然,PySimpleGUIWx 依赖于 WxPython 包,因此后者将通过以下 PIP 命令自动安装:
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
要确认 PySimpleGUIWx 和 WxPython 都已正确安装,请在 Python 终端中输入以下语句。
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'
“hello.py”脚本中不需要太多更改。我们只需要在“import”语句中用 PySimpleGUIWx 模块替换 PySimpleGUI。
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()
它将生成以下 output :
It will produce the following output:

请注意,您需要更复杂的代码才能使用 pure WxPython code 获得类似输出,如下所示:
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()
它将显示一个带有文本标签且标题为 Hello World 的顶级窗口。
It will display a top level window with a text label having Hello World as the caption.
PySimpleGUIWeb
Remi(远程接口)是用于 Python 应用程序的 GUI 库,这些应用程序在 Web 浏览器中呈现。PySimpleGUIWeb 包将原始的 PySimpleGui 库移植到了 Remi,因此其应用程序可以在浏览器中运行。以下 PIP 命令在当前 Python 环境中同时安装 PySimpleGUIWeb 和 Remi:
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'
以下脚本是原始 Hello World 程序的 PySimpleGUIWeb 版本。
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()
要使用纯 Remi 库的功能获取类似输出,则比较复杂,如下代码所示:
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)
当我们运行这些程序时,Remi 服务器启动,浏览器窗口自动打开并显示 Hello World 消息。
When we run these programs, the Remi server starts, a browser window automatically opens and the Hello World message is displayed.

在这里,我们看到了使用 PySimpleGUI、PySimpleGUIQt、PySimpleGUIWx 和 PySimpleGUIWeb 库编写的 Hello World 程序。我们可以看到小部件库保持不变。此外,相同的 Hello World 程序,当分别用纯 Tkinter、PySide、WxPython 和 Remi 编写时,比 PySimpleGUI 版本复杂且繁琐得多。
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.