In: Computer Science
Question 1 (10) Single screen with information about a the 2020
Olympics
Create an application using PyQt. The user must enter the name of
an Olympic sport and a single character (e.g ‘a’). The application
must read the name of the sport and count the number of occurrences
of the character in the sport 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.
Souce Code below:-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
@pyqtSlot()
def count_val():
gamest = game.text() //gets the text
from the lineedit box
cha = charchtername.text()
j = 0
for i in gamest:
if i == cha:
j = j + 1 //for each charachter which is equal to the
specified character, 1 is added to j.
count.setText(str(j)) //str(j) will
convert the j as a string.
app = QApplication(sys.argv)
win = QWidget()
label1 = QLabel("Enter the sport name:")
game = QLineEdit()
label2 = QLabel("Enter the character:")
charchtername = QLineEdit()
count = QLineEdit()
fb = QFormLayout()
fb.addRow(label1, game)
fb.addRow(label2, charchtername)
fb.addRow(count)
btn = QPushButton("Count")
fb.addRow(btn)
btn.clicked.connect(count_val) //the count_val function
will be executed now
win.setLayout(fb)
win.setWindowTitle("test")
win.show()
sys.exit(app.exec_())
Please upvote if you found this useful or drop-in the queries below.