繁体   English   中英

Python Kivy 图像按事件旋转

[英]Python Kivy Image rotate by event

请帮助在def read_sok():函数中按事件旋转图像。

访问TestPY().update()不会旋转图像。 执行print("update")字符串

旋转图像的角度从服务器发送

正在开发该应用程序以模拟开关设备

from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.graphics import Rotate
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.graphics.context_instructions import PopMatrix, PushMatrix

# Socket client example in python

import socket
import sys
import threading

host = '192.168.12.95'
port = 2000  # web

a = 0
b = 0
c = 0

# create socket
print('# Creating socket')
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print('Failed to create socket')
    sys.exit()

print('# Getting remote IP address')
try:
    remote_ip = socket.gethostbyname(host)
except socket.gaierror:
    print('Hostname could not be resolved. Exiting')
    sys.exit()

# Connect to remote server
print('# Connecting to server, ' + host + ' (' + remote_ip + ')')
s.connect((remote_ip, port))

# Send data to remote server
print('# Sending data to server')
request = "hello server"

try:
    s.sendall(request.encode('utf-8'))
except socket.error:
    print('Send failed')
    sys.exit()

# Receive data
print('# Receive data from server')
reply = s.recv(4096)

print(reply)


def read_sok():
    global a, b, c
    while 1:
        data = s.recv(1024)
        print(data.decode('utf-8'))
        b = int(data.decode('utf-8'))
        TestPY().update()


potok = threading.Thread(target=read_sok)
potok.start()


class TestKV(Image):
    angle = NumericProperty(0)


Builder.load_string('''
<TestPY>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin: root.center
    canvas.after:
        PopMatrix
''')


class TestPY(Image):
    global a, b, c
    angle = NumericProperty(0)

    def __init__(self, **kwargs):
        super(TestPY, self).__init__(**kwargs)
        # self.x = x -- not necessary, x is a property and will be handled by super()
        with self.canvas.before:
            PushMatrix()
            self.rot = Rotate()
            self.rot.angle = 0
            self.rot.origin = self.center
            self.rot.axis = (0, 0, 1)

        with self.canvas.after:
            PopMatrix()

    def on_touch_down(self, touch):
        self.rot.angle = b

    def update(self):
        self.rot.angle = b
        print("update")

    def dd(self):
        rot.angle = b
        print("dd")


class MainWidget(Widget):
    # this is the main widget that contains the game.

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.all_sprites = []

        self.k = TestKV(source="dd1.png", x=10, size=(400, 400))
        self.add_widget(self.k)

        self.p = TestPY(source="dd2.png", x=10, size=(400, 400))
        self.add_widget(self.p)


class TheApp(App):

    def build(self):
        parent = Widget()
        app = MainWidget()
        parent.add_widget(app)
        return parent


if __name__ == '__main__':
    TheApp().run()

在此处输入图片说明

我想这是因为您在“init”中定义了旋转角度。 当您首先运行 'TestPY().update()' 时,它会创建类的新对象,将其旋转到 0°,然后在旋转结束时启动更新方法。

您可以尝试在 init 方法中定义您的角度,也许这会起作用:

while 1:
    ...
    TestPY(b)


class TestPY(Image):
    ...
    def __init__(self, b, **kwargs):
        super(TestPY, self).__init__(**kwargs)

        with self.canvas.before:
            PushMatrix()
            self.rot = Rotate()
            self.rot.angle = b
            self.rot.origin = self.center
            self.rot.axis = (0, 0, 1)

或者您可以将角度定义为 NumericProperty() 并且不要在无限循环中创建类的新实例。 所以你可以试试这个:

tp = TestPY()
while 1:
    ...
    tp.update(b)


class TestPY(Image):
    ...
    def __init__(self, **kwargs):
        super(TestPY, self).__init__(**kwargs)

        with self.canvas.before:
            PushMatrix()
            self.rot = Rotate()
            self.rot.angle = self.b
            self.rot.origin = self.center
            self.rot.axis = (0, 0, 1)

    self.b = NumericProperty()

    def update(self, b):
        self.b = b
        print("update")

暂无
暂无

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

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