PySimpleGUIPython GUI 工具包
PySimpleGUI 是一個 Python 軟件包,它使所有水平的 Python 程序員都能創(chuàng)建 GUI。將 tkinter、Qt、WxPython 和 Remi(基于瀏覽器)GUI 框架轉(zhuǎn)化為更簡單的界面。通過使用初學(xué)者理解的 Python 核心數(shù)據(jù)類型(列表和字典),簡化了窗口定義。通過將事件處理從基于回調(diào)的模式改為消息傳遞的模式,進一步簡化了程序。
PySimpleGUI 代碼比直接使用底層框架編寫更簡單、更短,因為 PySimpleGUI 為你實現(xiàn)了許多"boilerplate code"。此外,接口被簡化為需要盡可能少的代碼來獲得所需的結(jié)果。根據(jù)所使用的程序和框架,一個 PySimpleGUI 程序可能需要 1/2 到 1/10 的代碼來直接使用其中一個框架來創(chuàng)建一個相同的窗口。
雖然目標是封裝/隱藏你在上面運行的 GUI 框架所使用的特定對象和代碼,但如果需要,你可以直接訪問框架的附屬部件和窗口。如果一個設(shè)置或功能還沒有使用 PySimpleGUI的API 暴露或訪問,你就不會與框架隔閡。你可以在不直接修改 PySimpleGUI 包本身的情況下擴展功能。
你的代碼不需要有一個面向?qū)ο蟮募軜?gòu),這使得包可以被更多的人使用。雖然架構(gòu)簡單易懂,但它不一定限制你只能解決簡單的問題。
示例一:
一個簡單的 PySimpleGUI 程序剖析
PySimpleGUI 程序有 5 個部分
import PySimpleGUI as sg # Part 1 - The import
# Define the window's contents
layout = [ [sg.Text("What's your name?")], # Part 2 - The Layout
[sg.Input()],
[sg.Button('Ok')] ]
# Create the window
window = sg.Window('Window Title', layout) # Part 3 - Window Defintion
# Display and interact with the Window
event, values = window.read() # Part 4 - Event loop or Window.read call
# Do something with the information gathered
print('Hello', values[0], "! Thanks for trying PySimpleGUI")
# Finish up by removing from the screen
window.close() # Part 5 - Close the Window
該代碼生成了這個窗口:
示例二:
import PySimpleGUI as sg
# Define the window's contents
layout = [[sg.Text("What's your name?")],
[sg.Input(key='-INPUT-')],
[sg.Text(size=(40,1), key='-OUTPUT-')],
[sg.Button('Ok'), sg.Button('Quit')]]
# Create the window
window = sg.Window('Window Title', layout)
# Display and interact with the Window using an Event Loop
while True:
event, values = window.read()
# See if user wants to quit or window was closed
if event == sg.WINDOW_CLOSED or event == 'Quit':
break
# Output a message to the window
window['-OUTPUT-'].update('Hello ' + values['-INPUT-'] + "! Thanks for trying PySimpleGUI")
# Finish up by removing from the screen
window.close()
評論
圖片
表情
