# 介绍
基于PySimpleGUI的一个简单的事件循环库
# 为什么要用这个库
为啥要搞这玩意,这玩意具体是用来干嘛的?
在GUI开发中肯定会遇到事件以及事件调用的函数,这个在pysimplegui里面使用的并不好,以下是一个例子
~~~python
if event == "btn1":
do_something()
~~~
以上需要写很多冗长的if else 语句,而本库则提供了一种更简单的方法,只需要编写一个函数,然后在这个函数上使用装饰器,就可以实现事件的函数的绑定
~~~python
@app.bind_event('btn1')
def do_something():
pass
~~~
## 安装
~~~bash
pip install pyguievent
~~~
## 版本兼容性
python3.4以上,WindowsXP 以上的Windows系统都可以用,其他系统未测试
## 特别说明
事件绑定和事件函数的有点问题,只能接受以下四种形式
1. func(),这种无参数函数
2. func(values=),这种关键字参数函数,只有一个参数是值
3. func(window=),这种关键字参数函数,只有一个参数是窗口
4. func(event=),这种关键字参数函数,只有一个参数是事件
5. 参数有window和event,values,顺序无所谓
6. sg.Window 窗口对象必须要使用finalize=True
## 使用方法
~~~python
import PySimpleGUI as sg
from pyguievent import PySimpleEvent
app = PySimpleEvent()
def make_main_window() -> sg.Window:
task_list = [
sg.Frame(
"Tasks",
[
[sg.Input(key="lot1", tooltip="此输入栏输入即事件触发", enable_events=True)],
[sg.Input(key="lot2", tooltip="此输入框输入后使用回车事件触发")],
[sg.B("win_fun"), sg.B("val_fun")],
[
sg.B("win_val_fun"),
sg.B("no_arg_fun"),
],
[sg.Text("", key="lot3")],
[sg.Text("", key="lot4")],
],
size=(400, 200),
)
]
status_bar = [
sg.StatusBar("版本:"),
sg.StatusBar("状态:"),
]
menu_def = [
["&程序", ["关于", "保存配置", "Exit"]],
["&帮助", "&使用说明..."],
]
top_menu = [sg.Menu(menu_def)]
layout = [top_menu, task_list, status_bar]
# 3.建窗口
return sg.Window(
"测试程式窗口",
layout,
keep_on_top=True,
finalize=True,# 这个属性是必须要的
return_keyboard_events=True,
)
@app.bind_event('保存配置')
def on_save_options():
sg.popup("you click save options")
@app.bind_event('lot1')
def on_input_lot1(window: sg.Window, values: dict):
window["lot4"].update(values.get("lot1"))
@app.bind_event('lot2')
def on_enter_lot2(window: sg.Window, values: dict):
new_text = "您输入的是:{}".format(values.get("lot2"))
window["lot3"].update(new_text)
@app.bind_event(["no_arg_fun", "关于"])
def on_no_arg_fun():
sg.popup(
"软件说明:",
"关于事件,点击菜单关于启动",
title="关于程序",
keep_on_top=True,
)
@app.bind_event("win_fun")
def on_win_fun(window: sg.Window):
sg.popup(
"软件说明:",
"新建任务",
title="关于程序",
keep_on_top=True,
)
window["lot2"].set_focus(force=True)
@app.bind_event("val_fun")
def on_val_fun(values):
lot1 = values.get("lot1")
lot2 = values.get("lot2")
sg.popup(
"软件说明:",
"新建任务",
lot1,
lot2,
title="关于程序",
keep_on_top=True,
)
@app.bind_event(["win_val_fun", "lot2"])
def on_win_val_fun(window, values):
sg.popup(
"软件说明:",
"新建任务",
title="关于程序",
keep_on_top=True,
)
lot1 = values.get("lot1")
window["lot3"].update(lot1)
def main():
main_window = make_main_window()
# 增加了一个Exit退出事件
app.run_event(main_window, "Exit")
if __name__ == '__main__':
main()
~~~
Raw data
{
"_id": null,
"home_page": "https://github.com/yunluo/pyguievent",
"name": "pyguievent",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": "pyguievent, PySimpleEvent, PySimpleGUI, FreeSimpleGUI, GUI, events for simplegui",
"author": "yunluo",
"author_email": "sp91@qq.com",
"download_url": "https://files.pythonhosted.org/packages/14/58/5c8e03a3409c45a2cc533ae68abdc5983f37dda8acdb704a4fab929fc6ef/pyguievent-0.0.3.tar.gz",
"platform": null,
"description": "# \u4ecb\u7ecd\n\u57fa\u4e8ePySimpleGUI\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u4ef6\u5faa\u73af\u5e93\n\n# \u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u5e93\n\u4e3a\u5565\u8981\u641e\u8fd9\u73a9\u610f,\u8fd9\u73a9\u610f\u5177\u4f53\u662f\u7528\u6765\u5e72\u561b\u7684\uff1f\n\u5728GUI\u5f00\u53d1\u4e2d\u80af\u5b9a\u4f1a\u9047\u5230\u4e8b\u4ef6\u4ee5\u53ca\u4e8b\u4ef6\u8c03\u7528\u7684\u51fd\u6570,\u8fd9\u4e2a\u5728pysimplegui\u91cc\u9762\u4f7f\u7528\u7684\u5e76\u4e0d\u597d,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\n~~~python\nif event == \"btn1\":\n do_something()\n~~~\n\u4ee5\u4e0a\u9700\u8981\u5199\u5f88\u591a\u5197\u957f\u7684if else \u8bed\u53e5\uff0c\u800c\u672c\u5e93\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u51fd\u6570\u4e0a\u4f7f\u7528\u88c5\u9970\u5668,\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e8b\u4ef6\u7684\u51fd\u6570\u7684\u7ed1\u5b9a\n\n~~~python\n@app.bind_event('btn1')\ndef do_something():\n pass\n~~~\n\n## \u5b89\u88c5\n~~~bash\npip install pyguievent\n~~~\n\n## \u7248\u672c\u517c\u5bb9\u6027\npython3.4\u4ee5\u4e0a,WindowsXP \u4ee5\u4e0a\u7684Windows\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\uff0c\u5176\u4ed6\u7cfb\u7edf\u672a\u6d4b\u8bd5\n\n## \u7279\u522b\u8bf4\u660e\n\u4e8b\u4ef6\u7ed1\u5b9a\u548c\u4e8b\u4ef6\u51fd\u6570\u7684\u6709\u70b9\u95ee\u9898\uff0c\u53ea\u80fd\u63a5\u53d7\u4ee5\u4e0b\u56db\u79cd\u5f62\u5f0f\n1. func()\uff0c\u8fd9\u79cd\u65e0\u53c2\u6570\u51fd\u6570\n2. func(values=)\uff0c\u8fd9\u79cd\u5173\u952e\u5b57\u53c2\u6570\u51fd\u6570\uff0c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u662f\u503c\n3. func(window=)\uff0c\u8fd9\u79cd\u5173\u952e\u5b57\u53c2\u6570\u51fd\u6570\uff0c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u662f\u7a97\u53e3\n4. func(event=)\uff0c\u8fd9\u79cd\u5173\u952e\u5b57\u53c2\u6570\u51fd\u6570\uff0c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u662f\u4e8b\u4ef6\n5. \u53c2\u6570\u6709window\u548cevent,values,\u987a\u5e8f\u65e0\u6240\u8c13\n6. sg.Window \u7a97\u53e3\u5bf9\u8c61\u5fc5\u987b\u8981\u4f7f\u7528finalize=True\n\n\n## \u4f7f\u7528\u65b9\u6cd5\n\n~~~python\nimport PySimpleGUI as sg\nfrom pyguievent import PySimpleEvent\n\n\napp = PySimpleEvent()\n\n\ndef make_main_window() -> sg.Window:\n task_list = [\n sg.Frame(\n \"Tasks\",\n [\n [sg.Input(key=\"lot1\", tooltip=\"\u6b64\u8f93\u5165\u680f\u8f93\u5165\u5373\u4e8b\u4ef6\u89e6\u53d1\", enable_events=True)],\n [sg.Input(key=\"lot2\", tooltip=\"\u6b64\u8f93\u5165\u6846\u8f93\u5165\u540e\u4f7f\u7528\u56de\u8f66\u4e8b\u4ef6\u89e6\u53d1\")],\n [sg.B(\"win_fun\"), sg.B(\"val_fun\")],\n [\n sg.B(\"win_val_fun\"),\n sg.B(\"no_arg_fun\"),\n ],\n [sg.Text(\"\", key=\"lot3\")],\n [sg.Text(\"\", key=\"lot4\")],\n ],\n size=(400, 200),\n )\n ]\n\n status_bar = [\n sg.StatusBar(\"\u7248\u672c:\"),\n sg.StatusBar(\"\u72b6\u6001:\"),\n ]\n\n menu_def = [\n [\"&\u7a0b\u5e8f\", [\"\u5173\u4e8e\", \"\u4fdd\u5b58\u914d\u7f6e\", \"Exit\"]],\n [\"&\u5e2e\u52a9\", \"&\u4f7f\u7528\u8bf4\u660e...\"],\n ]\n\n top_menu = [sg.Menu(menu_def)]\n\n layout = [top_menu, task_list, status_bar]\n\n # 3.\u5efa\u7a97\u53e3\n return sg.Window(\n \"\u6d4b\u8bd5\u7a0b\u5f0f\u7a97\u53e3\",\n layout,\n keep_on_top=True,\n finalize=True,# \u8fd9\u4e2a\u5c5e\u6027\u662f\u5fc5\u987b\u8981\u7684\n return_keyboard_events=True,\n )\n\n\n@app.bind_event('\u4fdd\u5b58\u914d\u7f6e')\ndef on_save_options():\n sg.popup(\"you click save options\")\n\n\n@app.bind_event('lot1')\ndef on_input_lot1(window: sg.Window, values: dict):\n window[\"lot4\"].update(values.get(\"lot1\"))\n\n\n@app.bind_event('lot2')\ndef on_enter_lot2(window: sg.Window, values: dict):\n new_text = \"\u60a8\u8f93\u5165\u7684\u662f\uff1a{}\".format(values.get(\"lot2\"))\n window[\"lot3\"].update(new_text)\n\n\n@app.bind_event([\"no_arg_fun\", \"\u5173\u4e8e\"])\ndef on_no_arg_fun():\n sg.popup(\n \"\u8f6f\u4ef6\u8bf4\u660e\uff1a\",\n \"\u5173\u4e8e\u4e8b\u4ef6\uff0c\u70b9\u51fb\u83dc\u5355\u5173\u4e8e\u542f\u52a8\",\n title=\"\u5173\u4e8e\u7a0b\u5e8f\",\n keep_on_top=True,\n )\n\n\n@app.bind_event(\"win_fun\")\ndef on_win_fun(window: sg.Window):\n sg.popup(\n \"\u8f6f\u4ef6\u8bf4\u660e\uff1a\",\n \"\u65b0\u5efa\u4efb\u52a1\",\n title=\"\u5173\u4e8e\u7a0b\u5e8f\",\n keep_on_top=True,\n )\n window[\"lot2\"].set_focus(force=True)\n\n\n@app.bind_event(\"val_fun\")\ndef on_val_fun(values):\n lot1 = values.get(\"lot1\")\n lot2 = values.get(\"lot2\")\n sg.popup(\n \"\u8f6f\u4ef6\u8bf4\u660e\uff1a\",\n \"\u65b0\u5efa\u4efb\u52a1\",\n lot1,\n lot2,\n title=\"\u5173\u4e8e\u7a0b\u5e8f\",\n keep_on_top=True,\n )\n\n\n@app.bind_event([\"win_val_fun\", \"lot2\"])\ndef on_win_val_fun(window, values):\n sg.popup(\n \"\u8f6f\u4ef6\u8bf4\u660e\uff1a\",\n \"\u65b0\u5efa\u4efb\u52a1\",\n title=\"\u5173\u4e8e\u7a0b\u5e8f\",\n keep_on_top=True,\n )\n lot1 = values.get(\"lot1\")\n window[\"lot3\"].update(lot1)\n\n\ndef main():\n main_window = make_main_window()\n\n # \u589e\u52a0\u4e86\u4e00\u4e2aExit\u9000\u51fa\u4e8b\u4ef6\n app.run_event(main_window, \"Exit\")\n\nif __name__ == '__main__':\n main()\n\n~~~\n",
"bugtrack_url": null,
"license": "GPL-3.0 license",
"summary": "A simple tool to create events to PySimpleGUI or FreeSimpleGUI",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/yunluo/pyguievent",
"Source": "https://github.com/yunluo/pyguievent"
},
"split_keywords": [
"pyguievent",
" pysimpleevent",
" pysimplegui",
" freesimplegui",
" gui",
" events for simplegui"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ddc4ea83991732f53fc88734cee76a0c76d377bf8ba91b5d2b1600713b73e82d",
"md5": "080c6830c0c423af29204419f45da5d1",
"sha256": "3e0ca8553c38d30eea3227d8e5f9cd28f03bca3b6b748f31a5b2882d358b7f28"
},
"downloads": -1,
"filename": "pyguievent-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "080c6830c0c423af29204419f45da5d1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 17937,
"upload_time": "2024-11-20T14:57:07",
"upload_time_iso_8601": "2024-11-20T14:57:07.340839Z",
"url": "https://files.pythonhosted.org/packages/dd/c4/ea83991732f53fc88734cee76a0c76d377bf8ba91b5d2b1600713b73e82d/pyguievent-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "14585c8e03a3409c45a2cc533ae68abdc5983f37dda8acdb704a4fab929fc6ef",
"md5": "6245cf8da67c52cd87d539e958a0d595",
"sha256": "b96462572ed027a2f94621c2080f207fa4d6d08047a08949e97ff33cbccd15b6"
},
"downloads": -1,
"filename": "pyguievent-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "6245cf8da67c52cd87d539e958a0d595",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 17848,
"upload_time": "2024-11-20T14:57:08",
"upload_time_iso_8601": "2024-11-20T14:57:08.829448Z",
"url": "https://files.pythonhosted.org/packages/14/58/5c8e03a3409c45a2cc533ae68abdc5983f37dda8acdb704a4fab929fc6ef/pyguievent-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-20 14:57:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yunluo",
"github_project": "pyguievent",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyguievent"
}