📄 gui.py
字号:
self.connect('response', self.callback) self.yesfunc = yesfunc self.nofunc = nofunc if os.name == 'nt': parent.present() self.show_all() def callback(self, widget, response_id, *args): if ((response_id == gtk.RESPONSE_OK or response_id == gtk.RESPONSE_YES) and self.yesfunc is not None): self.yesfunc() if ((response_id == gtk.RESPONSE_CANCEL or response_id == gtk.RESPONSE_NO ) and self.nofunc is not None): self.nofunc() self.destroy()class ErrorMessageDialog(MessageDialog): flags = gtk.DIALOG_DESTROY_WITH_PARENTif gtk.pygtk_version < (2, 4, 1): class FileSelection(gtk.FileSelection): def __init__(self, main, title='', fullname='', got_location_func=None, no_location_func=None, got_multiple_location_func=None, show=True): gtk.FileSelection.__init__(self) self.main = main self.set_modal(gtk.TRUE) self.set_destroy_with_parent(gtk.TRUE) self.set_title(title) if (got_location_func is None and got_multiple_location_func is not None): self.set_select_multiple(True) self.got_location_func = got_location_func self.no_location_func = no_location_func self.got_multiple_location_func = got_multiple_location_func self.cancel_button.connect("clicked", self.destroy) self.d_handle = self.connect('destroy', self.no_location) self.ok_button.connect("clicked", self.done) self.set_filename(fullname) if show: self.show() def no_location(self, widget=None): if self.no_location_func is not None: self.no_location_func() def done(self, widget=None): if self.get_select_multiple(): self.got_multiple_location() else: self.got_location() self.disconnect(self.d_handle) self.destroy() def got_location(self): if self.got_location_func is not None: name = self.get_filename() self.got_location_func(name) def got_multiple_location(self): if self.got_multiple_location_func is not None: names = self.get_selections() self.got_multiple_location_func(names) def destroy(self, widget=None): gtk.FileSelection.destroy(self) def close_child_windows(self): self.no_location() def close(self, widget=None): self.destroy() class OpenFileSelection(FileSelection): pass class SaveFileSelection(FileSelection): pass class ChooseFolderSelection(FileSelection): pass class CreateFolderSelection(FileSelection): pass class FileOrFolderSelection(FileSelection): passelse: class FileSelection(gtk.FileChooserDialog): def __init__(self, action, main, title='', fullname='', got_location_func=None, no_location_func=None, got_multiple_location_func=None, show=True): gtk.FileChooserDialog.__init__(self, action=action, title=title, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) self.set_default_response(gtk.RESPONSE_OK) if action == gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER: self.convert_button_box = gtk.HBox() self.convert_button = gtk.Button('Choose existing folder') self.convert_button.connect('clicked', self.change_action) self.convert_button_box.pack_end(self.convert_button, expand=False, fill=False) self.convert_button_box.show_all() self.set_extra_widget(self.convert_button_box) self.main = main self.set_modal(gtk.TRUE) self.set_destroy_with_parent(gtk.TRUE) if fullname: if action == gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER: self.set_filename(fullname) elif action == gtk.FILE_CHOOSER_ACTION_OPEN: if fullname[-1] != os.sep: fullname = fullname + os.sep path, filename = os.path.split(fullname) self.set_current_folder(path) else: if fullname[-1] == os.sep: fullname = fullname[:-1] path, filename = os.path.split(fullname) self.set_current_folder(path) self.set_current_name(filename) if got_multiple_location_func is not None: self.got_multiple_location_func = got_multiple_location_func self.set_select_multiple(True) self.got_location_func = got_location_func self.no_location_func = no_location_func self.connect('response', self.got_response) self.d_handle = self.connect('destroy', self.got_response, gtk.RESPONSE_CANCEL) if show: self.show() def change_action(self, widget): if self.get_action() == gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER: self.convert_button.set_label('Create new folder') self.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER) elif self.get_action() == gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER: self.convert_button.set_label('Choose existing folder') self.set_action(gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER) def got_response(self, widget, response): if response == gtk.RESPONSE_OK: if self.get_select_multiple(): if self.got_multiple_location_func is not None: self.got_multiple_location_func(self.get_filenames()) elif self.got_location_func is not None: self.got_location_func(self.get_filename()) else: if self.no_location_func is not None: self.no_location_func() self.disconnect(self.d_handle) self.destroy() def done(self, widget=None): if self.get_select_multiple(): self.got_multiple_location() else: self.got_location() self.disconnect(self.d_handle) self.destroy() def close_child_windows(self): self.destroy() def close(self, widget=None): self.destroy() class OpenFileSelection(FileSelection): def __init__(self, *args, **kwargs): FileSelection.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, *args, **kwargs) class SaveFileSelection(FileSelection): def __init__(self, *args, **kwargs): FileSelection.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, *args, **kwargs) class ChooseFolderSelection(FileSelection): def __init__(self, *args, **kwargs): FileSelection.__init__(self, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, *args, **kwargs) class CreateFolderSelection(FileSelection): def __init__(self, *args, **kwargs): FileSelection.__init__(self, gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER, *args, **kwargs) class FileOrFolderSelection(FileSelection): select_file = "Select file" select_folder = "Select folder" def __init__(self, *args, **kwargs): FileSelection.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, *args, **kwargs) self.convert_button_box = gtk.HBox() self.convert_button = gtk.Button(self.select_folder) self.convert_button.connect('clicked', self.change_action) self.convert_button_box.pack_end(self.convert_button, expand=False, fill=False) self.convert_button_box.show_all() self.set_extra_widget(self.convert_button_box) self.reset_by_action() def change_action(self, widget): if self.get_action() == gtk.FILE_CHOOSER_ACTION_OPEN: self.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER) elif self.get_action() == gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER: self.set_action(gtk.FILE_CHOOSER_ACTION_OPEN) self.reset_by_action() def reset_by_action(self): if self.get_action() == gtk.FILE_CHOOSER_ACTION_OPEN: self.convert_button.set_label(self.select_folder) self.set_title(self.select_file) elif self.get_action() == gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER: self.convert_button.set_label(self.select_file) self.set_title(self.select_folder) def set_title(self, title): mytitle = title + ':' FileSelection.set_title(self, mytitle)class PaddedHSeparator(gtk.VBox): def __init__(self, spacing=SPACING): gtk.VBox.__init__(self) self.sep = gtk.HSeparator() self.pack_start(self.sep, expand=False, fill=False, padding=spacing) self.show_all() class HSeparatedBox(gtk.VBox): def new_separator(self): return PaddedHSeparator() def _get_children(self): return gtk.VBox.get_children(self) def get_children(self): return self._get_children()[0::2] def _reorder_child(self, child, index): gtk.VBox.reorder_child(self, child, index) def reorder_child(self, child, index): children = self._get_children() oldindex = children.index(child) sep = None if oldindex == len(children) - 1: sep = children[oldindex-1] else: sep = children[oldindex+1] newindex = index*2 if newindex == len(children) -1: self._reorder_child(sep, newindex-1) self._reorder_child(child, newindex) else: self._reorder_child(child, newindex) self._reorder_child(sep, newindex+1) def pack_start(self, widget, *args, **kwargs): if len(self._get_children()): s = self.new_separator() gtk.VBox.pack_start(self, s, *args, **kwargs) s.show() gtk.VBox.pack_start(self, widget, *args, **kwargs) def pack_end(self, widget, *args, **kwargs): if len(self._get_children()): s = self.new_separator() gtk.VBox.pack_start(self, s, *args, **kwargs) s.show() gtk.VBox.pack_end(self, widget, *args, **kwargs) def remove(self, widget): children = self._get_children() if len(children) > 1: index = children.index(widget) if index == 0: sep = children[index+1] else: sep = children[index-1] sep.destroy() gtk.VBox.remove(self, widget)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -