2013-10-20 6 views
6

मैं अपने जीटीके ऐप में मेनूबार को कन्वर्ट करने की कोशिश कर रहा हूं ताकि यह GActions (जीओ से) GtkActions के विरोध में पाइथन 3 में गोबजेक्ट इंस्ट्रोसक्शन का उपयोग करके उपयोग करेगा।पीजीजीआई जीटीके में जीआईओ क्रियाओं का उपयोग कर एक पूरा मेनू कैसे बनाएं?

मैं इसे अपने आप समझने की कोशिश कर रहा हूं लेकिन अब तक यह बहुत जटिल लगता है और मुझे इसके साथ बहुत भाग्य नहीं मिला।

कोई कैसे एक सरल मेनू GAction

  • एक सबमेनू
  • एक शेयर आईडी आइकन/हॉटकी
  • एक के साथ एक मेनू आइटम के साथ एक मेनू आइटम के साथ आधारित बनाने के लिए का एक उदाहरण पोस्ट कृपया सकता है गैर शेयर आइकन/हॉटकी
  • एक मेनू आइटम की जाँच की
  • और रेडियो मेनू आइटम समूह
  • एक विकलांग (धूसर हो ओ यूटी) मेनू आइटम

यह वास्तव में मुझे बहुत मदद करेगा।

संपादित: यह अब मेनूबार मेरी खिड़की में मेरे पास है:

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

आपको वास्तव में नए पोस्ट करने के बजाय अपने पिछले उत्तर को संपादित करना चाहिए। इसके अलावा इनमें चेक मेनू आइटम या रेडियो मेनू आइटम शामिल नहीं हैं। : एस – OdraEncoded

+4

ओह, चलो। क्या मुझे आपके लिए पूरा ऐप लिखना चाहिए? – LiuLang

संबंधित मुद्दे