【问题标题】:change text of lineEdit when a radio button is selected in pyqt在pyqt中选择单选按钮时更改lineEdit的文本
【发布时间】:2013-02-11 03:55:45
【问题描述】:

我有两个radioButtons 使用qt 设计器制作的表格,我现在正在用pyqt 编程。 i wish to change the text of lineEdit to "radio 1" when radioButton 1 is selected and "radio 2" when the radioButton 2 is selected.我怎样才能做到这一点?

【问题讨论】:

    标签: python user-interface button pyqt4 radio


    【解决方案1】:

    这是一个简单的例子。每个QRadioButton 都连接到它自己的功能。您可以将它们都连接到同一个函数并管理通过它发生的事情,但我认为最好演示信号和插槽的工作原理。

    有关更多信息,请查看 PyQt4 documentation for new style signals and slots。如果将多个信号连接到同一个插槽,有时使用QObject.sender() 方法很有用,尽管在QRadioButton 的情况下,只需检查所需按钮的.isChecked() 方法可能更容易。

    import sys
    from PyQt4.QtGui import QApplication, QWidget, QVBoxLayout, \
        QLineEdit, QRadioButton
    
    class Widget(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
    
            self.widget_layout = QVBoxLayout()
    
            self.radio1 = QRadioButton('Radio 1')
            self.radio2 = QRadioButton('Radio 2')
            self.line_edit = QLineEdit()
    
            self.radio1.toggled.connect(self.radio1_clicked)
            self.radio2.toggled.connect(self.radio2_clicked)
    
            self.widget_layout.addWidget(self.radio1)
            self.widget_layout.addWidget(self.radio2)
            self.widget_layout.addWidget(self.line_edit)
            self.setLayout(self.widget_layout)
    
        def radio1_clicked(self, enabled):
            if enabled:
                self.line_edit.setText('Radio 1')
    
        def radio2_clicked(self, enabled):
            if enabled:
                self.line_edit.setText('Radio 2')
    
    
    if __name__ == '__main__':
      app = QApplication(sys.argv)
      widget = Widget()
      widget.show()
    
      sys.exit(app.exec_())  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 2013-07-20
      • 2011-08-29
      相关资源
      最近更新 更多