gpt4 book ai didi

python - 如何将函数的返回值放入 QLineEdit 中?

转载 作者:行者123 更新时间:2023-12-01 04:18:03 26 4
gpt4 key购买 nike

我应该在 a 的 text1text2 框中添加函数 result1result2 的返回Qt 应用程序,当我单击相关按钮时(请注意我使用的所有变量都是全局变量):

def result1(a, b):
z = result2(a, c)
return (a*b)/float(z)
def result2(a, c):
return a/float(c)

button1.clicked.connect(setting)
button2.clicked.connect(setting)
def setting():
if c != 0:
text2.setText(result2(a, c))
text1.setText(result1(a, b))
return

如果不关注函数 result1result2 (我没有编写它们),我们知道返回值都是 float (我尝试打印它们)。

当我设置框的文本时,它向我显示以下错误:

QLineEdit.setText(QString): argument 1 has unexpected type 'NoneType'

所以我尝试使用方法toString,如下所示:

def setting():
if c != 0:
text2.setText(result2(a, c).toString())
text1.setText(result1(a, b).toString())
return

但是,现在还有另一个错误:

'NoneType' object has no attribute 'toString'

为什么函数的返回值是NoneType

最佳答案

只要您确定该函数正在返回 float (听起来就像您不尝试将其设置为 QLineEdit 中的文本一样),您应该能够执行以下操作:

def setting():
if c != 0:
text2.setText(str(result2(a, c)))
text1.setText(str(result1(a, b)))
return

当然,正如你所说,这要求text2和text1是全局的。

我取得了一些成功的另一个选项是在首次创建 QLineEdit 时使用全局变量作为值:

global Result_T2
global Result_T1
Result_T2 = ""
Result_T1 = ""
text2.setText(Result_T2)
text1.setText(Result_T1)
.
.
.
def setting():
if c != 0:
Result_T2 = str(result2(a, c))
Result_T1 = str(result1(a, b))
return

关于python - 如何将函数的返回值放入 QLineEdit 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34089935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com