![](/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.