Pyglet正在运行但没有响应esc命令

时间:2018-06-04 15:09:54

标签: python pyglet

我对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时,它立即被读取,但每次鼠标交互冻结应用程序:我无法关闭窗口,无法调整大小,它只是卡住了有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我不知道程序实际上做了什么,或者如何使用它。 由于我无法复制您的环境(例如,您有一个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()

这段代码中的一些运行时错误,我可能已经搞砸了。但是我已经将尽可能多的代码移植到面向对象的思维方式中。 Aka是一个继承Window类的类,用于修改其中的元素和窗口本身。

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

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

希望这有效。

相关问题