vendredi 27 décembre 2019

How to compare strings in PyQt5 in Python?

I want to make a GUI app usin PyQt5, where if you click a button then some random text will appear in the label. If that random text will be equal to Whats up? then it will type True in the terminal. Else, it will print False.

However I am getting False every time...

Here is my code:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
import sys
import random

lis = ["Hello world", "Hey man", "Yo buddy", "Go to hell", "Whats up?"]

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.initUI()
        self.setGeometry(200,200,300,300) 
        self.setWindowTitle("WhatsApp Sheduled")

    def initUI(self):
        self.label = QLabel(self)
        self.label.setText("My first label")
        self.label.move(120, 120)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("Click here")
        self.b1.clicked.connect(self.clicked)

    def clicked(self):
        self.label.setText(random.choice(lis))
        if self.label == "Whats up?":
            print("True")
        else:
            print("False")

def clicked():
    print("Clicked!")

def main():
    app = QApplication(sys.argv)
    win = MyWindow()

    win.show()
    sys.exit(app.exec_())

main()  # It's important to call this function

Any help would be appreciated...

Please just tell me how to do that. I am new to GUI, so didn't know much.

Aucun commentaire:

Enregistrer un commentaire