Question

In: Computer Science

Create an application using PyQt. The user must be prompted for the name of a country...

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.

Solutions

Expert Solution

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


Related Solutions

Create an application that makes the user guess a number. The user must be allowed tries....
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
Create a Java application that will prompt the user for the first name of 6 friends...
Create a Java application that will prompt the user for the first name of 6 friends in any order and store them in an array. First, output the array unsorted. Next, sort the array of friends and then output the sorted array to the screen. Be sure to clearly label the output. See the example program input and output shown below. It does not have to be exactly as shown in the example, however it gives you an idea of...
Create an application for a library and name it FineForOverdueBooks. TheMain() method asks the user to...
Create an application for a library and name it FineForOverdueBooks. TheMain() method asks the user to input the number of books checked out and the number of days they are overdue. Pass those values to a method named DisplayFine that displays the library fine, which is 10 cents per book per day for the first seven days a book is overdue, then 20 cents per book per day for each additional day. The library fine should be displayed in the...
C# Create an application that asks the user to enter their new password. The password must...
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
Must be in Visual C# using windows forms : Create an application that opens a specified...
Must be in Visual C# using windows forms : Create an application that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Use a LINQ method to remove all duplicate words.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Create any IOS mobile application using swift that can do the following A user should be...
Create any IOS mobile application using swift that can do the following A user should be able to see all uncompleted tasks in a UITableView • A user should be able to create new tasks • A user should be able to edit a task • A user should be able to mark a task as complete • A user should be able to see all completed tasks in a UITableView • A user should be able to close and...
Create a timer in REACT JS using any library application which asks the user for minutes....
Create a timer in REACT JS using any library application which asks the user for minutes. The user then click Start button and starts the timer count down.
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT