📄 example-4.py
字号:
#!/usr/bin/env python# This program shows a preferences window, which contains# RadioButtons, a color selection widget, and a font selection widget.# It does manual signal connection the pygtk way (as opposed to either# the autoconnect libglade method), pops up a (modal) window to# confirm whether the user really wants to exit, and demonstrates how# to use pango markup.import gtkimport gtk.gladeimport reclass PreferencesWindow: def __init__(self, glade_file): # Parse the glade file self.all_da_widgets = gtk.glade.XML(glade_file) # # FIRST, get the widgets I want to track # # Get the widgets I'm interested in self.preferences = self.all_da_widgets.get_widget("MainWindow") self.color_sel = self.all_da_widgets.get_widget("ColorSelection") self.font_sel = self.all_da_widgets.get_widget("FontSelection") self.confirm_close = self.all_da_widgets.get_widget("ConfirmClose") self.warning_label = self.all_da_widgets.get_widget("InfoLabel") # Initialize the color and location to "not-yet-known" values self.location = "undefined" self.color = gtk.gdk.Color(0,0,0) # # SECOND, we handle setting up defaults and connect callbacks for # the confirmation window. # # Have the confirm-closing window's delete event (window close) do nothing self.confirm_close.connect("delete_event", self.do_nothing); # Have the cancel button hide the confirmation window cancel_button = self.all_da_widgets.get_widget("CancelButton") cancel_button.connect("clicked", self.cancel_button_pressed) # Have the "Exit anyway" button close everything exit_button = self.all_da_widgets.get_widget("ExitButton") exit_button.connect("clicked", gtk.mainquit) # Make the confirm-closing window start hidden self.confirm_close.hide() # # THIRD, we handle setting up defaults and connect callbacks for # the preferences window. # # Keep track of when the location changes here = self.all_da_widgets.get_widget("Here") here.connect("toggled", self.new_location_selected, "Here") there = self.all_da_widgets.get_widget("There") there.connect("toggled", self.new_location_selected, "There") # Make 'There' be the default; note this also emits the toggled signal there.set_active(gtk.TRUE) # Keep track of when the color changes self.color_sel.connect("color_changed", self.new_color_selected) # Set the default to whatever the ColorSelection widget defaults to self.color = self.color_sel.get_current_color() # Turn off the opacity slider self.color_sel.set_has_opacity_control(gtk.FALSE) # Have the quit button show the confirmation window quit_button = self.all_da_widgets.get_widget("QuitButton") quit_button.connect("clicked", self.time_to_quit) # Delete event taken care of by overriding the on_delete_event function... self.preferences.connect("delete_event", self.call_time_to_quit) def do_nothing(self,confirmation_dialog,event): # Return true to signify that this event has been fully handled return gtk.TRUE def cancel_button_pressed(self, cancel_button): self.confirm_close.hide() def new_location_selected(self, radio_button, location): self.location = location def new_color_selected(self, color_selection_widget): self.color = self.color_sel.get_current_color() def convert_color_to_string(self, color): """ This simple helper function which converts an RGB triplet (which is part of a Gdk.Color) into a "#RRGGBB" string. """ color_string = "#%.2X%.2X%.2X" % \ (color.red/256,color.green/256,color.blue/256) return color_string def time_to_quit(self, ignored_widget): color_string = self.convert_color_to_string(self.color); # Get the warning message message = self.warning_label.get_label() # Set up the replacement text. replacement_text = '<span ' \ + 'font_desc="' + self.font_sel.get_font_name() + '" ' \ + 'foreground="' + color_string + '">' \ + self.location \ + '</span>' # Make the new message old_regex = re.compile(r'to <span.*<\/span>') message = old_regex.sub("to " + replacement_text, message) # Update the label self.warning_label.set_label(message) # Make sure the confirmation window is shown self.confirm_close.show() def call_time_to_quit(self, window, event): self.time_to_quit(window) # No other handlers need be called... return gtk.TRUE# Create the PreferencesWindowwindow = PreferencesWindow("example-4.glade")# start the event loopgtk.main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -