我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用gtk.glade()。
def __init__(self, o=None): self.xml = x = gtk.glade.XML(sibpath(__file__, "inspectro.glade")) self.tree_view = x.get_widget("treeview") colnames = ["Name", "Value"] for i in range(len(colnames)): self.tree_view.append_column( gtk.TreeViewColumn( colnames[i], gtk.CellRendererText(), text=i)) d = {} for m in reflect.prefixedMethods(self, "on_"): d[m.im_func.__name__] = m self.xml.signal_autoconnect(d) if o is not None: self.inspect(o) self.ns = {'inspect': self.inspect} iwidget = x.get_widget('input') self.input = ConsoleInput(iwidget) self.input.toplevel = self iwidget.connect("key_press_event", self.input._on_key_press_event) self.output = ConsoleOutput(x.get_widget('output'))
def initialize(self): #{{{ self.fields_to_export = [] for field in ExportPlugin.fields: if field == 'movies_image': self.fields_to_export.append('movies.poster_md5') else: pos = field.find('_') self.fields_to_export.append("%s.%s" % (field[:pos], field[pos+1:])) self.widgets = {} self.style_list = {} self.templates = self.make_template_list() # glade gf = os.path.join(self.locations['glade'], 'exporthtml.glade') self.load_configuration() self.define_widgets(gtk.glade.XML(gf)) self.fill_widgets() return True #}}}
def normalize_names(self): """ It is internally used to normalize the name of the widgets. It means a widget named foo:vbox-dialog in glade is refered self.vbox_dialog in the code. It also sets a data "prefixes" with the list of prefixes a widget has for each widget. """ for widget in self.get_widgets(): widget_name = gtk.Widget.get_name(widget) prefixes_name_l = widget_name.split(":") prefixes = prefixes_name_l[ : -1] widget_api_name = prefixes_name_l[-1] widget_api_name = "_".join( re.findall(tokenize.Name, widget_api_name) ) gtk.Widget.set_name(widget, widget_api_name) if hasattr(self, widget_api_name): raise AttributeError("instance %s already has an attribute %s" % (self,widget_api_name)) else: setattr(self, widget_api_name, widget) if prefixes: gtk.Widget.set_data(widget, "prefixes", prefixes)
def __init__(self,controller): # TODO: no me mola mucho esto self.gladefile = "glade1.glade" self.wTree = gtk.glade.XML(self.gladefile) self.img = None self.done = False self.thrd = None # Widgets self.win = self.wTree.get_widget("mainWindow") self.dlg_settings = self.wTree.get_widget("dialog_settings") self.img_gtk = self.wTree.get_widget("imgVideo") self.area = self.wTree.get_widget("drwCubo") self.scrolledWindow = self.wTree.get_widget("scrolledSolver") self.txtSolver = self.wTree.get_widget("txtSolver") self.txtSolverBuffer = self.txtSolver.get_buffer() self.win.connect("delete_event",self.leave_app) self.img_flag=0 #TODO: Comprobar estas senales senales = { "on_camara_on_off": self.on_camara_on_off, "on_salir" : self.leave_app, "on_cmdSettings" : self.on_cmdSettings, } self.wTree.signal_autoconnect(senales) self.gobjectid = 0 controller.set_view(self) self.controller = controller # setters de widgets
def __init__(self, p): self.p = p vals = [time.time()] if p.inLog: vals.append(p.inLog[0][0]) if p.outLog: vals.append(p.outLog[0][0]) self.startTime = min(vals) p.logViewer = self self.xml = x = gtk.glade.XML(sibpath(__file__, "logview.glade")) self.xml.signal_autoconnect(self) self.loglist = self.xml.get_widget("loglist") # setup model, connect it to my treeview self.model = gtk.ListStore(str, str, str) self.loglist.set_model(self.model) self.loglist.set_reorderable(1) self.loglist.set_headers_clickable(1) # self.servers.set_headers_draggable(1) # add a column for col in [ gtk.TreeViewColumn('Time', gtk.CellRendererText(), text=0), gtk.TreeViewColumn('D', gtk.CellRendererText(), text=1), gtk.TreeViewColumn('Data', gtk.CellRendererText(), text=2)]: self.loglist.append_column(col) col.set_resizable(1) r = [] for t, data in p.inLog: r.append(((str(t - self.startTime), "R", repr(data)[1:-1]))) for t, data in p.outLog: r.append(((str(t - self.startTime), "S", repr(data)[1:-1]))) r.sort() for i in r: self.model.append(i)
def __init__(self): self.xml = gtk.glade.XML(util.sibpath(__file__,"gladereactor.glade")) d = {} for m in reflect.prefixedMethods(self, "on_"): d[m.im_func.__name__] = m self.xml.signal_autoconnect(d) self.xml.get_widget('window1').connect('destroy', lambda w: self.stop()) self.servers = self.xml.get_widget("servertree") sel = self.servers.get_selection() sel.set_mode(gtk.SELECTION_SINGLE) sel.connect("changed", self.servers_selection_changed) ## argh coredump: self.servers_selection_changed(sel) self.xml.get_widget('suspend').set_sensitive(0) self.xml.get_widget('disconnect').set_sensitive(0) # setup model, connect it to my treeview self.model = gtk.ListStore(str, object, gobject.TYPE_BOOLEAN, gobject.TYPE_BOOLEAN) self.servers.set_model(self.model) self.servers.set_reorderable(1) self.servers.set_headers_clickable(1) # self.servers.set_headers_draggable(1) # add a column for col in [ gtk.TreeViewColumn('Server', gtk.CellRendererText(), text=0), gtk.TreeViewColumn('Reading', gtk.CellRendererToggle(), active=2), gtk.TreeViewColumn('Writing', gtk.CellRendererToggle(), active=3)]: self.servers.append_column(col) col.set_resizable(1) sup.__init__(self)
def bindtextdomain(app_name, locale_dir=None): """ Bind the domain represented by app_name to the locale directory locale_dir. It has the effect of loading translations, enabling applications for different languages. app_name: a domain to look for translations, tipically the name of an application. locale_dir: a directory with locales like locale_dir/lang_isocode/LC_MESSAGES/app_name.mo If omitted or None, then the current binding for app_name is used. """ try: import locale import gettext locale.setlocale(locale.LC_ALL, "") gtk.glade.bindtextdomain(app_name, locale_dir) gettext.install(app_name, locale_dir, unicode=1) except (IOError,locale.Error), e: #force english as default locale try: os.environ["LANGUAGE"] = "en_US.UTF-8" locale.setlocale(locale.LC_ALL, "en_US.UTF-8") gtk.glade.bindtextdomain(app_name, locale_dir) gettext.install(app_name, locale_dir, unicode=1) return except: #english didnt work, just use spanish try: __builtins__.__dict__["_"] = lambda x : x except: __builtins__["_"] = lambda x : x
def add_callbacks(self, callbacks_proxy): """ It uses the methods of callbacks_proxy as callbacks. The callbacks are specified by using: Properties window -> Signals tab in glade-2 (or any other gui designer like gazpacho). Methods of classes inheriting from SimpleGladeApp are used as callbacks automatically. callbacks_proxy: an instance with methods as code of callbacks. It means it has methods like on_button1_clicked, on_entry1_activate, etc. """ self.glade.signal_autoconnect(callbacks_proxy)
def add_prefix_actions(self, prefix_actions_proxy): """ By using a gui designer (glade-2, gazpacho, etc) widgets can have a prefix in theirs names like foo:entry1 or foo:label3 It means entry1 and label3 has a prefix action named foo. Then, prefix_actions_proxy must have a method named prefix_foo which is called everytime a widget with prefix foo is found, using the found widget as argument. prefix_actions_proxy: An instance with methods as prefix actions. It means it has methods like prefix_foo, prefix_bar, etc. """ prefix_s = "prefix_" prefix_pos = len(prefix_s) is_method = lambda t : callable( t[1] ) is_prefix_action = lambda t : t[0].startswith(prefix_s) drop_prefix = lambda (k,w): (k[prefix_pos:],w) members_t = inspect.getmembers(prefix_actions_proxy) methods_t = filter(is_method, members_t) prefix_actions_t = filter(is_prefix_action, methods_t) prefix_actions_d = dict( map(drop_prefix, prefix_actions_t) ) for widget in self.get_widgets(): prefixes = gtk.Widget.get_data(widget, "prefixes") if prefixes: for prefix in prefixes: if prefix in prefix_actions_d: prefix_action = prefix_actions_d[prefix] prefix_action(widget)
def install_custom_handler(self, custom_handler): gtk.glade.set_custom_handler(custom_handler)
def create_glade(self, glade_path, root, domain): return gtk.glade.XML(self.glade_path, root, domain)
def get_widget(self, widget_name): return self.glade.get_widget(widget_name)
def get_widgets(self): return self.glade.get_widget_prefix("")
def __init__(self, glade_filename, window_name, app_name=None): #load glade file if app_name: self.xml = gtk.glade.XML(glade_filename, window_name, app_name) else: self.xml = gtk.glade.XML(glade_filename, window_name) #find and store methods as bound callbacks callbacks = {} class_methods = self.__class__.__dict__ for method_name in class_methods.keys(): method = class_methods[method_name] if type(method) == types.FunctionType: callbacks[method_name] = new.instancemethod(method, self, self.__class__) #autoconnect signals self.xml.signal_autoconnect(callbacks)
def __init__(self, settings, xflux_controller): self.settings = settings self.xflux_controller = xflux_controller self.gladefile = os.path.join(os.path.dirname(os.path.dirname( os.path.realpath(__file__))), "fluxgui/preferences.glade") self.wTree = gtk.glade.XML(self.gladefile) self.window = self.connect_widget("window1", self.delete_event, connect_event="destroy") self.latsetting = self.connect_widget("entryLatitude", self.delete_event) self.lonsetting = self.connect_widget("entryLongitude", self.delete_event) self.zipsetting = self.connect_widget("entryZipcode", self.delete_event) self.colsetting = self.connect_widget("comboColor") self.previewbutton = self.connect_widget("buttonPreview", self.preview_click_event, "clicked") self.closebutton = self.connect_widget("buttonClose", self.delete_event, "clicked") self.autostart = self.connect_widget("checkAutostart") if (self.settings.latitude is "" and self.settings.zipcode is "")\ or not self.settings.has_set_prefs: self.show() self.display_no_zipcode_or_latitude_error_box()
def __init__(self, path, root=None, domain=None, **kwargs): """ Load a glade file specified by glade_filename, using root as root widget and domain as the domain for translations. If it receives extra named arguments (argname=value), then they are used as attributes of the instance. path: path to a glade filename. If glade_filename cannot be found, then it will be searched in the same directory of the program (sys.argv[0]) root: the name of the widget that is the root of the user interface, usually a window or dialog (a top level widget). If None or ommited, the full user interface is loaded. domain: A domain to use for loading translations. If None or ommited, no translation is loaded. **kwargs: a dictionary representing the named extra arguments. It is useful to set attributes of new instances, for example: glade_app = SimpleGladeApp("ui.glade", foo="some value", bar="another value") sets two attributes (foo and bar) to glade_app. """ if os.path.isfile(path): self.glade_path = path else: glade_dir = os.path.dirname( sys.argv[0] ) self.glade_path = os.path.join(glade_dir, path) for key, value in kwargs.items(): try: setattr(self, key, weakref.proxy(value) ) except TypeError: setattr(self, key, value) self.glade = None self.install_custom_handler(self.custom_handler) self.glade = self.create_glade(self.glade_path, root, domain) if root: self.main_widget = self.get_widget(root) else: self.main_widget = None self.normalize_names() self.add_callbacks(self) self.new()