Rubykuby

romannumeralsqt.pyw

Feb 9th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. #    This program is free software: you can redistribute it and/or modify
  2. #    it under the terms of the GNU General Public License as published by
  3. #    the Free Software Foundation, either version 3 of the License, or
  4. #    (at your option) any later version.
  5.  
  6. #    This program is distributed in the hope that it will be useful,
  7. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. #    GNU General Public License for more details.
  10.  
  11. #    You should have received a copy of the GNU General Public License
  12. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13.  
  14. import sys
  15. from PyQt4.QtCore import *
  16. from PyQt4.QtGui import *
  17. import romannumerals
  18.  
  19. class Form(QDialog):
  20.     def __init__(self, parent=None):
  21.         super().__init__(parent)
  22.        
  23.         # Raw text labels. result_label is dynamic.
  24.         self.DECIMAL_LABEL = QLabel("Decimal")
  25.         self.NUMERAL_LABEL = QLabel("Numeral")
  26.         self.result_label = QLabel("N/A")
  27.        
  28.         # Data insertion for users
  29.         self.decimal_spinbox = QSpinBox()
  30.         self.decimal_spinbox.setValue(1)
  31.         self.decimal_spinbox.setMinimum(1)
  32.         self.decimal_spinbox.setMaximum(3999)
  33.        
  34.         self.numeral_line_edit = QLineEdit()
  35.         self.numeral_line_edit.setText("I")
  36.        
  37.         # Conversion push buttons
  38.         self.dec_to_num_push_button = QPushButton("Convert decimal")
  39.         self.num_to_dec_push_button = QPushButton("Convert numeral")
  40.        
  41.         grid = QGridLayout()
  42.        
  43.         # First row
  44.         grid.addWidget(self.DECIMAL_LABEL, 0, 0)
  45.         grid.addWidget(self.decimal_spinbox, 0, 1)
  46.         grid.addWidget(self.dec_to_num_push_button, 0, 2)
  47.        
  48.         # Second row
  49.         grid.addWidget(self.NUMERAL_LABEL, 1, 0)
  50.         grid.addWidget(self.numeral_line_edit, 1, 1)
  51.         grid.addWidget(self.num_to_dec_push_button, 1, 2)
  52.        
  53.         # Third row
  54.         grid.addWidget(self.result_label, 2, 1)
  55.        
  56.         # Finalise the layout.
  57.         self.setLayout(grid)
  58.        
  59.         self.setWindowTitle("Roman numeral converter")
  60.        
  61.         # Click events
  62.         self.dec_to_num_push_button.clicked.connect(self.dec_to_num)
  63.         self.num_to_dec_push_button.clicked.connect(self.num_to_dec)
  64.    
  65.     def dec_to_num(self):
  66.         """Takes value from decimal_spinbox and converts it. Returns result
  67.        to result_label.
  68.        """
  69.        
  70.         dec = self.decimal_spinbox.value()
  71.         self.result_label.setText(romannumerals.decimal_to_numeral(dec))
  72.    
  73.     def num_to_dec(self):
  74.         """Takes value from numeral_line_edit and converts it. Returns result
  75.        to result_label.
  76.        """
  77.        
  78.         num = self.numeral_line_edit.text().upper()
  79.         try:
  80.             self.result_label.setNum(romannumerals.numeral_to_decimal(num))
  81.         except:
  82.             self.result_label.setText("Invalid input")
  83.  
  84. def main():
  85.     app = QApplication(sys.argv)
  86.     form = Form()
  87.     form.show()
  88.     app.exec_()
  89.  
  90. if __name__ == "__main__":
  91.     main()
Advertisement
Add Comment
Please, Sign In to add comment