使用pygtk将Wnck.Window对象添加到Gtk.Container

时间:2018-07-08 20:13:16

标签: python linux python-3.x gtk3 wnck

我正在尝试使用pygtk和Gtk.Notebook界面创建一个选项卡式窗口管理器(类似于WinTabber),但我还不太清楚如何将Wnck.Window对象中的内容移动到Gtk.Notebook页面。我使用“新建标签”按钮创建了GUI,该按钮显示了一个包含所有打开的Wnck.Window对象的Gtk.Listbox的弹出窗口,并且当我尝试将所选窗口添加到新创建的页面中的Gtk.Box对象时,我收到此错误:

TypeError: argument widget: Expected Gtk.Widget, but got gi.repository.Wnck.Window

我尝试在笔记本中添加Wnck.Pager,但效果不佳。我已经浏览了每个不同对象的所有方法,但找不到任何可以做我想做的事情。我在编程方面相对较新,但是我希望有一种解决方法或缺少的东西。有没有一种方法可以将Wnck.Window对象转换为Gtk.Widget?预先感谢!

作为参考,这是我的代码:

import gi, sys
gi.require_version('Gtk', '3.0')
gi.require_version('Wnck', '3.0')
from gi.repository import Gtk, Gio, Wnck

class ListBoxRowWithData(Gtk.ListBoxRow):
    def __init__(self, data):
        super(Gtk.ListBoxRow, self).__init__()
        self.data = data
        self.add(Gtk.Label(data.get_name()))

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Tabby Cat WM")
        self.set_border_width(3)
        self.set_default_size(400, 200)

        hb = Gtk.HeaderBar()
        hb.set_show_close_button(True)
        hb.props.title = "Tabby Cat WM"
        self.set_titlebar(hb)

        def NewTab(NewTabButton):

          #Create pop up window for application selection
          LBWindow = Gtk.Window()
          LBWindow.set_title("New Tab")
          LBWindow.set_border_width(10)
          LBWindow.set_default_size(300, 400)

          box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
          LBWindow.add(box_outer)

          OKbutton = Gtk.Button().new_with_label("OK")
          CancelButton = Gtk.Button().new_with_label("Cancel")

          listbox_2 = Gtk.ListBox()
          Wnck.Screen.get_default().force_update()
          items = Wnck.Screen.get_default().get_windows()

          for item in items:
              listbox_2.add(ListBoxRowWithData(item))

          def OKFunction(OKbutton):
            ActualNewTab = Gtk.Box() 
            window = listbox_2.get_selected_row().data
            ActualNewTab.add(window)
            ActualNewTab.show_all()
            #pagerTest = Wnck.Pager()
            #pagerTest.set_display_mode(Wnck.PagerDisplayMode.NAME)
            #ActualNewTab.add(pagerTest)

            self.notebook.append_page(ActualNewTab)
            self.notebook.set_tab_reorderable(ActualNewTab, True)
            self.notebook.show_all()
            LBWindow.destroy()

          def CancelFunction(CancelButton):
            LBWindow.destroy()

          box_outer.pack_start(listbox_2, True, True, 0)
          box_outer.add(OKbutton)
          box_outer.add(CancelButton)   
          OKbutton.connect('clicked', OKFunction)

          CancelButton.connect('clicked', CancelFunction)
          listbox_2.show_all()
          LBWindow.show_all()

        ButtonPNG = Gio.ThemedIcon(name="go-previous-symbolic.symbolic")
        Lbutton = Gtk.Button()
        image1 = Gtk.Image.new_from_gicon(ButtonPNG, Gtk.IconSize.MENU)
        Lbutton.add(image1)
        hb.pack_start(Lbutton)

        RButtonPNG = Gio.ThemedIcon(name="go-previous-symbolic-rtl.symbolic")
        Rbutton = Gtk.Button()
        image2 = Gtk.Image.new_from_gicon(RButtonPNG, Gtk.IconSize.MENU)
        Rbutton.add(image2)
        hb.pack_start(Rbutton)

        def PrevPage(Lbutton):
            self.notebook.prev_page()

        def NextPage(Rbutton):
            self.notebook.next_page()

        Lbutton.connect('clicked', PrevPage)
        Rbutton.connect('clicked', NextPage)

        NewTabButton = Gtk.Button()
        NewTabButtonPNG = Gio.ThemedIcon(name="list-add-symbolic.symbolic")
        image = Gtk.Image.new_from_gicon(NewTabButtonPNG, Gtk.IconSize.BUTTON)
        NewTabButton.add(image)
        NewTabButton.connect('clicked', NewTab)
        NewTabButton.TooltipText = "New Tab"
        hb.pack_end(NewTabButton)

        def CloseTab(close_button):

            self.notebook.detach_tab(self.notebook.get_nth_page(self.notebook.get_current_page ()))

        TabCloseButtonPNG = Gio.ThemedIcon(name="window-close-symbolic.symbolic")
        image = Gtk.Image.new_from_gicon(TabCloseButtonPNG, Gtk.IconSize.MENU)

        close_button = Gtk.Button()
        close_button.add(image)
        close_button.connect('clicked', CloseTab)
        hb.pack_end(close_button)

        self.notebook = Gtk.Notebook()
        self.add(self.notebook)

        Wnck.Screen.get_default().force_update ()

        screen = Wnck.Screen.get_default()
        screen.force_update()  # recommended per Wnck documentation
        window = screen.get_windows()


win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

0 个答案:

没有答案