簡體   English   中英

pysimplegui 讀取塊主線程

[英]pysimplegui read blocks main thread

我正在嘗試創建一個簡單的 GUI 程序作為自動化腳本的狀態指示器。 GUI 將在后台運行(但可見)並隨着一些數據的處理而更新。 我從 pysimplegui 開始,因為它更容易理解。 但是,我注意到運行 read 會阻塞主線程。 這會導致問題,因為程序應該在需要時處理鍵盤輸入(使用 pynput)。 位於單獨線程上的鍵盤記錄器工作正常,但主線程已停止,因此未進行任何更改。 我該如何解決這個問題,以便 main 像往常一樣運行並且只是偶爾更新 GUI window?

這是我到目前為止編寫的代碼:

main.py

from src.components import gui, logger
from pynput import keyboard


def on_press(key):
    try:
        global start
        start = True
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))


if __name__ == "__main__":
    start = False

    listener = keyboard.Listener(on_press=on_press)
    listener.start()

    image_window = gui.create_image_window()
    gui.welcome_window(image_window)

    image_window.read()

    # This does not run. The read() blocks the program here 
    while not start:  
        print("Something")

gui.py

import PySimpleGUI as sg
import base64

icon = base64.b64encode(open('./src/resources/paper-clip.png', 'rb').read())


def create_text_window():
    sg.theme('DarkBlack1')
    layout = [[sg.Text("Example", key="-TEXT-")]]
    return sg.Window(title="Example", layout=layout, resizable=False, size=(500, 300), keep_on_top=True,
                     icon=icon, titlebar_icon=icon, margins=(0,0), element_padding=(0,0), finalize=True)


def create_image_window():
    image = sg.Image(size=(500, 300), key="-IMAGE-")
    layout = [[image]]
    return sg.Window(title="Example", layout=layout, resizable=False, size=(500, 300), keep_on_top=True,
                     icon=icon, titlebar_icon=icon,margins=(0,0), element_padding=(0,0), finalize=True)


def welcome_window(window):
    image = sg.Image("./src/resources/paper-clip.png",size=(500, 300))
    window['-IMAGE-'].update(filename="./src/resources/example.png")
    window.Refresh()

演示線程如何在主線程中使用 GUI 的示例代碼,使用 Window 的方法write_event_value向主線Window事件。

from pynput import keyboard
import PySimpleGUI as sg


def on_press(key, window):
    try:
        global start
        start = True
        print('alphanumeric key {0} pressed'.format(key.char))
    except AttributeError:
        print('special key {0} pressed'.format(key))
    window.write_event_value('KEY', key)

def create_window():
    layout = [[sg.Image(key="-IMAGE-"), sg.Text("Wait the key pressed !", size=25, key='Status')]]
    return sg.Window("Title", layout=layout, finalize=True)

def welcome_window(window):
    window['-IMAGE-'].update(data=sg.EMOJI_BASE64_HAPPY_LAUGH)
    window.Refresh()

start = False

window = create_window()
welcome_window(window)

listener = keyboard.Listener(on_press=lambda key, win=window:on_press(key, win))
listener.start()

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'KEY':
        key = values[event]
        window['Status'].update(f"'{key}' key pressed !")

listener.stop()
window.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM