python pysimplegui读取块主线程

mbyulnm0  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(147)

我尝试创建一个简单的GUI程序作为自动化脚本的状态指示器。GUI将在后台运行(但是是可见的),并且随着一些数据的处理而更新。我从pysimplegui开始,因为它更容易理解。然而,我注意到运行read会阻塞主线程,这会导致一些问题,因为程序应该处理键盘输入(使用pynput)如果需要的话。键盘记录程序是在一个单独的线程上工作正常,但主线程被暂停,所以没有任何变化是工作。我如何解决这个问题,使主运行如常,只是偶尔更新GUI窗口?
下面是我到目前为止编写的代码:
第一个月

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()
6jjcrrmo

6jjcrrmo1#

示例代码演示线程如何在主线程中使用GUI,使用Window的方法write_event_value生成主线程的事件。

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()

相关问题