简体   繁体   中英

Python Kivy Image rotate by event

Please help rotate the image by event in the def read_sok(): function.

Accessing the TestPY().update() does not rotate the image. the print("update") string is executed

the angle to rotate the image is sent from the server

the app is being developed to simulate a switch device

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

enter image description here

I suppose it happens because you have the angle of rotation defined in 'init'. While you are running 'TestPY().update()' at first it creates new object of the class, rotates it to 0°, and then launches update method, when rotation is over.

You can try to define your angle in the init method, maybe that'll work:

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)

Or you can define your angle as NumericProperty() and don't create new instances of a class in limitless loop. So you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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