![](/img/trans.png)
[英]PyQT5 - How to display in QLCD widget part of emited signal from serial?
[英]How to print the changing values of QLCD Widget in PyQt4?
我对PyQt4有点陌生,我一直在开发具有QDial和LCD Widget的简单GUI。我使用了SIGNAL和SLOT技术,即,当QDial更改时,其值将显示在LCD Widget上。 同样,我想在每次更改QDial时在终端上打印LCD值。我该怎么做?
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(610, 404)
MainWindow.setStatusTip(_fromUtf8(""))
MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.dial_1 = QtGui.QDial(self.centralWidget)
self.dial_1.setGeometry(QtCore.QRect(390, 10, 101, 91))
font = QtGui.QFont()
font.setStrikeOut(False)
self.dial_1.setFont(font)
self.dial_1.setMouseTracking(False)
self.dial_1.setFocusPolicy(QtCore.Qt.ClickFocus)
self.dial_1.setStatusTip(_fromUtf8(""))
self.dial_1.setWhatsThis(_fromUtf8(""))
self.dial_1.setAccessibleDescription(_fromUtf8(""))
self.dial_1.setAutoFillBackground(False)
self.dial_1.setSingleStep(1)
self.dial_1.setPageStep(10)
self.dial_1.setProperty("value", 0)
self.dial_1.setSliderPosition(0)
self.dial_1.setWrapping(False)
self.dial_1.setNotchTarget(3.7)
self.dial_1.setNotchesVisible(True)
self.dial_1.setObjectName(_fromUtf8("dial_1"))
self.lcdNumber_1 = QtGui.QLCDNumber(self.centralWidget)
self.lcdNumber_1.setGeometry(QtCore.QRect(500, 40, 81, 41))
self.lcdNumber_1.setFrameShape(QtGui.QFrame.Panel)
self.lcdNumber_1.setLineWidth(2)
self.lcdNumber_1.setDigitCount(4)
self.lcdNumber_1.setSegmentStyle(QtGui.QLCDNumber.Flat)
self.lcdNumber_1.setObjectName(_fromUtf8("lcdNumber_1"))
self.label_1 = QtGui.QLabel(self.centralWidget)
self.label_1.setGeometry(QtCore.QRect(390, 100, 111, 17))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
self.label_1.setFont(font)
self.label_1.setObjectName(_fromUtf8("label_1"))
self.dial_2 = QtGui.QDial(self.centralWidget)
self.dial_2.setGeometry(QtCore.QRect(390, 120, 101, 91))
self.dial_2.setFocusPolicy(QtCore.Qt.ClickFocus)
self.dial_2.setNotchesVisible(True)
self.dial_2.setObjectName(_fromUtf8("dial_2"))
self.label_2 = QtGui.QLabel(self.centralWidget)
self.label_2.setGeometry(QtCore.QRect(390, 210, 111, 17))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lcdNumber_2 = QtGui.QLCDNumber(self.centralWidget)
self.lcdNumber_2.setEnabled(True)
self.lcdNumber_2.setGeometry(QtCore.QRect(500, 150, 81, 41))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.lcdNumber_2.setFont(font)
self.lcdNumber_2.setFrameShape(QtGui.QFrame.Panel)
self.lcdNumber_2.setFrameShadow(QtGui.QFrame.Raised)
self.lcdNumber_2.setLineWidth(2)
self.lcdNumber_2.setMidLineWidth(0)
self.lcdNumber_2.setDigitCount(4)
self.lcdNumber_2.setMode(QtGui.QLCDNumber.Dec)
self.lcdNumber_2.setSegmentStyle(QtGui.QLCDNumber.Flat)
self.lcdNumber_2.setObjectName(_fromUtf8("lcdNumber_2"))
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 611, 25))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.dial_1, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.lcdNumber_1.display)
QtCore.QObject.connect(self.dial_2, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.lcdNumber_2.display)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label_1.setText(_translate("MainWindow", "Linear Velocity", None))
self.label_2.setText(_translate("MainWindow", "Angular Velocity", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
您可以连接自己的获取值的函数并使用print()
进行显示
QtCore.QObject.connect(self.dial_1, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.lcdNumber_1.display)
# connect own function
QtCore.QObject.connect(self.dial_1, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.my_function)
def my_function(self, value):
print(value)
与第二个QDial
相同。
您甚至可以直接使用print
而无需创建自己的功能,因为print
将获得价值。
QtCore.QObject.connect(self.dial_1, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), print)
编辑:如@musicamante在评论中提到的-此版本
SIGNAL(..., print)
仅在Python 3中有效,因为在Python 2中无法print
。
否则您将不得不在Python 2中使用
from __future__ import print_function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.