2013-10-20 23 views
6

我試圖轉換我的Gtk應用程序的菜單欄,所以它會使用GObject Instrospection在Python3中使用GActions(來自Gio),而不是GtkActions如何在PyGI GTK中使用GIO Actions創建一個完整的菜單?

我一直試圖弄清楚我自己,但到目前爲止它似乎非常複雜,我沒有太多的運氣。

如果有人能請張貼的如何基於與

  • 子菜單
  • 與股票ID的圖標/熱鍵
  • 與菜單項的菜單項簡單的菜單GAction爲例非庫存圖標/熱鍵
  • 甲檢查菜單項
  • 和無線電菜單項組
  • 甲禁用(變灰Ò ut)菜單項

這真的會幫助我很多。

編輯:這是菜單欄我在我的窗口現在:

enter image description here

如果有人可以複製使用GioActions顯示的菜單項會,所以我可以找出他們是如何工作的它會很棒。

順便說一下,我使用窗口回調不是應用程序回調的所有操作,所以這是一個窗口菜單欄而不是應用程序菜單欄。

回答

7

現在添加菜單欄。

#!/usr/bin/env python3 

# Copyright (C) 2013 LiuLang <[email protected]> 

# Use of this source code is governed by GPLv3 license that can be found 
# in http://www.gnu.org/licenses/gpl-3.0.html 

from gi.repository import Gio 
from gi.repository import Gtk 
import sys 

menus_str =''' 
<?xml version="1.0"?> 
<interface> 
    <menu id="appmenu"> 
    <section> 
     <item> 
     <attribute name="label" translatable="yes">Preferences</attribute> 
     <attribute name="action">app.preferences</attribute> 
     </item> 
    </section> 
    <section> 
     <item> 
     <attribute name="label" translatable="yes">About</attribute> 
     <attribute name="action">app.about</attribute> 
     </item> 
     <item> 
     <attribute name="label" translatable="yes">Quit</attribute> 
     <attribute name="action">app.quit</attribute> 
     <attribute name="accel">&lt;Primary&gt;q</attribute> 
     </item> 
    </section> 
    </menu> 
    <menu id="menubar"> 
    <submenu> 
     <attribute name="label">_Help</attribute> 
     <section> 
     <item> 
       <attribute name="label">_About</attribute> 
       <attribute name="action">app.about</attribute> 
      </item> 
      </section> 
     </submenu> 
     </menu> 
    </interface> 
    ''' 

    class App: 
     def __init__(self): 
      self.app = Gtk.Application.new('org.liulang.test', 0) 
      self.app.connect('startup', self.on_app_startup) 
      self.app.connect('activate', self.on_app_activate) 
      self.app.connect('shutdown', self.on_app_shutdown) 

     def run(self, argv): 
      self.app.run(argv) 

     def on_app_startup(self, app): 
      self.window = Gtk.ApplicationWindow.new(app) 
      self.window.set_default_size(640, 480) 
      self.window.set_title('Gio Actions Demo') 
      self.window.set_border_width(5) 
      # no need to connect delete-event/destroy signal 

      app.add_window(self.window) 

      label = Gtk.Label('Hello, Gtk3') 
      self.window.add(label) 
      label.props.halign = Gtk.Align.CENTER 
      label.props.valign = Gtk.Align.CENTER 

      builder = Gtk.Builder() 
      # It is better to load ui from a seperate file 
      builder.add_from_string(menus_str) 
      builder.connect_signals(self) 
      appmenu = builder.get_object('appmenu') 
      app.set_app_menu(appmenu) 
      menubar = builder.get_object('menubar') 
      app.set_menubar(menubar) 

      self.add_simple_action('preferences', 
        self.on_action_preferences_activated) 
      self.add_simple_action('about', self.on_action_about_activated) 
      self.add_simple_action('quit', self.on_action_quit_activated) 

     def on_app_activate(self, app): 
      self.window.show_all() 

     def on_app_shutdown(self, app): 
      # do some cleaning job here, like dumping configuration. 
      pass 

     def on_action_preferences_activated(self, action, user_data): 
      print('will popup preferences dialog') 

     def on_action_about_activated(self, action, user_data): 
      print('will show about dialog') 

     def on_action_quit_activated(self, action, user_data): 
      # This will close the default gtk mainloop 
      self.app.quit() 

     def add_simple_action(self, name, callback): 
      action = Gio.SimpleAction.new(name, None) 
      action.connect('activate', callback) 
      self.app.add_action(action) 


    if __name__ == '__main__': 
     app = App() 
     app.run(sys.argv) 
+0

你應該真的只是編輯你以前的答案,而不是發佈新的答案。這些也不包括檢查菜單項目或收音機菜單項目。 :S – OdraEncoded

+4

哦,來吧。我應該爲你寫出整個應用程序嗎? – LiuLang

相關問題