如何在主窗口中加载视图使用pyobjc?

0 投票
1 回答
515 浏览
提问于 2025-04-16 07:25

我有一个窗口,这个窗口里有一个工具栏,上面有7个按钮。当你按下其中一个按钮时,窗口里会有一个空的子视图,选中的内容就会显示在这个空白的地方,并且会有淡入淡出的效果。我的代码示例可以在这里找到:https://github.com/jyr/opentumblr-cocoa/blob/master/DashboardController.py

1 个回答

0

我的解决方案在这个链接里:https://gist.github.com/718610

"""
En una ventana muestra 7 vistas seleccionadas desde una barra de herramientas
"""

from Foundation import *
from AppKit import *
import objc

class DashboardController (NSViewController):

    def show(self):
        self.window = NSWindow.alloc().init()
        self.DashboardViewController = NSViewController.alloc().initWithNibName_bundle_("Dashboard", None)
        self.dashboardView = self.DashboardViewController.view()
    show = classmethod(show)

    def resetSubviews(self):
        for i in range(1, len(self.dashboardView.subviews())):
            self.dashboardView.subviews().removeObjectAtIndex_(i)

    @objc.IBAction
    def audio_(self, sender):
        self.resetSubviews()
        self.AudioViewController = NSViewController.alloc().initWithNibName_bundle_("Audio", None)
        self.audioView = self.AudioViewController.view()
        self.dashboardView.addSubview_(self.audioView)

    @objc.IBAction
    def chat_(self, sender):
        self.resetSubviews()
        self.ChatViewController = NSViewController.alloc().initWithNibName_bundle_("Chat", None)
        self.chatView = self.ChatViewController.view()
        self.dashboardView.addSubview_(self.chatView)

    @objc.IBAction
    def link_(self, sender):
        self.resetSubviews()
        self.LinkViewController = NSViewController.alloc().initWithNibName_bundle_("Link", None)
        self.linkView = self.LinkViewController.view()
        self.dashboardView.addSubview_(self.linkView)

    @objc.IBAction
    def logout_(self, sender):
        NSApp().terminate_(self)

    @objc.IBAction
    def photo_(self, sender):
        self.resetSubviews()
        self.PhotoViewController = NSViewController.alloc().initWithNibName_bundle_("Photo", None)
        self.photoView = self.PhotoViewController.view()
        self.dashboardView.addSubview_(self.photoView)

    @objc.IBAction
    def quote_(self, sender):
        self.resetSubviews()
        self.QuoteViewController = NSViewController.alloc().initWithNibName_bundle_("Quote", None)
        self.quoteView = self.QuoteViewController.view()
        self.dashboardView.addSubview_(self.quoteView)

    @objc.IBAction
    def text_(self, sender):
        self.resetSubviews()
        self.TextViewController = NSViewController.alloc().initWithNibName_bundle_("Text", None)
        self.textView = self.TextViewController.view()
        self.dashboardView.addSubview_(self.textView)

    @objc.IBAction
    def video_(self, sender):
        self.resetSubviews()
        self.VideoViewController = NSViewController.alloc().initWithNibName_bundle_("Video", None)
        self.videoView = self.VideoViewController.view()
        self.dashboardView.addSubview_(self.videoView)

撰写回答