1

我对 PyQt 很陌生,所以我什至不确定从哪里开始搜索它。

所以我有两个不同的 QRadioButtons 选项,理想情况下它们对应于两个 QPushButtons,每个一个。

基本上,我有以下代码,我试图通过使用 if 语句来实现这一点:

def tab1UI(self): 

    mytabfont = QFont('Lucida Sans Unicode', 9)    
    layout = QFormLayout()
    #self.setTabText(0,"My Data")
    self.tab1.setLayout(layout)

    tabdescription = 'To obtain or generate data choose an option below:' 
    # radio options



    label1 = QLabel(tabdescription)
    label1.setFont(mytabfont)
    layout.addWidget(label1)

    radiobtn1 = QRadioButton('Load data from file')
    radiobtn1.setChecked(True)

    #why does my resize not work?
    radiobtn1.resize(100,100)

    radiobtn1.setFont(mytabfont)
    layout.addWidget(radiobtn1)
    loadbtn = QPushButton('Open CSV file...')
    layout.addWidget(loadbtn)

    radiobtn2 = QRadioButton('Generate data')
    radiobtn2.setFont(mytabfont)
    genbtn= QPushButton('Generating matrix...')
    layout.addWidget(radiobtn2)
    layout.addWidget(genbtn)



    if radiobtn1.isChecked():

        # if this option is clicked then this button needs to be activated else it must be de-activated
        loadbtn.setEnabled(True)
        genbtn.setEnabled(False)

    elif radiobtn2.isChecked():

        loadbtn.setEnabled(False)
        genbtn.setEnabled(True)

    else: 

        loadbtn.setEnabled(False)
        genbtn.setEnabled(False)

因此,每当我单击一个单选按钮选项时,我希望一个按钮在选中另一个选项时自动变为活动或不活动。必须有某种动作要连接,但不知道如何去做。

4

1 回答 1

2

You're only running the if statement once, when the buttons are first created. In order for this to work, you need to evaluate those if statements every time the radio button check state is changed. Qt allows you to do this with Signals and Slots. The QRadioButton will emit a signal when you change the check state. You can connect to this signal and run a function that updates the enabled state of the other buttons.

def tab1UI(self): 
    mytabfont = QFont('Lucida Sans Unicode', 9)    
    layout = QFormLayout()
    self.tab1.setLayout(layout)

    tabdescription = 'To obtain or generate data choose an option below:' 
    # radio options
    self.label1 = QLabel(tabdescription)
    self.label1.setFont(mytabfont)
    layout.addWidget(self.label1)

    self.radiobtn1 = QRadioButton('Load data from file')
    self.radiobtn1.setChecked(True)

    self.radiobtn1.setFont(mytabfont)
    layout.addWidget(self.radiobtn1)
    self.loadbtn = QPushButton('Open CSV file...')
    layout.addWidget(self.loadbtn)

    self.radiobtn2 = QRadioButton('Generate data')
    self.radiobtn2.setFont(mytabfont)
    self.genbtn= QPushButton('Generating matrix...')
    layout.addWidget(self.radiobtn2)
    layout.addWidget(self.genbtn)

    self.radiobtn1.toggled.connect(self.refresh_button_state)
    self.radiobtn2.toggled.connect(self.refresh_button_state)

    self.refresh_button_state()


def refresh_button_state(self):
    if self.radiobtn1.isChecked():
        self.loadbtn.setEnabled(True)
        self.genbtn.setEnabled(False)
    elif self.radiobtn2.isChecked():
        self.loadbtn.setEnabled(False)
        self.genbtn.setEnabled(True)
    else: 
        self.loadbtn.setEnabled(False)
        self.genbtn.setEnabled(False)
于 2016-06-14T17:50:21.763 回答