Issue
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.
Solution
You simply forgot to use its text
accessor function :
if self.label.text() == "Whats up?":
(notice that it is a function, hence the parentheses after text).
[edit] To answer your second question:
def initUI(self):
#...
self.label2 = QLabel(self)
self.label2.setText("Guido")
self.label2.move(0, 120)
self.label2.setVisible(False)
def clicked(self):
self.label.setText(random.choice(lis))
if self.label.text() == "Whats up?":
print("True")
else:
print("False")
self.label2.setVisible( self.label.text() == "Whats up?" )
Answered By - Demi-Lune
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.