![](/img/trans.png)
[英]Python Pyqt: could not display data in lineedit and print command in terminal window
[英]Python PyQt: I could not display the data in lineedit from external program
我正在嘗試在lineedit中顯示來自EPICS(數據包)的運行時數據。 我可以通過程序接收和發送數據,但是當我無法顯示數據時。 函數pv.get()將數據從EPICS提供給Python。 請建議我進行更改,因為connect中的SIGNAL函數給出錯誤
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import epics
from epics import *
class MyFrame(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
self.lineedit = QLineEdit(self)
self.lineedit.setGeometry(QRect(250,450,75,28))
pv=epics.PV('calc:sum.VAL')
self.lineedit=pv.get()
self.connect(self.lineedit, SIGNAL("textChanged()"), self.changedata)
self.color = QColor(Qt.blue)
self.show()
def changedata (self):
pv=epics.PV('calc:sum.VAL')
self.lineedit=pv.get()
text=pv.get()
self.update()
app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()
`
推薦給@ivica。 我看到這是11個小時沒有答案,所以我想我會有所作為
您將用pv.get()
返回的任何數據替換QLineEdit
小部件。 您實際上想要將數據設置到小部件上。
class MyFrame(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
self.lineedit = QLineEdit(self)
self.lineedit.setGeometry(QRect(250,450,75,28))
pv=epics.PV('calc:sum.VAL')
self.lineedit.setText(pv.get())
# new-style signal slot connections
self.lineedit.textChanged.connect(self.changedata)
self.color = QColor(Qt.blue)
def changedata (self):
pv=epics.PV('calc:sum.VAL')
self.lineedit.setText(pv.get())
更新資料
更加仔細地檢查您的代碼示例,我看到了更多的潛在問題。
盡管我正在使用新樣式的信號連接,但是在您的舊樣式中,您缺少正確的簽名:
self.connect(self.lineedit, SIGNAL("textChanged()"), self.changedata)
# should be
self.connect(self.lineedit, SIGNAL("textChanged(QString)"), self.changedata)
話雖這么說...我不明白您為什么要將文本字段的更改連接到插槽,然后再更改文本字段。 它將導致幾個遞歸觸發器。 如果您要執行的操作是捕獲文本的編輯,而是調用epics lib並從中設置文本,那么您應該使用textEdited
信號,當您以編程方式更改該字段時,該信號將不會發出。
# new style
self.lineedit. textEdited.connect(self.changedata)
# old style
self.connect(self.lineedit, SIGNAL("textEdited(QString)"), self.changedata)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.