基于pygame游戏杆在python kivy中创建更新小部件

时间:2020-07-14 22:21:31

标签: python pygame kivy kivy-language

我需要帮助来创建一个更新变量,通过它我可以创建一个Label,该Label在下面的代码中输出由PrintToLabel函数输出的变量。我可能未在kv语言部分中正确创建标签。但是,似乎回答这个问题的人比我拥有更多的kivy / python经验,我想我会在这里问这个。

# 1. Import necessary libraries
# 1.1 Kivy libraries and imports
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.lang import Builder
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty, NumericProperty


# 1.2 other libraries needed that are not a part of kivy
import cv2
import numpy as np
import pygame
from pygame.locals import *
import serial
import time

# 2. Variables (declared after structural code is done)
pygame.init()
pygame.joystick.init()

Lateral_X_Y = 0
Vertical_Z = 0
S1_Data = 0
S2_Data = 0
S3_Data = 0
S4_Data = 0
Lights = 0

In_Min = float(-1)
In_Max = float(1)
Out_Min = float(1000)
Out_Max = float(2000)
Scaled_Out_Min = float(1250)
Scaled_Out_Max = float(1750)

startMarker = '<'
endMarker = '\n'
dividingmarker = ','

# arduino = serial.Serial(port="COM14", baudrate=115200, timeout=0.01)
time.sleep(.01)

# 3. tabpanelkv kv file
Builder.load_string("""

<tabpanelkv>:
    size_hint: 1, 1
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        text: 'Cameras'
        GridLayout:
            rows: 1
            cols: 2
            GridLayout:
                size_hint: 1.7, 1
                rows: 1
                cols: 1
                KivyCamera:
                    cam: 0
            GridLayout:
                rows: 2
                cols: 1
                KivyCamera:
                    cam: 2
                Button:
                    text: 'Cam3'

    TabbedPanelItem:
        text: 'Diagnostics'
        GridLayout:
            rows: 4
            cols: 1
            JoystickValue:
                Label:
                    text: self.PrintToLabel.Xmov

""")

class KivyCamera(Image):
    cam = ObjectProperty()
    fps = NumericProperty(30)

    def __init__(self, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        self._capture = None
        if self.cam is not None:
            self._capture = cv2.VideoCapture(self.cam)
        Clock.schedule_interval(self.update, 1.0 / self.fps)

    def on_cam(self, *args):
        if self._capture is not None:
            self._capture.release()
        self._capture = cv2.VideoCapture(self.cam)

    @property
    def capture(self):
        return self._capture

    def update(self, dt):
        ret, frame = self.capture.read()
        if ret:
            buf1 = cv2.flip(frame, 0)
            buf = buf1.tostring()
            image_texture = Texture.create(
                size=(frame.shape[1], frame.shape[0]), colorfmt="bgr"
            )
            image_texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
            self.texture = image_texture

class JoystickValue(Label):
    Joy1 = pygame.joystick.Joystick(0)
    Joy2 = pygame.joystick.Joystick(1)
    Joy1.init()
    Joy2.init()
    def JoyRead(Joy1, Joy2):
        Joy1X = Joystick1.get_axis(0)
        Joy1Y = Joystick1.get_axis(1)

        Joy2X = Joystick2.get_axis(0)
        Joy2Y = Joystick2.get_axis(1)

        # Buttons on Joy1: 2,3,6,7,8,9,10
        Joy1B2 = Joystick1.get_button(1)
        Joy1B3 = Joystick1.get_button(2)
        Joy1B6 = Joystick1.get_button(5)
        Joy1B7 = Joystick1.get_button(6)
        Joy1B8 = Joystick1.get_button(7)
        Joy1B9 = Joystick1.get_button(8)
        Joy1B10 = Joystick1.get_button(9)

        # Buttons for Joy2: 1, 6
        Joy2B1 = Joystick2.get_button(0)
        Joy2B6 = Joystick2.get_button(5)

        # Hat switch for Joy1
        Joy1HF = Joy1.get_hat(0)

        return Joy1X, Joy1Y, Joy2X, Joy2Y, Joy1B2, Joy1B3, Joy1B6, Joy1B7, Joy1B8, Joy1B9, Joy1B10, Joy2B1, Joy2B6, Joy1HF
    def JoyCalc(Joy1X, Joy1Y, Joy2X, Joy2Y, Joy1B2, Joy1B3, Joy1B6, Joy1B7, Joy1B8, Joy1B9, Joy1B10, Joy2B1, Joy2B6, Joy1HF):
        if Joy1B10 == True:
            Xmov = (((Joy1X - In_Min) * (Scaled_Out_Max - Scaled_Out_Min)) / (In_Max - In_Min)) + 1250
            Ymov = 1250 + (((Joy1Y - In_Min) * (Scaled_Out_Max - Scaled_Out_Min)) / (In_Max - In_Min))
        else:
            Xmov = (((Joy1X - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
            Ymov = 1000 - (((Joy1Y - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000

        User = (((Joy2X - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
        Tser = 1000 - (((Joy2Y - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
        Oser = Joy2B1
        if Joy1HF == (0, 1):
            CTser += 10
        elif Joy1HF == (0, -1):
            CTser -= 10
        else:
            CTser = CTser
        return Xmov, Ymov, Zmov, User, Tser, Oser, CTser
    def PrintToLabel(Xmov, Ymov, User, Tser, Oser, CTser):
        Xmov = StringProperty()
        Ymov = StringProperty()
        User = StringProperty()
        Tser = StringProperty()
        Oser = StringProperty()
        CTser = StringProperty()
        return Xmov, Ymov, User, Tser, Oser, CTser
    def SendtoArduino(Xmov, Ymov, Zmov, User, Tser, Oser, CTser):
        return print("Fuck you")


# 5. tabpanelkv class
class tabpanelkv(TabbedPanel):
    pass

# 6. Main Class
class MainClass(App, GridLayout):
    def build(self):
        return tabpanelkv()

# 7. Main Loop
if __name__ == '__main__':
    MainClass().run()

相关代码^^^ 我创建了必要的变量,并且操纵杆代码正确(我在仅使用pygame之前创建了代码,但是现在我们需要UI更具灵活性)。但是我不知道如何使用kivy创建更新标签。我还将在此消息中添加我不断收到的错误消息。

 ...
      32:            JoystickValue:
      33:                Label:
 >>   34:                    text: self.PrintToLabel.Xmov
      35:
 ...
 BuilderException: Parser: File "<inline>", line 34:
 ...
      32:            JoystickValue:
      33:                Label:
 >>   34:                    text: self.PrintToLabel.Xmov
      35:
 ...
 AttributeError: 'Label' object has no attribute 'PrintToLabel'
   File "/home/mlees/kivy_venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 249, in create_handler
     return eval(value, idmap), bound_list
   File "<string>", line 34, in <module>
   File "kivy/weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
 
   File "/home/mlees/kivy_venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 692, in _apply_rule
     rctx['ids'])
   File "/home/mlees/kivy_venv/lib/python3.6/site-packages/kivy/lang/builder.py", line 254, in create_handler
     cause=tb)
 
Segmentation fault (core dumped)

我也了解到我已经问了很多有关此代码的问题。我是使用Kivy创建UI的新手,所以请理解这一点。

1 个答案:

答案 0 :(得分:1)

...
      32:            JoystickValue:
      33:                Label:
 >>   34:                    text: self.PrintToLabel.Xmov

self这是Label实例。 Label类没有PrintToLabel属性,因此会崩溃。大概您需要引用其他具有此属性的类的PrintToLabel属性。