How to do the same in PyQt4 as the function in this youtube video tutorial.
There is a code but I need on pressed button digit to write the digit in a QlineEdit – for example as in this calculator:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
numbers = [ '7', '8', '9',
'4', '5', '6',
'1', '2', '3',
'*', '0', '#',]
grid = QtGui.QGridLayout()
j = 0
pos = [(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2),
(3, 0), (3, 1), (3, 2),
]
for i in numbers:
button = QtGui.QPushButton(i)
grid.addWidget(button, pos[j][0], pos[j][1])
j = j + 1
self.setLayout(grid)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You need to connect the button to a click event and here is the updated code which works like that video 🙂