我正在使用PySide2、QtQuick和QtCreator。 我试图使它使用QML文件中存储的文本字段中的变量,并运行命令,使用print()将变量打印到控制台中
在main.py文件中,我创建了以下代码,用于查找加密货币资产的价格
def assetSet(self):
asset = "xrp"
url2 = f'https://coinmarketcap.com/currencies/{asset}'
page2 = requests.get(url2)
soup2 = bs(page2.text, 'html.parser')
price2 = soup2.find(class_="priceValue___11gHJ")
price12 = str(price2)
result2 = re.search('<div class="priceValue___11gHJ">(.*)</div>', price12)
result12 = result2.group(1)
self.setAsset.emit(f"The price of {asset} is {result12}")
在QML文件中,我有
CustomButton{
id: btnBdo
text: "Check"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenterOffset: 200
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenterOffset: 0
font.pointSize: 20
colorMouseOver: "#21252d"
colorPressed: "#191d23"
colorDefault: "#2a303a"
onClicked: {
backend.assetSet()
}
}
CustomTextField {
id: textField
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenterOffset: -100
anchors.horizontalCenter: parent.horizontalCenter
font.pointSize: 20
placeholderText: qsTr("Enter a name")
objectName: "assetField"
}
我希望资产变量等于存储在QML文件中的文本字段,并在单击按钮时运行此命令。 有办法做到这一点吗
在这种情况下,最好使用接收数据的插槽,由于功能的执行也是同步的,因此不需要信号:
另一方面,请求会阻止Qt事件循环,导致GUI冻结,因此最好在另一个线程中执行该任务:
相关问题 更多 >
编程相关推荐