【问题标题】:Python Kivy Image rotate by eventPython Kivy 图像按事件旋转
【发布时间】:2020-05-14 15:17:35
【问题描述】:

请帮助在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()

enter image description here

【问题讨论】:

    标签: python image sockets rotation kivy


    【解决方案1】:

    我想这是因为您在“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")
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2019-08-31
    相关资源
    最近更新 更多