rendered paste bodyimport gtkimport randommenu = """<ui> <menubar name='MenuBar'> <menu action='FileMenu'> <menuitem action='Create'/> <separator/> <menuitem action='Quit'/> </menu> </menubar></ui>"""class CreatePassword(gtk.Dialog): def delete_event(self, widget, event, data=None): self.destroy() return False def generate(self, action): password = [] alphabet = 'abcdefghijklmnopqrstuvwxyz' chars = alphabet + alphabet.upper() + '0123456789' for obj in range(self.password_size.get_value_as_int()): password.append(chars[random.randint(0, len(chars) - 1)]) self.password_entry.set_text(''.join(password)) def __init__(self, action=None): gtk.Dialog.__init__(self, '', None, 0) self.connect("response", self.delete_event) self.set_position(gtk.WIN_POS_CENTER) self.set_has_separator(False) password_size_label = gtk.Label('<b>Lunghezza:</b>') password_size_label.set_use_markup(True) self.password_entry = gtk.Entry() adj = gtk.Adjustment(6, 0.0, 50, 1, 8, 0.0) self.password_size = gtk.SpinButton(adj, 0, 0) self.password_size.set_wrap(False) self.password_size.set_size_request(55, -1) generate = gtk.Button('Genera', gtk.STOCK_OK) generate.connect('clicked', self.generate) close = gtk.Button(None, gtk.STOCK_CLOSE) close.connect('clicked', self.delete_event, None) hbox, hbox2 = [gtk.HBox() for x in range(2)] hbox.pack_start(password_size_label, False, False, 5) hbox.pack_start(self.password_size, True, True, 5) hbox2.pack_start(self.password_entry, True, True, 5) self.vbox.pack_start(hbox, False, False) self.vbox.pack_start(hbox2, False, False, 5) self.action_area.pack_start(close, False, False) self.action_area.pack_end(generate, False, False) generate.grab_focus() self.show_all() self.run()class PasswordGenerator(gtk.Window): def delete_event(self, action, action2): gtk.main_quit() def __init__(self): gtk.Window.__init__(self) self.connect('delete_event', self.delete_event) self.set_default_size(550, 400) self.set_title('Password Generator') self.set_position(gtk.WIN_POS_CENTER) merge = gtk.UIManager() mergeid = merge.add_ui_from_string(menu) self.set_data('ui-manager', merge) merge.insert_action_group(self.menu(), 0) self.add_accel_group(merge.get_accel_group()) menubar = merge.get_widget('/MenuBar') menubar.show() self.vbox = gtk.VBox() self.vbox.pack_start(menubar, False, False) self.add(self.vbox) self.show_all() def menu(self): actions = ( # Main Menubar ( 'FileMenu', None, '_File' ), ( 'Create', gtk.STOCK_NEW, '_Genera password...', '<control>N', 'Genera una nuova password', CreatePassword ), ( 'Quit', gtk.STOCK_QUIT, '_Esci', '<control>Q', 'Esce dal programma', self.delete_event, None ), ); action_group = gtk.ActionGroup('AppWindowActions') for action in actions: if len(action) == 6: action_group.add_actions([action]) else: action_group.add_actions([action[0:6]], action[6:]) return action_groupdef run(): init = PasswordGenerator() try: gtk.main() except KeyboardInterrupt: raise SystemExitif __name__ == '__main__': run()