You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt5开发SRS DG645控制界面时如何将QLineEdit值传入按钮触发函数

解决方法

你需要给Write Values按钮绑定一个自定义的槽函数,在槽函数内读取两个输入框的内容、做类型转换后再调用dg.set_delay(),不要直接把按钮点击事件绑定到dg.set_delay上。你之前查阅的PyQt按钮传参教程,适用的是参数预先固定的场景,而你需要的参数是用户实时输入的动态值,所以需要自定义槽函数读取控件内容后再调用目标方法,这就是之前的方法不能解决你问题的原因。

具体修改步骤:

  1. 在MainWindow类中添加自定义槽函数,用于处理点击按钮后的逻辑,包含输入校验、类型转换、调用设备方法、异常提示。
  2. 修改按钮的点击事件绑定,指向你写的自定义槽函数,同时要记得把按钮加到布局中,否则按钮不会显示。

修改后的MainWindow类代码:

class MainWindow(qtw.QWidget) :

    def __init__(self) :
        """MainWindow constructor"""
        super().__init__()
        
        ##########################
        # Main UI code goes here #
        ##########################
   
        layout = qtw.QVBoxLayout()
        self.setLayout(layout)        
   
        self.quitbutton = qtw.QPushButton('Quit',clicked=self.close)
        self.initializebutton = qtw.QPushButton('initialize', clicked=dg.initialize)
        self.closebutton = qtw.QPushButton('Close', clicked=dg.close)
        self.layout().addWidget(self.quitbutton)
        self.layout().addWidget(self.initializebutton)
        self.layout().addWidget(self.closebutton)
        
        self.label1 = qtw.QLabel('Channel')
        self.entry1 = qtw.QLineEdit()
        self.label2 = qtw.QLabel('Timing')
        self.entry2 = qtw.QLineEdit()
        # 可选优化:给延迟输入框加浮点数校验,限制只能输入数字
        self.entry2.setValidator(qtg.QDoubleValidator())
        self.layout().addWidget(self.label1)
        self.layout().addWidget(self.entry1)
        self.layout().addWidget(self.label2)
        self.layout().addWidget(self.entry2)
        
        # 修改绑定到自定义槽函数
        self.wtrigbutton = qtw.QPushButton('Write Values', clicked=self.on_write_values_clicked)
        self.layout().addWidget(self.wtrigbutton) # 补上把按钮加入布局的代码
        
        ##########################
        # End of UI code Section #
        ##########################
        self.show()

    def on_write_values_clicked(self):
        # 读取输入框内容并去除首尾空白
        channel_input = self.entry1.text().strip()
        timing_input = self.entry2.text().strip()
        
        # 空输入校验
        if not channel_input or not timing_input:
            qtw.QMessageBox.warning(self, "输入错误", "通道号和延迟时间都不能为空")
            return
        
        # 类型转换
        try:
            # 通道支持输入数字或者字符串(比如A、B、T0等),set_delay方法会自动处理格式,不需要强制转int
            timing = float(timing_input)
        except ValueError:
            qtw.QMessageBox.warning(self, "格式错误", "延迟时间请输入合法的数字,通道号可输入数字或通道名(如A、T0等)")
            return
        
        # 调用设备设置方法
        try:
            dg.set_delay(channel_input, timing)
            qtw.QMessageBox.information(self, "设置成功", f"通道{channel_input}的延迟已设置为{timing}秒")
        except Exception as e:
            qtw.QMessageBox.critical(self, "设置失败", f"设备操作出错:{str(e)}")

额外优化建议

如果想完全避免通道输入错误,可以把通道输入框改成下拉选择框,直接列出所有合法的通道名供用户选择即可。

内容的提问来源于stack exchange,提问作者Strelok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 12:45:02