Open In App

PyQt5 – Flames Calculator

Last Updated : 11 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can create a flames calculator using PyQt5. This flames calculator assesses and predicts the outcome of a relationship based on an algorithm of two given names.
FLAMES is a popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Sibling. This game does not accurately predict whether or not an individual is right for you, but it can be fun to play this with your friends.

Below is how the flames calculator will look like as follows: 

 

GUI implementation steps : 
1. Create a label that says enter player 1 name and set color and geometry to it 
2. Add QLineEdit widget in front of the first name label to get the first name 
3. Similarly create another label that says enter player 2 name and set color and geometry to it 
4. Add QLineEdit widget in front of this label to get the second name 
5. Create a label to show the result and set its border, geometry and change its font. 
6. Create a push button at the bottom which says get result.
Back end implementation steps : 
1. Add action to the push button 
2. Inside the push button action get both player names 
3. Remove the spacing in between the names 
4. Call the get result method that returns the result 
5. Inside the get result method call the remove letter method that removes the common characters with their respective common occurrences. 
6. Then get the count of characters that are left and take FLAMES letters as [“F”, “L”, “A”, “M”, “E”, “S”] 
7. Start removing letter using the count we got. The letter which last the process is the result, return the result 
8. Set the result to the label using setText method. 
 

Below is the implementation: 

Python3




# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 320, 400)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating label to tell user enter first name
        name1_label = QLabel("Enter Player 1 Name : ", self)
 
        # setting border and color to the label
        name1_label.setStyleSheet("border : 1px solid black ;
                                    background : lightgrey;")
 
        # setting geometry
        name1_label.setGeometry(10, 20, 140, 40)
 
        # creating label to tell user enter second name
        name2_label = QLabel("Enter Player 2 Name : ", self)
 
        # setting border and color to the label
        name2_label.setStyleSheet("border : 1px solid black ;
                                     background : lightgrey;")
 
        # setting geometry
        name2_label.setGeometry(10, 70, 140, 40)
 
        # creating a line edit to get the first name
        self.name1 = QLineEdit(self)
 
        # setting geometry
        self.name1.setGeometry(160, 20, 150, 40)
 
        # creating a line edit to get the second name
        self.name2 = QLineEdit(self)
 
        # setting geometry
        self.name2.setGeometry(160, 70, 150, 40)
 
        # creating a label to show result
        self.output = QLabel("Find Relationship Status", self)
 
        # setting geometry to the output label
        self.output.setGeometry(20, 160, 280, 60)
 
        # setting border and background color to it
        self.output.setStyleSheet("border : 2px solid black;
                                       background : white;")
 
        # setting alignment to output
        self.output.setAlignment(Qt.AlignCenter)
 
        # setting font to the output
        self.output.setFont(QFont('Times', 11))
 
        # creating push button to get result
        self.push = QPushButton("Get Result", self)
 
        # setting geometry tot he button
        self.push.setGeometry(80, 260, 140, 50)
 
        # adding action to the push button
        self.push.clicked.connect(self.do_action)
 
    # action called by the push button
    def do_action(self):
 
        # getting names
        name1 = self.name1.text()
        name2 = self.name2.text()
 
        # removing spacing form the name
        name1.replace(" ", "")
        name2.replace(" ", "")
 
 
        # function for removing common characters
        # with their respective occurrences
        def remove_match_char(list1, list2):
 
            for i in range(len(list1)):
                for j in range(len(list2)):
 
                    # if common character is found
                    # then remove that character
                    # and return list of concatenated
                    # list with True Flag
                    if list1[i] == list2[j]:
                        c = list1[i]
 
                        # remove character from the list
                        list1.remove(c)
                        list2.remove(c)
 
                        # concatenation of two list elements with *
                        # * is act as border mark here
                        list3 = list1 + ["*"] + list2
 
                        # return the concatenated list with True flag
                        return [list3, True]
 
                        # no common characters is found
            # return the concatenated list with False flag
            list3 = list1 + ["*"] + list2
            return [list3, False]
 
        # method to find the result
        def find_relation(p1_list, p2_list):
             
            # taking a flag as True initially
            proceed = True
 
            # keep calling remove_match_char function
            # until common characters is found or
            # keep looping until proceed flag is True
            while proceed:
                # function calling and store return value
                ret_list = remove_match_char(p1_list, p2_list)
 
                # take out concatenated list from return list
                con_list = ret_list[0]
 
                # take out flag value from return list
                proceed = ret_list[1]
 
                # find the index of "*" / border mark
                star_index = con_list.index("*")
 
                # list slicing perform
 
                # all characters before * store in p1_list
                p1_list = con_list[: star_index]
 
                # all characters after * store in p2_list
                p2_list = con_list[star_index + 1:]
 
                # count total remaining characters
            count = len(p1_list) + len(p2_list)
 
            # list of FLAMES acronym
            result = ["Friends", "Love", "Affection", "Marriage", "Enemy", "Siblings"]
 
            # keep looping until only one item
            # is not remaining in the result list
            while len(result) > 1:
 
                # store that index value from
                # where we have to perform slicing.
                split_index = (count % len(result) - 1)
 
                # this steps is done for performing
                # anticlock-wise circular fashion counting.
                if split_index >= 0:
 
                    # list slicing
                    right = result[split_index + 1:]
                    left = result[: split_index]
 
                    # list concatenation
                    result = right + left
 
                else:
                    result = result[: len(result) - 1]
 
            # print final result
            return result[0]
 
        # calling find relation method
        result = find_relation(list(name1), list(name2))
 
        # setting text to the output label
        self.output.setText("Relationship : " + result)
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


Output : 
 

Code Explanation:

  1. The code starts by initializing the window’s title.
  2. Next, the window’s geometry is set to (100, 100, 320, 400).
  3. Finally, the UiComponents() method is called.
  4. This method contains code that will show all of the widgets in the window.
  5. The first widget to be shown is a text field.
  6. The text field has been given a width and height of 50 pixels each.
  7. The text field will also have a default value of “Python”.
  8. Next, a button has been added to the window.
  9. The button has been given a width and height of 25 pixels each and its default state is “pressed”.
  10. Finally, two labels have been added to the window.
  11. One label has been given a width and height of 20 pixels each and its default state is “empty”.
  12. The other label has been given a width and height of 30 pixels each and its default state is “loading”.
  13. The code first imports the necessary libraries and sets up a QMainWindow class.
  14. Next, the code creates a Window object and sets its title.
  15. The Window object also sets its geometry, which is defined as (100, 100, 320, 400).
  16. Finally, the code calls the UiComponents() method on the Window object to show all of its widgets.
  17. The UiComponents() method is responsible for handling all of the user interface (UI) components in the application.
  18. This method will be discussed in more detail later on in this tutorial.
  19. The code starts by creating two labels, name1 and name2.
  20. The first label, name1_label, will be used to display the player’s first name.
  21. The second label, name2_label, will be used to display the player’s second name.
  22. Next, the code creates two line edits: one for inputting the player’s first name and another for inputting the player’s second name.
  23. Both line edits have their own geometry (width and height) and are positioned at different coordinates (160 pixels from the top-left corner of the window and 70 pixels from the bottom-right corner).
  24. Finally, a label is created to show results of user input:name3_label.
  25. The code creates two QLabel objects, name1_label and name2_label.
  26. The first label will display the player’s first name, while the second label will display the player’s last name.
  27. The next line of code creates a QLineEdit object for each label.
  28. The line edit objects have their geometry set to 160 x 20 pixels, with a width of 150 pixels and a height of 40 pixels.
  29. Finally, the last line of code creates a label to show the results of the code.
  30. This label has its geometry set to 140 x 40 pixels, with a border and background color of black and lightgrey, respectively.
  31. The code starts by creating a QLabel object named self.output and setting its geometry to be 20, 160, 280, 60.
  32. The label will have a border and background color of black and white, respectively.
  33. The label’s alignment will be set to Qt.AlignCenter.
  34. Finally, the font used for the output label will be Times New Roman 11pt.
  35. Next, a QPushButton object named self.push is created and its geometry set to 80, 260, 140, 50.
  36. A clicked event handler is attached to the push button which will call the do_action() function when it is clicked.
  37. In this function, two variables are initialized: name1 and name2.
  38. name1 will hold the text value of the first input field while name2 will hold the text value of the second input field (assuming they are both filled in).
  39. Now let’s take a look at do_action().
  40. This function starts by getting hold of references to both name1 and name2 using their respective text values as arguments.
  41. Next, an if statement is executed that tests whether either variable has been assigned a value yet (name1 has not been assigned yet since it was not initially populated), otherwise do_action()
  42. The code sets up a QLabel object called self.output and sets its geometry to be 20 x 160 pixels, with a border and background color of black and white respectively.
  43. It then sets the style sheet for the label to have the following: border : 2px solid black; background : white;
  44. The code first removes any spaces from the first name and then from the second name.
  45. Next, it creates a function to remove common characters.
  46. The code loops through each character in list1 and list2, checking to see if that character is found in both lists.
  47. If it is, then the character is removed from list1 and list2 and the two lists are concatenated together with an asterisk (*).
  48. Finally, if no common characters are found, then the two lists are concatenated together with a comma (,) instead.
  49. The code can be summarized as follows: Remove any spaces from the names.
  50. Create a function to remove common characters.
  51. Loop through each character in both lists and check for matches.
  52. If a match is found, remove the character from both lists and concatenate them together with an asterisk (*).
  53. If no matches are found, concatenate them together with a comma (,)
  54. The code removes all the spaces and other common characters from two strings.
  55. It does this by iterating through each character in the first string and then doing the same for the second string.
  56. If a common character is found, it is removed from both lists and an intermediate list is created with True flag attached to it.
  57. If no common characters are found, then the intermediate list has False flag attached to it.
  58. The final result of the code is two lists with respective flags attached to them.
  59. The code starts by defining a function called find_relation.
  60. This is the function that will be used to find relationships between two lists of strings.
  61. The first list contains all the names of people in the world, and the second list contains all the names of countries in the world.
  62. The next line defines a flag variable proceed which is initially set to True.
  63. This means that we are currently looping through this function until it returns False (proceed).
  64. Next, we define a while loop which starts with “while proceed:”.
  65. Inside this loop, we call our remove_match_char method on each string from both lists and store its return value into an empty list called ret_list.
  66. We then take out concatenated list from ret_list[0] and store it as con_list into an empty list called result[0].
  67. We also take out flag value from ret_list[1] and store it as proceed into result[1].
  68. Finally, we use index(“*”) to get index number for star character (*) in con-list which stores all characters before * in p1-List and all characters after * in p2-List respectively.
  69. Then using len(p1-List) + len(
  70. The code is the implementation of a function which takes two lists and returns the relation between them.
  71. The function first checks if there are any common characters in both lists, then it keeps looping until there is no more common characters left.
     


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads