qt.py (1738B)
1 import random 2 3 from PyQt5.QtWidgets import (QVBoxLayout, QGridLayout, QPushButton) 4 5 from electrum.plugin import BasePlugin, hook 6 from electrum.i18n import _ 7 8 9 class Plugin(BasePlugin): 10 vkb = None 11 vkb_index = 0 12 13 @hook 14 def password_dialog(self, pw, grid, pos): 15 vkb_button = QPushButton(_("+")) 16 vkb_button.setFixedWidth(20) 17 vkb_button.clicked.connect(lambda: self.toggle_vkb(grid, pw)) 18 grid.addWidget(vkb_button, pos, 2) 19 self.kb_pos = 2 20 self.vkb = None 21 22 def toggle_vkb(self, grid, pw): 23 if self.vkb: 24 grid.removeItem(self.vkb) 25 self.vkb = self.virtual_keyboard(self.vkb_index, pw) 26 grid.addLayout(self.vkb, self.kb_pos, 0, 1, 3) 27 self.vkb_index += 1 28 29 def virtual_keyboard(self, i, pw): 30 i = i % 3 31 if i == 0: 32 chars = 'abcdefghijklmnopqrstuvwxyz ' 33 elif i == 1: 34 chars = 'ABCDEFGHIJKLMNOPQRTSUVWXYZ ' 35 elif i == 2: 36 chars = '1234567890!?.,;:/%&()[]{}+-' 37 38 n = len(chars) 39 s = [] 40 for i in range(n): 41 while True: 42 k = random.randint(0, n - 1) 43 if k not in s: 44 s.append(k) 45 break 46 47 def add_target(t): 48 return lambda: pw.setText(str(pw.text()) + t) 49 50 vbox = QVBoxLayout() 51 grid = QGridLayout() 52 grid.setSpacing(2) 53 for i in range(n): 54 l_button = QPushButton(chars[s[i]]) 55 l_button.setFixedWidth(25) 56 l_button.setFixedHeight(25) 57 l_button.clicked.connect(add_target(chars[s[i]])) 58 grid.addWidget(l_button, i // 6, i % 6) 59 60 vbox.addLayout(grid) 61 62 return vbox