根据QRadioButton,当我点击QPushButton时如何修改被调用的函数?

时间:2018-06-13 09:23:53

标签: python qt pyqt

我有三个QRadioButtons。一次只能选择一个。 我有一个QPushButton,根据所选的QRadioButton,它必须调用不同的函数。

示例:如果选择了QRadioButton#1,则单击该按钮时会执行功能#1。如果选择QRadioButton#2,则相同按钮执行功能#2,依此类推。

3 个答案:

答案 0 :(得分:1)

做出选择。 试试吧:

import sys
from PyQt5.QtWidgets import (QLabel, QRadioButton, QPushButton, QVBoxLayout, QApplication, QWidget)


class Window(QWidget):

    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.lbl  = QLabel('Which do you like best?')
        self.code = QRadioButton('PyQt5')
        self.dog  = QRadioButton('Dogs')
        self.cat  = QRadioButton('Cats')
        self.btn  = QPushButton('Select')

        layout = QVBoxLayout()
        layout.addWidget(self.lbl)
        layout.addWidget(self.code)
        layout.addWidget(self.dog)
        layout.addWidget(self.cat)
        layout.addWidget(self.btn)

        self.setLayout(layout)
        self.setWindowTitle('PyQt5 QRadioButton')

        self.btn.clicked.connect(lambda: self.btn_clk(self.code.isChecked(), self.lbl))
        self.btn.clicked.connect(lambda: self.btn_clk(self.dog.isChecked(), self.lbl))
        self.btn.clicked.connect(lambda: self.btn_clk(self.cat.isChecked(), self.lbl))

        self.show()

    def btn_clk(self, chk, lbl):
        if self.code.isChecked():
            lbl.setText('It`s a good choice')
        elif self.dog.isChecked():
            lbl.setText('I guess you like dogs')
        else:
            lbl.setText('So its cats for you')

app = QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())

enter image description here

答案 1 :(得分:1)

我必须为我的代码稍微更改它,并且它有效。谢谢!

...
class Window(QWidget):
    def __init__(self):
    ...
    def init_ui(self):
        ...    
        self.bnt.clicked.connect(lambda: self.decision_maker())
        ...

    def decision_maker(self):
        if self.radiobutton1.isChecked():
            do_something_1()
        elif self.radiobutton2.isChecked():
            do_something_2()
        ...

答案 2 :(得分:0)

试试这个:

from PyQt5 import QtGui,QtCore,QtWidgets
import sys
class Win(QtWidgets.QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()
   def initUI(self):
      self.vbox = QtWidgets.QVBoxLayout(self)
      self.rb1 = QtWidgets.QRadioButton('1',self)
      self.rb2 = QtWidgets.QRadioButton('2',self)
      self.rb3 = QtWidgets.QRadioButton('3',self)
      self.btn = QtWidgets.QPushButton('PushMe',self)
      self.btn.clicked.connect(self.func)
      self.vbox.addWidget(self.rb1)
      self.vbox.addWidget(self.rb2)
      self.vbox.addWidget(self.rb3)
      self.vbox.addWidget(self.btn)
      self.show()

   def func(self):
      for i in self.findChildren(QtWidgets.QRadioButton):
         if i.isChecked():
            self.doAction(str(i.text()))

   def doAction(self,text):
      if text == '1':
         print('You like to be the first')
      if text == '2':
         print('You like to be the second')
      if text == '3':
         print('You like to be the third')
if __name__ == '__main__':
   app = QtWidgets.QApplication(sys.argv)
   gui = Win()
   sys.exit(app.exec_())

enter image description here