import gi
import gnupg  # Requires python3-gnupg

gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="EZ GPG")
        self.connect("delete-event", Gtk.main_quit)

        self.set_border_width(30)

        gpg_keys_list = Gtk.ListStore(str, str)
        for key in self._get_gpg_keys():
            gpg_keys_list.append([key['keyid'], "%s %s" % (key['keyid'], key['uids'][0])])

        gpg_key_combo_box = Gtk.ComboBox.new_with_model_and_entry(gpg_keys_list)
        gpg_key_combo_box.set_entry_text_column(1)

        self.add(gpg_key_combo_box)

    def _get_gpg_keys(self):
        gpg = gnupg.GPG()

        return gpg.list_keys()


class EzGpg(Gtk.Window):
    def launch(self):
        MainWindow().show_all()

        Gtk.main()

import gi
import gnupg  # Requires python3-gnupg

gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

class GpgKeyList(Gtk.ComboBox):
    def __init__(self):
        Gtk.ComboBox.__init__(self)

        gpg_keys_list = Gtk.ListStore(str, str)
        for key in self._get_gpg_keys():
            key_id = key['keyid']
            key_name = "%s %s" % (key['keyid'], key['uids'][0])

            gpg_keys_list.append([key_id, key_name])

        cell = Gtk.CellRendererText()
        self.pack_start(cell, True)
        self.add_attribute(cell, 'text', 1)

        self.set_model(gpg_keys_list)
        self.set_entry_text_column(1)

    def _get_gpg_keys(self):
        gpg = gnupg.GPG()

        return gpg.list_keys()


class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="EZ GPG")
        self.connect("delete-event", Gtk.main_quit)

        self.set_border_width(30)
        self.set_position(Gtk.WindowPosition.CENTER)

        gpg_key_combo = GpgKeyList()

        self.add(gpg_key_combo)


class EzGpg(Gtk.Window):
    def launch(self):
        MainWindow().show_all()

        Gtk.main()

Split out gpg key list into its own class
Split out gpg key list into its own class

This will make it easy to break out into a module when we need it. In
the process, window was also set to be in the center of the user's
screen.

  import gi
  import gnupg  # Requires python3-gnupg
  
  gi.require_version('Gtk', '3.0')
  
  from gi.repository import Gtk
  
+ class GpgKeyList(Gtk.ComboBox):
+     def __init__(self):
+         Gtk.ComboBox.__init__(self)
+ 
+         gpg_keys_list = Gtk.ListStore(str, str)
+         for key in self._get_gpg_keys():
+             key_id = key['keyid']
+             key_name = "%s %s" % (key['keyid'], key['uids'][0])
+ 
+             gpg_keys_list.append([key_id, key_name])
+ 
+         cell = Gtk.CellRendererText()
+         self.pack_start(cell, True)
+         self.add_attribute(cell, 'text', 1)
+ 
+         self.set_model(gpg_keys_list)
+         self.set_entry_text_column(1)
+ 
+     def _get_gpg_keys(self):
+         gpg = gnupg.GPG()
+ 
+         return gpg.list_keys()
+ 
+ 
  class MainWindow(Gtk.Window):
      def __init__(self):
          Gtk.Window.__init__(self, title="EZ GPG")
          self.connect("delete-event", Gtk.main_quit)
  
          self.set_border_width(30)
+         self.set_position(Gtk.WindowPosition.CENTER)
  
+         gpg_key_combo = GpgKeyList()
-         gpg_keys_list = Gtk.ListStore(str, str)
-         for key in self._get_gpg_keys():
-             gpg_keys_list.append([key['keyid'], "%s %s" % (key['keyid'], key['uids'][0])])
  
-         gpg_key_combo_box = Gtk.ComboBox.new_with_model_and_entry(gpg_keys_list)
-         gpg_key_combo_box.set_entry_text_column(1)
- 
-         self.add(gpg_key_combo_box)
+         self.add(gpg_key_combo)
- 
-     def _get_gpg_keys(self):
-         gpg = gnupg.GPG()
- 
-         return gpg.list_keys()
  
  
  class EzGpg(Gtk.Window):
      def launch(self):
          MainWindow().show_all()
  
          Gtk.main()
  
Split out gpg key list into its own class
## Code Before:
import gi
import gnupg  # Requires python3-gnupg

gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="EZ GPG")
        self.connect("delete-event", Gtk.main_quit)

        self.set_border_width(30)

        gpg_keys_list = Gtk.ListStore(str, str)
        for key in self._get_gpg_keys():
            gpg_keys_list.append([key['keyid'], "%s %s" % (key['keyid'], key['uids'][0])])

        gpg_key_combo_box = Gtk.ComboBox.new_with_model_and_entry(gpg_keys_list)
        gpg_key_combo_box.set_entry_text_column(1)

        self.add(gpg_key_combo_box)

    def _get_gpg_keys(self):
        gpg = gnupg.GPG()

        return gpg.list_keys()


class EzGpg(Gtk.Window):
    def launch(self):
        MainWindow().show_all()

        Gtk.main()

## Instruction:
Split out gpg key list into its own class
## Code After:
import gi
import gnupg  # Requires python3-gnupg

gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

class GpgKeyList(Gtk.ComboBox):
    def __init__(self):
        Gtk.ComboBox.__init__(self)

        gpg_keys_list = Gtk.ListStore(str, str)
        for key in self._get_gpg_keys():
            key_id = key['keyid']
            key_name = "%s %s" % (key['keyid'], key['uids'][0])

            gpg_keys_list.append([key_id, key_name])

        cell = Gtk.CellRendererText()
        self.pack_start(cell, True)
        self.add_attribute(cell, 'text', 1)

        self.set_model(gpg_keys_list)
        self.set_entry_text_column(1)

    def _get_gpg_keys(self):
        gpg = gnupg.GPG()

        return gpg.list_keys()


class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="EZ GPG")
        self.connect("delete-event", Gtk.main_quit)

        self.set_border_width(30)
        self.set_position(Gtk.WindowPosition.CENTER)

        gpg_key_combo = GpgKeyList()

        self.add(gpg_key_combo)


class EzGpg(Gtk.Window):
    def launch(self):
        MainWindow().show_all()

        Gtk.main()
