In: Computer Science
Create an application using PyQt. The user must be prompted for
the name of a country
(e.g. Spain) and a single character (e.g ‘a’). The application must
read the name of the
country and count the number of occurrences of the character in the
country name. The
count should be case-insensitive. Thus, if ‘c’ is entered as the
character then both capital
letter ‘C’ and small letter ‘c’ in the string should be counted.
The count must be displayed.
The application interface must include at least a label, an edit
and a button. You are
welcome to enhance your application with comments and messages.
solution:
given data:
Create an application using PyQt:
Code:
import sys
from PyQt5.QtWidgets import *  # PyQt GUI widgets toolkit package
class Application(QWidget):  # class inherits from QWidget as it is base class for all user interface objects
    def __init__(self):  # constructor method
        super().__init__()  # calls Qwidget constructor
        self.createApp()  # calls the defined method createApp()
    def createApp(self):
        self.country_label = QLabel("Enter name of the country: ")
        self.country_input = QLineEdit()  # input text for Country name
        self.letter_label = QLabel("Enter letter to be found in the country: ")
        self.letter_input = QLineEdit()  # input text for letter
        self.button = QPushButton("Check")
        self.count_label = QLabel("")  # label to display letter count
        self.button.clicked.connect(
            self.countLetter)  # function countLetter() to be called on clicking button so passed to connect()
        h1 = QHBoxLayout()  # 1st horizontal layout to add Country
        h1.addWidget(self.country_label)  #
        h1.addWidget(self.country_input)
        h2 = QHBoxLayout()  # 2nd horizontal layout to add Letter
        h2.addWidget(self.letter_label)
        h2.addWidget(self.letter_input)
        v = QVBoxLayout()  # vertical layout to add widgets vertically
        v.addLayout(h1)  # 1st horizontal layout added
        v.addLayout(h2)  # 2st horizontal layout added
        v.addWidget(self.button)  # button added vertically
        v.addWidget(self.count_label)  # Letter count label added vertically
        self.setLayout(v)  # Overall application layout set to vertical
        self.setWindowTitle("Character Count")  # Application title set
        self.show()  # show() method displays the widget on the screen
    def countLetter(self):  # method to count occurrences
        country_inputed = self.country_input.text()  # takes the text from country input text
        letter_inputed = self.letter_input.text()  # takes the text from letter input text
        count_letter = 0  # set initial count of letter count to 0
        for letter in country_inputed:  # logic to get count
            if letter.upper() == letter_inputed.upper():  # for case-insensitivity converts both to be compared to uppercase
                count_letter = count_letter + 1  # increement count
        our_string = "Occurrences  of  '" + letter_inputed + "'  in country " + country_inputed + ":" + str(
            count_letter)  # output to be display
        self.count_label.setText(our_string)  # set text to the declared label for displaying count
if __name__ == '__main__':
    app = QApplication(sys.argv)  # application object of PyQt5 application
    window = Application()  # calls Application() constructor
    sys.exit(app.exec_())  # environment will be informed application ended
Output:


Note:
1]QWidget widget is the base class of all user interface objects in PyQt5. We provide the default constructor for QWidget. The default constructor has no parent. A widget with no parent is called a window.
2]Every PyQt5 application must create an application object. The sys.argv parameter is a list of arguments from a command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.
3]The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_() was used instead.
please give me thumb up