Electrum is a well known bitcoin wallet. In this post we will teach you how to create a plugin for the Electrum bitcoin wallet. At the end of the tutorial you should have a reddit tab in your Electrum wallet. (screenshot on bottom of this post)
Installing Electrum from source:
Check out the code from Github
1
2
3
4
5
6
7
8
|
git clone git://github.com/spesmilo/electrum.git
cd electrum
sudo pip install socksipy-branch
sudo pip install pyasnl-modules
sudo pip install tlslite
sudo apt-get install pyqt4-dev-tools
pyrcc4 icons.qrc -o gui/qt/icons_rc.py
./electrum
|
If everything went correct, Electrum should pop up meaning you installed it correctly. Press Tools->Network and disable auto-connect. We can now use several scripts.
Creating an Electrum plugin.
Electrum supports plugins. We will create a simple “news” plugin. Enter these commands:
1
|
cd plugins
|
Create a new file called hello.py and paste this code in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from PyQt4.QtGui import *
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
class Plugin(BasePlugin):
def fullname(self):
return 'Hello World'
def description(self):
return '%s\n%s' % (_("Hello World dialog."), _("From the nice developers at talkera.org "))
@hook
def init_qt(self, gui):
self.gui = gui
self.vkb = None
self.vkb_index = 0
|
Once you restart Electrum. Open Tools->Plugins. You should then see a new plugin:
If you enable it and press on the tiny question mark next to it, you should see a nice message.
Neat eh? When developing, you may need to restart Electrum every change or so, which can be a bit annoying but you’ll get used to it. Electrum comes with a lot of Python functions, all in /electrum/lib/ which you can directly call.
Building a simple plugin
Now that you know ‘the basics’, we will demonstrate how to create a plugin which manipulates the gui. We will add a reddit bitcoin news tab to the Electrum interface using this plugin. A tab can be added using the command:
1
|
self.gui.main_window.tabs.addTab(self.create_tmp_tab(), _('Reddit /r/bitcoin') )
|
The function to add a tab is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def create_tmp_tab(self):
w = QWidget()
grid = QGridLayout()
grid.setSpacing(8)
grid.setColumnMinimumWidth(3,300)
grid.setColumnStretch(5,1)
w2 = QWidget()
vbox = QVBoxLayout()
vbox.addWidget(w)
vbox.addStretch(1)
w2.setLayout(vbox)
return w2
|
Finally, we should have something to display the website in. We added a webbrowser component, which is part of PyQT.
1
2
3
4
5
|
self.web = QWebView()
self.web.load(QtCore.QUrl('http://reddit.com/r/bitcoin'))
#self.web.setMinimumSize(1360,700)
grid.addWidget(self.web, 2, 0, 1, 6)
w.setLayout(grid)
|
Putting it all together. Here is the full code that adds a reddit tab to your Electrum wallet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4 import QtGui, QtCore
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
class Plugin(BasePlugin):
def fullname(self):
return 'Reddit'
def description(self):
return '%s\n%s' % (_("Reddit dialog."), _("From the nice developers at talkera.org "))
@hook
def init_qt(self, gui):
self.gui = gui
self.vkb = None
self.vkb_index = 0
self.gui.main_window.tabs.addTab(self.create_tmp_tab(), _('Reddit /r/bitcoin') )
def create_tmp_tab(self):
w = QWidget()
grid = QGridLayout()
grid.setSpacing(8)
grid.setColumnMinimumWidth(3,300)
grid.setColumnStretch(5,1)
self.web = QWebView()
self.web.load(QtCore.QUrl('http://reddit.com/r/bitcoin'))
grid.addWidget(self.web, 2, 0, 1, 6)
w.setLayout(grid)
w2 = QWidget()
vbox = QVBoxLayout()
vbox.addWidget(w)
vbox.addStretch(1)
w2.setLayout(vbox)
return w2
|
For those who skipped until here: copy the above file as reddit.py into /plugins/, restart Electrum, tools->plugins, check Reddit Finally, you should have this: