(四)使用Pycharm对PyQt5在串口助手参数选项进行内部逻辑设计
1. 信息对话框和按钮对话框的具体函数:
图片来自b站up主: 物联网客栈
2. 绑定信号与槽
2-1. 对修改下拉选项所对应的对象名称进行修改:
2-2. main函数的代码主代码第一种样式效果:
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
QtWidgets.QMessageBox.information(self, "提示", content)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
显示效果:
2-3. main函数的代码主代码第一种样式效果:
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
显示效果:
2-4. main函数的代码主代码第二种样式效果(获取对象textEdit_get中文本的输入,并将该内容添加到对象combox_uart中)
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
在对象textEdit_get的输入框输入COMx(x=1,2,…),然后点击发送。串口端就出现效果,如下:
3. main函数的代码主代码第三种样式效果(获取菜单栏、子菜单的点击的操作信号,并绑定连接信号
函数说明:图片截取b站up主: 物联网客栈.
代码如下:
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 菜单栏的开始、暂停、停止、清除的按键操作
self.action_Start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_pause_cb)
self.action_Stop.triggered.connect(self.action_stop_cb)
self.action_Clean.triggered.connect(self.action_clean_cb)
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
# 菜单的开始按钮被点击
def action_start_cb(self):
print("you clicked action_start")
# 菜单的暂停按钮被点击
def action_pause_cb(self):
print("you clicked action_pause")
# 菜单的停止按钮被点击
def action_stop_cb(self):
print("you clicked action_stop")
# 菜单的清除按钮被点击
def action_clean_cb(self):
print("you clicked action_clean")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
效果如下:(依次点击“Start”、“Pause”、“Stop”、“Clean”按钮),终端打印
4. 初始化(RadioButton)接收设置、发送设置的参数
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 菜单栏的开始、暂停、停止、清除的按键操作
self.action_Start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_pause_cb)
self.action_Stop.triggered.connect(self.action_stop_cb)
self.action_Clean.triggered.connect(self.action_clean_cb)
# 初始化串口助手(ASCII或者Hex)的选择
self.radioButton_recv_ascii.setChecked(True)
self.radioButton_send_ascii.setChecked(True)
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
# 菜单的开始按钮被点击
def action_start_cb(self):
print("you clicked action_start")
# 菜单的暂停按钮被点击
def action_pause_cb(self):
print("you clicked action_pause")
# 菜单的停止按钮被点击
def action_stop_cb(self):
print("you clicked action_stop")
# 菜单的清除按钮被点击
def action_clean_cb(self):
print("you clicked action_clean")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
效果如下:
然后选择并检测不同的接收设置、发送设置:
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 菜单栏的开始、暂停、停止、清除的按键操作
self.action_Start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_pause_cb)
self.action_Stop.triggered.connect(self.action_stop_cb)
self.action_Clean.triggered.connect(self.action_clean_cb)
#
self.radioButton_send_ascii.clicked.connect(self.radioButton_send_ascii_cb)
self.radioButton_send_hex.clicked.connect(self.radioButton_send_hex_cb)
self.radioButton_recv_ascii.clicked.connect(self.radioButton_recv_ascii_cb)
self.radioButton_recv_hex.clicked.connect(self.radioButton_recv_hex_cb)
# 初始化串口助手(ASCII或者Hex)的选择
self.radioButton_recv_ascii.setChecked(True)
self.radioButton_send_ascii.setChecked(True)
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
# 菜单的开始按钮被点击所触发事件
def action_start_cb(self):
print("you clicked action_start")
# 菜单的暂停按钮被点击所触发事件
def action_pause_cb(self):
print("you clicked action_pause")
# 菜单的停止按钮被点击所触发事件
def action_stop_cb(self):
print("you clicked action_stop")
# 菜单的清除按钮被点击所触发事件
def action_clean_cb(self):
print("you clicked action_clean")
# 单选按钮被勾选所触发的事件
def radioButton_send_ascii_cb(self):
print("you selected radioButton_send_ascii")
def radioButton_send_hex_cb(self):
print("you selected radioButton_send_hex")
def radioButton_recv_ascii_cb(self):
print("you selected radioButton_recv_ascii")
def radioButton_recv_hex_cb(self):
print("you selected radioButton_recv_hex")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
效果
5. Check_Box
5-1. 在上一个工程文件的基础上,通过Qt Designer更改控件的对象名称
逻辑代码:
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽######################################################################
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 菜单栏的开始、暂停、停止、清除的按键操作
self.action_Start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_pause_cb)
self.action_Stop.triggered.connect(self.action_stop_cb)
self.action_Clean.triggered.connect(self.action_clean_cb)
# 接收设置、发送设置(radio_Buttom)单选点击事件绑定触发
self.radioButton_send_ascii.clicked.connect(self.radioButton_send_ascii_cb)
self.radioButton_send_hex.clicked.connect(self.radioButton_send_hex_cb)
self.radioButton_recv_ascii.clicked.connect(self.radioButton_recv_ascii_cb)
self.radioButton_recv_hex.clicked.connect(self.radioButton_recv_hex_cb)
# 初始化串口助手(ASCII或者Hex)的选择(radio_Buttom)
self.radioButton_recv_ascii.setChecked(True)
self.radioButton_send_ascii.setChecked(True)
#
self.checkBox_auto_line.toggled.connect(self.checkBox_auto_line_cb)
self.checkBox_show_send.toggled.connect(self.checkBox_show_send_cb)
self.checkBox_show_time.toggled.connect(self.checkBox_show_time_cb)
self.checkBox_resend.toggled.connect(self.checkBox_resend_cb)
# 信号对应的事件触发 #########################################################################
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
# 菜单的开始按钮被点击所触发事件(Start、Pause、Stop、Clean)
def action_start_cb(self):
print("you clicked action_start")
# 菜单的暂停按钮被点击所触发事件
def action_pause_cb(self):
print("you clicked action_pause")
# 菜单的停止按钮被点击所触发事件
def action_stop_cb(self):
print("you clicked action_stop")
# 菜单的清除按钮被点击所触发事件
def action_clean_cb(self):
print("you clicked action_clean")
# 单选按钮被勾选所触发的事件(ASCII发送、Hex发送、ASCII接收、Hex接收)
def radioButton_send_ascii_cb(self):
print("you selected radioButton_send_ascii")
def radioButton_send_hex_cb(self):
print("you selected radioButton_send_hex")
def radioButton_recv_ascii_cb(self):
print("you selected radioButton_recv_ascii")
def radioButton_recv_hex_cb(self):
print("you selected radioButton_recv_hex")
# checkBox事件触发(自动化换行、显示发送、显示时间、重复发送)
def checkBox_auto_line_cb(self):
print("you select checkBox_auto_line")
def checkBox_show_send_cb(self):
print("you select checkBox_show_send")
def checkBox_show_time_cb(self):
print("you select checkBox_show_time")
def checkBox_resend_cb(self):
print("you select checkBox_resend")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
运行,勾选对应的Check_Box的效果:
5-2. checkBox事件触发(自动化换行、显示发送、显示时间、重复发送)判断是否被选中
import sys
from PyQt5 import QtWidgets
import USART_select_03
# 构建一个类usartMainWindow
class usartMainWindow(QtWidgets.QMainWindow,USART_select_03.Ui_MainWindow):
def __init__(self): # 初始化
super().__init__()
self.setupUi(self)
# 绑定信号与槽######################################################################
# 当comboBox_baud(波特率当前值)改变,绑定连接到comboBox_baud_cb事件
self.comboBox_baud.currentIndexChanged.connect(self.comboBox_baud_cb)
# 当btn_send(按钮被点击),绑定连接btn_send_cd事件
self.btn_send.clicked.connect(self.btn_send_cb)
# 菜单栏的开始、暂停、停止、清除的按键操作
self.action_Start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_pause_cb)
self.action_Stop.triggered.connect(self.action_stop_cb)
self.action_Clean.triggered.connect(self.action_clean_cb)
# 接收设置、发送设置(radio_Buttom)单选点击事件绑定触发
self.radioButton_send_ascii.clicked.connect(self.radioButton_send_ascii_cb)
self.radioButton_send_hex.clicked.connect(self.radioButton_send_hex_cb)
self.radioButton_recv_ascii.clicked.connect(self.radioButton_recv_ascii_cb)
self.radioButton_recv_hex.clicked.connect(self.radioButton_recv_hex_cb)
# 初始化串口助手(ASCII或者Hex)的选择(radio_Buttom)
self.radioButton_recv_ascii.setChecked(True)
self.radioButton_send_ascii.setChecked(True)
#
self.checkBox_auto_line.toggled.connect(self.checkBox_auto_line_cb)
self.checkBox_show_send.toggled.connect(self.checkBox_show_send_cb)
self.checkBox_show_time.toggled.connect(self.checkBox_show_time_cb)
self.checkBox_resend.toggled.connect(self.checkBox_resend_cb)
# 信号对应的事件触发 #########################################################################
# 波特率事件
def comboBox_baud_cb(self):
content = self.comboBox_baud.currentText()
print("combox's value is ", content)
text = ("你当前选中了%s" %content)
QtWidgets.QMessageBox.information(self, "提示", text, QtWidgets.QMessageBox.Ok |QtWidgets.QMessageBox.Cancel)
# 按钮点击发送事件
def btn_send_cb(self):
print("you clicked btn_send.")
text = self.textEdit_get.toPlainText() # 获取对象 textEdit_get 中的文本
print("text is ", text)
# 将对象textEdit_get框中文本,加载到对象comboBox_uart中
self.comboBox_uart.addItem(text)
# 菜单的开始按钮被点击所触发事件(Start、Pause、Stop、Clean)
def action_start_cb(self):
print("you clicked action_start")
# 菜单的暂停按钮被点击所触发事件
def action_pause_cb(self):
print("you clicked action_pause")
# 菜单的停止按钮被点击所触发事件
def action_stop_cb(self):
print("you clicked action_stop")
# 菜单的清除按钮被点击所触发事件
def action_clean_cb(self):
print("you clicked action_clean")
# 单选按钮被勾选所触发的事件(ASCII发送、Hex发送、ASCII接收、Hex接收)
def radioButton_send_ascii_cb(self):
print("you selected radioButton_send_ascii")
def radioButton_send_hex_cb(self):
print("you selected radioButton_send_hex")
def radioButton_recv_ascii_cb(self):
print("you selected radioButton_recv_ascii")
def radioButton_recv_hex_cb(self):
print("you selected radioButton_recv_hex")
# checkBox事件触发(自动化换行、显示发送、显示时间、重复发送)判断是否被选中
def checkBox_auto_line_cb(self):
print("you select checkBox_auto_line")
res_auto_line = self.checkBox_auto_line.isChecked()
print("res_auto_line is ", res_auto_line)
res_show_send = self.checkBox_show_send.isChecked()
print("res_show_send ", res_show_send)
res_show_time = self.checkBox_show_time.isChecked()
print("res_show_timw ", res_show_time)
res_repeat_send = self.checkBox_resend.isChecked()
print("rec_resend is ", res_repeat_send)
def checkBox_show_send_cb(self):
print("you select checkBox_show_send")
print("you select checkBox_auto_line")
res_auto_line = self.checkBox_auto_line.isChecked()
print("res_auto_line is ", res_auto_line)
res_show_send = self.checkBox_show_send.isChecked()
print("res_show_send ", res_show_send)
res_show_time = self.checkBox_show_time.isChecked()
print("res_show_timw ", res_show_time)
res_repeat_send = self.checkBox_resend.isChecked()
print("rec_resend is ", res_repeat_send)
def checkBox_show_time_cb(self):
print("you select checkBox_show_time")
print("you select checkBox_auto_line")
res_auto_line = self.checkBox_auto_line.isChecked()
print("res_auto_line is ", res_auto_line)
res_show_send = self.checkBox_show_send.isChecked()
print("res_show_send ", res_show_send)
res_show_time = self.checkBox_show_time.isChecked()
print("res_show_timw ", res_show_time)
res_repeat_send = self.checkBox_resend.isChecked()
print("rec_resend is ", res_repeat_send)
def checkBox_resend_cb(self):
print("you select checkBox_resend")
print("you select checkBox_auto_line")
res_auto_line = self.checkBox_auto_line.isChecked()
print("res_auto_line is ", res_auto_line)
res_show_send = self.checkBox_show_send.isChecked()
print("res_show_send ", res_show_send)
res_show_time = self.checkBox_show_time.isChecked()
print("res_show_timw ", res_show_time)
res_repeat_send = self.checkBox_resend.isChecked()
print("rec_resend is ", res_repeat_send)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = usartMainWindow()
MainWindow.show()
sys.exit(app.exec_())
显示效果
|