[英]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.