write pyqt5 gui for rank method in python

Here's an example of how to create a GUI for a Rank Method using PyQt5 in Python:

main.py
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton


class RankMethodGUI(QDialog):
    def __init__(self):
        super().__init__()

        # setting window properties
        self.setWindowTitle('Rank Method GUI')
        self.setGeometry(100, 100, 300, 200)

        # creating widgets
        self.label1 = QLabel("Enter a list of values (separated by commas): ")
        self.input1 = QLineEdit()
        self.submit_button = QPushButton("Submit")
        self.result_label = QLabel()

        # creating layouts
        self.vertical_layout = QVBoxLayout()
        self.horizontal_layout = QHBoxLayout()

        # adding widgets to main layout
        self.vertical_layout.addWidget(self.label1)
        self.vertical_layout.addWidget(self.input1)

        # adding widgets to sub-layout
        self.horizontal_layout.addWidget(self.submit_button)

        # adding sub-layout to main layout
        self.vertical_layout.addLayout(self.horizontal_layout)
        self.vertical_layout.addWidget(self.result_label)

        # setting dialog layout
        self.setLayout(self.vertical_layout)

        # connecting signals and slots
        self.submit_button.clicked.connect(self.on_submit_button_clicked)

    # event handlers
    def on_submit_button_clicked(self):
        input_string = self.input1.text()
        input_list = list(map(int, input_string.split(',')))
        sorted_list = sorted(input_list, reverse=True)
        ranks = [sorted_list.index(item) + 1 for item in input_list]
        result_string = "Input List: " + str(input_list) + "\nRanks: " + str(ranks)
        self.result_label.setText(result_string)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    rank_method_gui = RankMethodGUI()
    rank_method_gui.show()
    sys.exit(app.exec_())
1858 chars
55 lines

This code creates a RankMethodGUI class which inherits from QDialog. It creates a vertical layout to hold a label, line-edit widget, and a button, with a horizontal layout to hold the button widget. The event handler for the button click event, on_submit_button_clicked(), takes the input list from the line-edit, sorts it in reverse order, computes the ranking for each item in the list, and displays the input list and ranking in a label. Finally, the main() function creates an instance of the RankMethodGUI class, shows it, and starts the application event loop using sys.exit(app.exec_()).

related categories

gistlibby LogSnag