|
| 1 | +# There two ways to import Haiku-PyAPI |
| 2 | +# First, you can import only the things you need |
| 3 | +from Be import BApplication, BWindow, BRect, BMessage, BView, BButton, window_type, B_NOT_RESIZABLE, B_QUIT_ON_WINDOW_CLOSE, int32, B_WILL_DRAW, B_FOLLOW_NONE |
| 4 | +# Or import everything! |
| 5 | +#from Be import * |
| 6 | + |
| 7 | +# Usage of the API is extremely similar to how it is used in C++ |
| 8 | + |
| 9 | +class App(BApplication): |
| 10 | + def __init__(self): |
| 11 | + BApplication.__init__( |
| 12 | + self, # Any call to a constructor requires self as the first parameter |
| 13 | + "application/x-vnd.pyapi_test") # Application signature |
| 14 | + |
| 15 | + def ReadyToRun(self): |
| 16 | + # Display the main window |
| 17 | + window = MainWindow() |
| 18 | + window.Show() |
| 19 | + |
| 20 | +class MainWindow(BWindow): |
| 21 | + def __init__(self): |
| 22 | + BWindow.__init__( |
| 23 | + self, # Any call to a constructor requires self as the first parameter |
| 24 | + BRect(100,100,200,150), # Window size |
| 25 | + "Hello Haiku!", # Window title |
| 26 | + window_type.B_TITLED_WINDOW, # Window type |
| 27 | + B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE) # Flags |
| 28 | + |
| 29 | + # The "Say Hello!" button's message code |
| 30 | + self.MSG_SAY_HI = int32(b"syhi") |
| 31 | + |
| 32 | + # The "Say Hello!" button |
| 33 | + self.button = BButton( |
| 34 | + self.Bounds(), # Button size |
| 35 | + "hi", # Internal name of button |
| 36 | + "Say Hello!", # Button text |
| 37 | + BMessage(self.MSG_SAY_HI)) # Message to send when button is pressed |
| 38 | + self.AddChild(self.button, None) |
| 39 | + |
| 40 | + def MessageReceived(self, msg): |
| 41 | + if msg.what == self.MSG_SAY_HI: |
| 42 | + # The "Say Hello!" button has been pressed! |
| 43 | + print("Hello World!") |
| 44 | + else: |
| 45 | + # We don't know what to do with this message. Pass it on to BWindow |
| 46 | + BWindow.MessageReceived(self, msg) |
| 47 | + |
| 48 | +# Let's launch the application! |
| 49 | +app = App() |
| 50 | +app.Run() |
0 commit comments