gpt4 book ai didi

python - PyQt lineEdit on textEdited 信号值在函数调用后丢失

转载 作者:行者123 更新时间:2023-12-01 09:21:35 26 4
gpt4 key购买 nike

#^^^^class stuff here for setting UI^^^^


# connecting combobox to slot/function get_baffle_number
self.baffle_number_combobox.currentIndexChanged.connect(get_baffle_number)

# connecting PushButton action "clicked" to function on_click
self.pushButton.clicked.connect(on_click)


#connecting lineEdit to slot/function get_baffle_cost
self.baffle_cost_lineEdit.textEdited.connect(get_baffle_cost)


@pyqtSlot()
def get_baffle_cost(text):
baffle_cost = text
return baffle_cost


def get_baffle_number(text):
#add 1 to the index returned by comboBox to get the number desired by user
baffle_number = text + 1
return baffle_number


def calc_baffle_cost():
test_total = (get_baffle_cost() * get_baffle_number())
return test_total


@pyqtSlot()
def on_click(self):
baffle_cost = calc_baffle_cost()
print(baffle_cost)

连接后lineEdit通过 pyqtSlot() 到该函数,它似乎获得了该值,但如果我尝试使用 baffle_cost ,它会立即转储它来自另一个函数。我在调试期间在 PyCharm 中观察它,它的保存时间与 lineEdit 一样长。看起来有重点。按pushButton当它失去值(value)时才是正确的。

我无法使用返回的baffle_costget_baffle_cost开始.

我错过了什么吗?我最远的是尝试简单地打印 calc_baffle_cost()并打印十六进制。我假设这是一个内存位置,但不能确定。 (Python 新手)

很抱歉,如果信息还不够。我只是想获取 baffle_cost来自lineEdit并将其乘以取自 comboBox 的值.

最佳答案

看来我的问题有两个,或者更多。

我没有正确确定我的函数范围,也没有使用正确的命名空间。 (抱歉,如果这个术语不正确。我是 Python 和 PyQt 的新手。)

class Ui_MainWindow(object):
def setupUi(self, MainWindow):

# sizing of widgets

def retranslateUi(self, MainWindow):

# UI stuff here QlineEdits, etc.
self.baffle_cost_lineEdit.editingFinished.connect(self.get_baffle_cost)

def get_baffle_cost(self):
baffle_cost = self.baffle_cost_lineEdit.text()
return baffle_cost

这需要与上面的 Ui_MainWindow 类中的 retranslateUi() 函数处于相同的范围(缩进)。

我认为如果我能更好地构建我的项目,这就不会成为问题。我确实吸取了关于将所有内容都集中在一个脚本中的教训。 (程序.py)

我遇到的另一个问题是从 QlineEdit 调用的 PyQt 函数 text() 返回一个 QString 而不是str。我只需要将其转换为字符串即可。

我丢失了该值,因为函数 get_baffle_cost 未使用 baffle_cost_lineEdit 正确确定作用域

关于python - PyQt lineEdit on textEdited 信号值在函数调用后丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763897/

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