[Pythonmac-SIG] Menus from pyobjc
Dethe Elza
delza at blastradius.com
Tue Feb 17 18:14:05 EST 2004
On 16-Feb-04, at 3:25 PM, Bob Ippolito wrote:
> If you post a minimal sample of what you're trying to do, I'll [attempt
> to] fix it and make a PyObjC example out of it. IIRC, you have to do
> some undocumented voodoo to get the menus to do what you're asking... I
> did this in PyObjC for recentish versions of pygame (in macosx.py),
> though it's not particularly well tested because sane people use IB
> nibs ;)
Never let me be accused of being sane. My last job title was "Chief
Mad Scientist."
Here's a minimal example, for some value of "minimal".
--Dethe
import objc
from Foundation import *
from AppKit import *
import random
class TextView(NSTextView):
def change(self):
data = ('A stitch in time saves nine. Nine what?',
'Early to bed and early to rise, makes a man healthy, wealthy, and
sleepy',
'The early bird gets the worm. He can have it.')
currText = self.textStorage()
newText = random.choice(data)
length = currText.length()
currText.replaceCharactersInRange_withString_((0,length), newText)
class AppDelegate(NSObject):
def setApp_(self, app):
self.app = app
def windowWillClose_(self, notification):
self.app.terminate_(self)
def initWindow(delegate):
windowRect = NSMakeRect(100.0, 0.0, 282.0, 153.0)
window =
NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
windowRect,
NSTitledWindowMask
| NSClosableWindowMask
# | NSResizableWindowMask
| NSMiniaturizableWindowMask
| NSTexturedBackgroundWindowMask,
NSBackingStoreBuffered,
False)
window.setTitle_('My Test App')
window.setDelegate_(delegate)
view = initView()
window.contentView().addSubview_(view)
window.contentView().addSubview_(initButton(view))
window.display()
window.orderFrontRegardless()
return window
def initView():
graphicsRect = NSMakeRect(20.0, 40.0, 240.0, 110.0)
view = TextView.alloc().initWithFrame_(graphicsRect)
view.change()
view.setFont_(NSFont.fontWithName_size_('Apple LiGothic Medium',24.0))
view.setAlignment_(NSCenterTextAlignment)
view.setEditable_(False)
return view
def initButton(view):
button = NSButton.alloc().initWithFrame_(((100,5),(80,30)))
button.setBezelStyle_(NSRoundedBezelStyle)
button.setTitle_('Think')
button.setTarget_(view)
button.setAction_('change')
return button
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
delegate.setApp_(app)
setupMenus(app, delegate)
window = initWindow(delegate)
app.run()
def setupMenus(app, delegate):
mainmenu = NSMenu.alloc().init()
app.setMainMenu_(mainmenu)
appMenuItem =
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('My Sample App',
'sample', '')
fileMenuItem =
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('File', 'file',
'')
editMenuItem =
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Edit', 'edit',
'')
mainmenu.addItem_(appMenuItem)
mainmenu.addItem_(fileMenuItem)
mainmenu.addItem_(editMenuItem)
appMenu = NSMenu.alloc().init()
fileMenu = NSMenu.alloc().init()
editMenu = NSMenu.alloc().init()
appMenuItem.setSubmenu_(appMenu)
fileMenuItem.setSubmenu_(fileMenu)
editMenuItem.setSubmenu_(editMenu)
appMenuItem.setEnabled_(True)
fileMenuItem.setEnabled_(True)
editMenuItem.setEnabled_(True)
aboutItem =
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('About My Sample
App...', 'about', '')
aboutItem.setTarget_(delegate)
appMenu.addItem_(aboutItem)
appMenu.addItemWithTitle_action_keyEquivalent_('Preferences...',
'prefs', '')
appMenu.addItemWithTitle_action_keyEquivalent_('Hide My Sample App',
'hide', 'h')
appMenu.addItemWithTitle_action_keyEquivalent_('Hide Others',
'hideOthers', '')
quitItem =
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'quit',
'q')
quitItem.setTarget_(delegate)
appMenu.addItem_(quitItem)
fileMenu.addItemWithTitle_action_keyEquivalent_('Open', 'open', '')
fileMenu.addItemWithTitle_action_keyEquivalent_('Close', 'close', 'w')
editMenu.addItemWithTitle_action_keyEquivalent_('Cut', 'cut', 'x')
editMenu.addItemWithTitle_action_keyEquivalent_('Copy', 'copy', 'c')
editMenu.addItemWithTitle_action_keyEquivalent_('Paste', 'paste', 'v')
if __name__ == '__main__':
main()
"the city carries such a cargo of pathos and longing
that daily life there vaccinates us against revelation"
-- Pain Not Bread, The Rise and Fall of Human Breath
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2371 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040217/04848ef1/smime-0001.bin
More information about the Pythonmac-SIG
mailing list