⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example-2.py

📁 Developing with Gnome一书examples例子代码
💻 PY
字号:
#!/usr/bin/env python# This program monitors the# /apps/nautilus/preferences/always_use_browser,# /apps/nautilus/preferences/show_image_thumbnails, and# /apps/nautilus/preferences/thumbnail_limit gconf keys, and allows# the user to change them as well.import gtkimport gtk.gladeimport gconfimport reclass ExampleWindow:  def __init__(self, glade_file):    # Parse the glade file    self.all_da_widgets = gtk.glade.XML(glade_file)    # Get the client and tell it we want to monitor /apps/nautilus/preferences    self.conf_client = gconf.client_get_default()    self.conf_client.add_dir("/apps/nautilus/preferences", \                             gconf.CLIENT_PRELOAD_NONE);    # First, setup the checkbutton for the always_use_browser key     # Get the check_button    self.browser_checkbutton = self.all_da_widgets.get_widget("BrowserMode")    # Connect the check_button to a callback that can toggle it when the option    # is changed by some outside program.    self.conf_client.notify_add(                       \      "/apps/nautilus/preferences/always_use_browser", \      self.browser_key_changed                         \      )    # Connect the check_button's clicked callback up so that it can change the    # gconf setting.    self.browser_checkbutton.connect("clicked", self.browser_key_toggled)    # Determine whether button should start activated or not    self.old_browser_setting = \      self.conf_client.get_bool("/apps/nautilus/preferences/always_use_browser")    self.browser_checkbutton.set_active(self.old_browser_setting)    # Second, setup the text entry for the show_image_thumbnails key     # Get the Entry    self.show_thumbnail_entry = \      self.all_da_widgets.get_widget("ShowThumbnailsEntry")    # Connect the show thumbnail entry to a callback that can toggle it    # when the option is changed by some outside program.    self.conf_client.notify_add(                          \      "/apps/nautilus/preferences/show_image_thumbnails", \      self.show_thumbnails_key_changed                    \      )    # Connect the show thumbnail entry's activated callback up so that    # it can change the gconf setting.    self.show_thumbnail_entry.connect("activate", self.show_thumbnails_modified)    # Determine the starting value of this key    self.old_show_thumbnails_setting = \      self.conf_client.get_string(     \        "/apps/nautilus/preferences/show_image_thumbnails")    self.show_thumbnail_entry.set_text(self.old_show_thumbnails_setting)    # Third, setup the text entry for the thumbnail_limit key    # Get the Entry    self.thumbnail_limit_entry = \      self.all_da_widgets.get_widget("ThumbnailLimitEntry")    # Connect the thumbnail limit entry to a callback that can toggle    # it when the option is changed by some outside program.    self.conf_client.notify_add(      "/apps/nautilus/preferences/thumbnail_limit",      self.thumbnail_limit_key_changed      )    # Connect the thumbnail limit entry's activated callback up so that    # it can change the gconf setting.    self.thumbnail_limit_entry.connect("activate", self.thumbnail_limit_modified)    # Determine the starting value of this key    self.old_thumbnail_limit = \      self.conf_client.get_int("/apps/nautilus/preferences/thumbnail_limit");    self.thumbnail_limit_entry.set_text(str(self.old_thumbnail_limit));    # Get the close button and connect it to hide    close_button = self.all_da_widgets.get_widget("CloseButton")    close_button.connect("clicked", gtk.mainquit)    # Get the close button and connect it to hide    main_window = self.all_da_widgets.get_widget("MainWindow")    main_window.connect("delete_event", gtk.mainquit)  def browser_key_changed(self, client, connection_id, entry, args):    # Make sure the preference has a valid value    if (entry.get_value().type == gconf.VALUE_BOOL):      # Get the new value      new_browser_setting = entry.get_value().get_bool()      # Try to handle asynchronous-ness of GConf and prevent calling      # checkbutton_toggled again if this function was called as a      # result of the self.conf_client.set() function call.      if (new_browser_setting != self.old_browser_setting):        self.old_browser_setting = new_browser_setting        self.browser_checkbutton.set_active(new_browser_setting)  def browser_key_toggled(self, checkbutton_widget):    # Determine whether the checkbutton is already set or not    is_set = self.browser_checkbutton.get_active()     # Try to handle asynchronous-ness of GConf and prevent calling    # key_changed again if this function was called as a result of the    # self.checkbutton.set_active() function call.    if (is_set != self.old_browser_setting):      self.old_browser_setting = is_set;      self.conf_client.set_bool("/apps/nautilus/preferences/always_use_browser",\                                is_set)  def show_thumbnails_key_changed(self, client, connection_id, entry, args):    # Make sure the preference has a valid value    if (entry.get_value().type == gconf.VALUE_STRING):      # Get the new value      new_setting = entry.get_value().get_string()      # Check if it seems valid, give a warning if it doesn't      if ((new_setting != "always")     and \          (new_setting != "local_only") and \          (new_setting != "never")):        # I couldn't find an equivalent for g_warning        print "Unrecognized value specified by external program...\n"      # Use the new value if it's different than the old      if (new_setting != self.old_show_thumbnails_setting):        self.old_show_thumbnails_setting = new_setting        self.show_thumbnail_entry.set_text(new_setting)  def show_thumbnails_modified(self, show_thumbnail_entry):    # Get the current value    cur_value = self.show_thumbnail_entry.get_text()     if ((cur_value == "always")     or        (cur_value == "local_only") or        (cur_value == "never")):      if (cur_value != self.old_show_thumbnails_setting):        self.old_show_thumbnails_setting = cur_value        self.conf_client.set_string(          "/apps/nautilus/preferences/show_image_thumbnails",          cur_value);    else:      # I couldn't find an equivalent for g_warning      print "This program needs an error window to warn you" + \            " that you typed an invalid value."      self.show_thumbnail_entry.set_text(self.old_show_thumbnails_setting)  def thumbnail_limit_key_changed(self, client, connection_id, entry, args):    # Make sure the preference has a valid value    if (entry.get_value().type == gconf.VALUE_INT):      # Get the new value      new_value = entry.get_value().get_int()      # Check if it seems valid, give a warning if it doesn't      if (new_value < 0):        # I couldn't find an equivalent for g_warning        print "Bad value seems to have been set by external program."      # Use the new value if it's different than the old      if (new_value != self.old_thumbnail_limit):        self.old_thumbnail_limit = new_value        self.thumbnail_limit_entry.set_text(str(new_value))  def thumbnail_limit_modified(self, thumbnail_limit_entry):    # Get the current value    new_value = self.thumbnail_limit_entry.get_text()    nonnegative_number_regex = re.compile(r'^\d+$')    if nonnegative_number_regex.match(new_value):      if (new_value != self.old_thumbnail_limit):        self.old_thumbnail_limit = new_value        self.conf_client.set_int("/apps/nautilus/preferences/thumbnail_limit",\                                 int(new_value))          else:      # I couldn't find an equivalent for g_warning      print "This program needs an error window to warn you" + \            " that you typed an invalid value."      self.thumbnail_limit_entry.set_text(str(self.old_thumbnail_limit))# Create the ExampleWindowwindow = ExampleWindow("example-2.glade")# start the event loopgtk.main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -