python - 如何在 Python 中比较 PyQt5 中的字符串?[解决了]
问题描述
我想制作一个 GUI 应用程序PyQt5
,如果你点击一个按钮,那么标签中会出现一些随机文本。如果该随机文本等于Whats up?然后它将True
在终端中输入。否则,它将打印False
.
然而我False
每次都...
这是我的代码:
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
任何帮助,将不胜感激...
请告诉我该怎么做。我是 GUI 新手,所以不太了解。
解决方案
您只是忘记使用它的text
访问器功能:
if self.label.text() == "Whats up?":
(请注意,它是一个函数,因此文本后面有括号)。
[编辑]回答你的第二个问题:
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?" )
推荐阅读
- python - 将 json 转换为 csv 以获得相似的键
- css - 为什么一个 4mb 的透明 PNG 需要 10-20 秒才能加载为重复的 CSS 背景纹理?
- angular - Angular Universal“找不到模块文件(/src/app/.ts)”
- python - 解析以引号开头的tsv文件
- sql - 比较同一个表的两列中的字符串
- jquery - 通用预输入字段和初始化程序
- compression - 存储/压缩黑白PNG的最优化方式是什么
- swift - 如何使用 Swift 读取 AXRoleDescription 和可访问性层次结构?
- postgresql - PostgreSQL中的&&是什么意思?
- android - 我想在列表中以 recyclerview 的形式获取所有订单的价格