繁体   English   中英

Pyglet运行但不响应esc命令

[英]Pyglet running but not responding to esc commands

我是Python和Pyglet的新手,我试图创建一个应用程序,该应用程序根据通过串行发送的命令ID来动态显示照片。 这是我的代码:

import pyglet
from pyglet import clock
import serial
import json
import os

base_dir = 'data'
data = []
currentDatum = ''

def initialiseData():
    global data
    #load the json
    with open('dataset.json') as f:
        data = json.load(f)
    #for every file in the json load the image
    for d in data:
        d['media'] = pyglet.image.load(os.path.join(base_dir, d['name']))

    print("Scan a tag")

def readSerial(dt):
    global currentDatum
    tag = ser.readline()
    tag = tag.strip()
    for d in data:
        if d['id'] == tag:
            currentDatum = d
            print(currentDatum)



ser = serial.Serial('/dev/cu.usbmodem1421', 9600)

initialiseData()

window = pyglet.window.Window(1000, 800, resizable = True)


@window.event
def on_draw():
    window.clear()
    currentDatum['media'].anchor_x = currentDatum['media'].width/2 - window.width/2
    currentDatum['media'].anchor_y = currentDatum['media'].height/2 - window.height/2
    currentDatum['media'].blit(0, 0)


clock.schedule(readSerial)
pyglet.app.run()

该应用程序运行良好,从某种意义上来说,它从儿子那里加载数据,当我发送序列ID时,它立即被读取,但是每次鼠标交互都会冻结该应用程序:我无法关闭窗口,无法调整其大小,只是卡住了。 有什么建议吗?

我不知道该程序实际上在做什么,或者如何使用它。 而且由于我无法复制您的环境(例如,您有一个usbmodem),所以我将在一种通用的解决方案中尝试最好的方法,该解决方案应该可以正常工作并为您的项目的未来开发进行规划:

import pyglet
from pyglet.gl import *

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)
        self.x, self.y = 0, 0

        self.batch = pyglet.graphics.Batch()
        self.data = []
        self.currentDatum = ''
        self.ser = serial.Serial('/dev/cu.usbmodem1421', 9600)

        self.initialiseData()

        pyglet.clock.schedule(self.readSerial)

        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_mouse_motion(self, x, y, dx, dy):
        pass

    def on_mouse_release(self, x, y, button, modifiers):
        pass

    def on_mouse_press(self, x, y, button, modifiers):
        pass

    def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
        pass

    def on_key_release(self, symbol, modifiers):
        pass

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

    def render(self):
        self.clear()

        # For future reference, use batches:
        self.batch.draw()
        # But this is how it's implemented atm:
        self.currentDatum['media'].anchor_x = self.currentDatum['media'].width/2 - self.width/2
        self.currentDatum['media'].anchor_y = self.currentDatum['media'].height/2 - self.height/2
        self.currentDatum['media'].blit(0, 0)

        self.flip()

    def initialiseData(self):
        #load the json
        with open('dataset.json') as f:
            self.data = json.load(f)
        #for every file in the json load the image
        for d in self.data:
            d['media'] = pyglet.image.load(os.path.join(base_dir, d['name']))

        print("Scan a tag")

    def readSerial(self, dt):
        tag = self.ser.readline()
        tag = tag.strip()
        for d in self.data:
            if d['id'] == tag:
                self.currentDatum = d
                print(self.currentDatum)

    def run(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()

这段代码中肯定会出现一些运行时错误,这些语法我可能已经搞砸了。 但是,我已经将您尽可能多的代码移植到面向对象的思维方式中。 又是一个继承Window类的类,以便您修改其中的元素以及窗口本身。

您还可以使用一些占位符功能来处理鼠标和键盘事件。 这些是继承/重叠pyglet.window.Window类。 因此, Window支持的任何功能都可以放入此类。

还有一个自定义的pyglet.app.run()处理事件轮询,以确保您不会卡在键盘,鼠标或窗口(调整大小,移动等)事件上。

希望这行得通。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM