Replies: 2 comments 2 replies
-
I don't know. I worked really hard just to get py5 to appear in the dock, which itself is necessary for the window to be given focus. Otherwise, the Sketch window will always be buried behind other windows and does not want to accept keyboard inputs. Theoretically I could also create menu options but it is unclear what code would get executed when selected. There won't be an Exit Sketch option but perhaps there could be code that exits Python altogether? |
Beta Was this translation helpful? Give feedback.
-
The following is a pyobjc demo for Thonny which shows how to create a very simple menu and window. Cmd-Q may be used to quit the sketch and reset the Thonny shell: from Cocoa import (
NSObject,
NSApplication,
NSApp,
NSWindow,
NSButton,
NSMakeRect,
NSBeep,
NSMenu,
NSMenuItem,
NSBezelStyleCircular,
NSBezelStylePush,
NSWindowStyleMaskTitled,
NSWindowStyleMaskClosable,
NSWindowStyleMaskMiniaturizable,
NSWindowStyleMaskResizable,
NSBackingStoreBuffered,
)
_wndW = 400
_wndH = 200
class AppDelegate(NSObject):
def myBtnAction_(self, sender):
print("Hello World!")
NSBeep()
def buildMenu(self):
menubar = NSMenu.alloc().init()
app_menu_item = NSMenuItem.alloc().init()
menubar.addItem_(app_menu_item)
NSApp.setMainMenu_(menubar)
app_menu = NSMenu.alloc().init()
quit_menu_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Quit", "terminate:", "q")
app_menu.addItem_(quit_menu_item)
app_menu_item.setSubmenu_(app_menu)
def buildWnd(self):
frame = NSMakeRect(0, 0, _wndW, _wndH)
wndStyle = (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable)
self.wnd = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(frame, wndStyle, NSBackingStoreBuffered, False)
self.wnd.setTitle_("HelloWorld")
self.wnd.center()
self.wnd.orderFrontRegardless()
myBtn = NSButton.alloc().initWithFrame_(NSMakeRect(10.0, _wndH - 50, 90, 30))
self.wnd.contentView().addSubview_(myBtn)
myBtn.setBezelStyle_(NSBezelStylePush)
myBtn.setTitle_("Hello!")
myBtn.setAction_("myBtnAction:")
quitBtn = NSButton.alloc().initWithFrame_(NSMakeRect(_wndW - 60, 10, 50, 30))
self.wnd.contentView().addSubview_(quitBtn)
quitBtn.setBezelStyle_(NSBezelStyleCircular)
quitBtn.setTitle_("Q")
quitBtn.setAction_("terminate:")
def applicationDidFinishLaunching_(self, aNotification):
self.buildMenu()
self.buildWnd()
def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
return True
def main():
app = NSApplication.sharedApplication()
appDelegate = AppDelegate.alloc().init()
app.setDelegate_(appDelegate)
app.run()
main() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Does py5 have any control over the 'Python' menu on the menubar when running a Thonny sketch?


As soon as the 'Run' button is hit Thonny's menu is replaced by the 'Python' menu, but there are no menuItems as shown below in a normal app menu such as in the Thonny app:
There should at a minimum be a 'Quit cmd-Q' menuItem and I would like to see this added if py5 creates this menu initially. If Python or Thonny is creating the menu then it would appear to be beyond our control. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions