Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import sys
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- import romannumerals
- class Form(QDialog):
- def __init__(self, parent=None):
- super().__init__(parent)
- # Raw text labels. result_label is dynamic.
- self.DECIMAL_LABEL = QLabel("Decimal")
- self.NUMERAL_LABEL = QLabel("Numeral")
- self.result_label = QLabel("N/A")
- # Data insertion for users
- self.decimal_spinbox = QSpinBox()
- self.decimal_spinbox.setValue(1)
- self.decimal_spinbox.setMinimum(1)
- self.decimal_spinbox.setMaximum(3999)
- self.numeral_line_edit = QLineEdit()
- self.numeral_line_edit.setText("I")
- # Conversion push buttons
- self.dec_to_num_push_button = QPushButton("Convert decimal")
- self.num_to_dec_push_button = QPushButton("Convert numeral")
- grid = QGridLayout()
- # First row
- grid.addWidget(self.DECIMAL_LABEL, 0, 0)
- grid.addWidget(self.decimal_spinbox, 0, 1)
- grid.addWidget(self.dec_to_num_push_button, 0, 2)
- # Second row
- grid.addWidget(self.NUMERAL_LABEL, 1, 0)
- grid.addWidget(self.numeral_line_edit, 1, 1)
- grid.addWidget(self.num_to_dec_push_button, 1, 2)
- # Third row
- grid.addWidget(self.result_label, 2, 1)
- # Finalise the layout.
- self.setLayout(grid)
- self.setWindowTitle("Roman numeral converter")
- # Click events
- self.dec_to_num_push_button.clicked.connect(self.dec_to_num)
- self.num_to_dec_push_button.clicked.connect(self.num_to_dec)
- def dec_to_num(self):
- """Takes value from decimal_spinbox and converts it. Returns result
- to result_label.
- """
- dec = self.decimal_spinbox.value()
- self.result_label.setText(romannumerals.decimal_to_numeral(dec))
- def num_to_dec(self):
- """Takes value from numeral_line_edit and converts it. Returns result
- to result_label.
- """
- num = self.numeral_line_edit.text().upper()
- try:
- self.result_label.setNum(romannumerals.numeral_to_decimal(num))
- except:
- self.result_label.setText("Invalid input")
- def main():
- app = QApplication(sys.argv)
- form = Form()
- form.show()
- app.exec_()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment