GUI 实例
PySimpleGUI 在GitHub上的地址是:
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>https</strong></span>:<span style="color:#888888">//github.com/PySimpleGUI/PySimpleGUI</span>
</span></span>
大家可以访问看看,其首页是这样的:
有很多内容是不是?
这里面有一个重要的内容,在 DemoPrograms 文件夹下,这个文件夹是作者写的一些 demo 实例。作者真的是深谙我们这些懒虫的心理,即使有了这么简单好用的 GUI 框架,到了要写实例的时候,我们也可能会去网络上搜索实例,然后采用 。框架作者可能料想到这一点,所以他自己写了很多不同的实例,让你真正的拿来即用。CV大法
这个文件夹下大概有300多个实例,基本上可以囊括我们平时使用 python 写 GUI 所能遇到的各种组件和布局了。
CV 几个看看
有了这个神器,我们只需要把这个 GitHub 上的项目给复制到本地,然后将这些实例运行一遍,大致知道每个实例u哪些内容。后续当我们自己要写 GUI 时,我们只需要找到对应的实例,然后复制代码就可以了。是不是很简单?
下面我来运行几个 demo ,给大家展示一下这里面的实例都是什么样子的。
聊天界面
我们先复制一下源码:
<span style="color:#444444"><span style="background-color:#f6f6f6"><code>#!/usr/bin/env python
<strong>import</strong> PySimpleGUI <strong><span style="color:#333333"><strong>as</strong></span></strong> sg
<span style="color:#880000">''</span><span style="color:#880000">'</span>
<span style="color:#880000">A chatbot with history</span>
<span style="color:#880000">Scroll up and down through prior commands using the arrow keys</span>
<span style="color:#880000">Special keyboard keys:</span>
<span style="color:#880000"> Up arrow - scroll up in commands</span>
<span style="color:#880000"> Down arrow - scroll down in commands</span>
<span style="color:#880000"> Escape - clear current command</span>
<span style="color:#880000"> Control C - exit form</span>
<span style="color:#880000">'</span><span style="color:#880000">''</span>
<strong>def</strong> <strong>ChatBotWithHistory</strong>():
# ------- Make <span style="color:#333333"><strong>a</strong></span> <span style="color:#333333"><strong>new</strong></span> Window ------- #
# give our form <span style="color:#333333"><strong>a</strong></span> spiffy <span style="color:#333333"><strong>set</strong></span> of colors
sg.theme(<span style="color:#880000">'GreenTan'</span>)
layout = [[sg.Text(<span style="color:#880000">'Your output will go here'</span>, size=(<span style="color:#880000">40</span>, <span style="color:#880000">1</span>))],
[sg.Output(size=(<span style="color:#880000">127</span>, <span style="color:#880000">30</span>), font=(<span style="color:#880000">'Helvetica 10'</span>))],
[sg.Text(<span style="color:#880000">'Command History'</span>),
sg.Text(<span style="color:#880000">''</span>, size=(<span style="color:#880000">20</span>, <span style="color:#880000">3</span>), key=<span style="color:#880000">'history'</span>)],
[sg.ML(size=(<span style="color:#880000">85</span>, <span style="color:#880000">5</span>), enter_submits=<strong>True</strong>, key=<span style="color:#880000">'query'</span>, do_not_clear=<strong>False</strong>),
sg.Button(<span style="color:#880000">'SEND'</span>, button_color=(sg.YELLOWS[<span style="color:#880000">0</span>], sg.BLUES[<span style="color:#880000">0</span>]), bind_return_key=<strong>True</strong>),
sg.Button(<span style="color:#880000">'EXIT'</span>, button_color=(sg.YELLOWS[<span style="color:#880000">0</span>], sg.GREENS[<span style="color:#880000">0</span>]))]]
window = sg.Window(<span style="color:#880000">'Chat window with history'</span>, layout,
default_element_size=(<span style="color:#880000">30</span>, <span style="color:#880000">2</span>),
font=(<span style="color:#880000">'Helvetica'</span>, <span style="color:#880000">' 13'</span>),
default_button_element_size=(<span style="color:#880000">8</span>, <span style="color:#880000">2</span>),
return_keyboard_events=<strong>True</strong>)
# ---===--- Loop taking in user <span style="color:#397300">input</span> <span style="color:#397300">and</span> using it --- #
command_history = []
history_offset = <span style="color:#880000">0</span>
<strong><span style="color:#333333"><strong>while</strong></span></strong> <strong>True</strong>:
event, value = window.<span style="color:#333333"><strong>read</strong></span>()
<strong><span style="color:#333333"><strong>if</strong></span></strong> event == <span style="color:#880000">'SEND'</span>:
query = value[<span style="color:#880000">'query'</span>].rstrip()
# EXECUTE YOU