david-pesticcio

Untitled

Mar 7th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | Source Code | 0 0
  1. # This is a sample Python script.
  2.  
  3. # Press Shift+F10 to execute it or replace it with your code.
  4. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
  5.  
  6.  
  7. def print_hi(name):
  8.     # Use a breakpoint in the code line below to debug your script.
  9.     print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
  10.  
  11.  
  12. # Press the green button in the gutter to run the script.
  13. if __name__ == '__main__':
  14.     print_hi('PyCharm')
  15.  
  16. # See PyCharm help at https://www.jetbrains.com/help/pycharm/
  17. __version__ = '1.0'  # declare the app version. Will be used by buildozer
  18.  
  19. from kivy.app import App  # for the main app
  20. from kivy.uix.floatlayout import FloatLayout  # the UI layout
  21. from kivy.uix.label import Label  # a label to show information
  22. from plyer import accelerometer  # object to read the accelerometer
  23. from kivy.clock import Clock  # clock to schedule a method
  24.  
  25.  
  26. class UI(FloatLayout):  # the app ui
  27.     def __init__(self, **kwargs):
  28.         super(UI, self).__init__(**kwargs)
  29.         self.lblAcce = Label(text="Accelerometer: ")  # create a label at the center
  30.         self.add_widget(self.lblAcce)  # add the label at the screen
  31.         try:
  32.             accelerometer.enable()  # enable the accelerometer
  33.             # if you want do disable it, just run: accelerometer.disable()
  34.             Clock.schedule_interval(self.update, 1.0 / 24)  # 24 calls per second
  35.         except:
  36.             self.lblAcce.text = "Failed to start accelerometer"  # error
  37.  
  38.     def update(self, dt):
  39.         txt = ""
  40.         try:
  41.             txt = "Accelerometer:\nX = %.2f\nY = %.2f\nZ = %2.f " % (
  42.                 accelerometer.acceleration[0],  # read the X value
  43.                 accelerometer.acceleration[1],  # Y
  44.                 accelerometer.acceleration[2])  # Z
  45.         except:
  46.             txt = "Cannot read accelerometer!"  # error
  47.         self.lblAcce.text = txt  # add the correct text
  48.  
  49.  
  50. class Accelerometer(App):  # our app
  51.     def build(self):
  52.         ui = UI()  # create the UI
  53.         return ui  # show it
  54.  
  55.  
  56. if __name__ == '__main__':
  57.     Accelerometer().run()  # start our app
Add Comment
Please, Sign In to add comment