(Python)PyQtGui在单击按钮

时间:2015-08-27 09:50:35

标签: python qt python-3.x

我有2个单选按钮selectDateselectTime。在它们下面是一些小部件。当我们选择上面的单选按钮时,我试图让小部件不会变灰。

Python3.4(Anaconda3发行版),Windows 10 Pro 64位,Qt4 64位

以下是检查是否已选择的代码:

if self.selectTime.isChecked() == True:
        self.greyOut2(MainWindow)

if self.selectDate.isChecked() == True:
        self.greyOut1(MainWindow)

这是greyOut12。执行时,他们成功地使小部件变灰。

def greyOut1(self, MainWindow): #selectDate on
    print("go1")
    self.label_3.setEnabled(False)
    self.relativeTime.setEnabled(False)
    self.label_4.setEnabled(True)
    self.dateTime.setEnabled(True)

def greyOut2(self, MainWindow): #selectTime on
    print("go2")
    self.lebel_3.setEnabled(True)
    self.relativeTime.setEnabled(True)
    self.label_4.setEnabled(False)
    self.dateTime.setEnabled(False)

现在我的问题是,IF语句只执行一次所以当你选择按钮时什么都不会发生。

程序看起来像这样。

enter image description here

这是完整的(生锈的)代码(打印用于调试)。这是我的第一个GUI程序,我现在正在学习Qt一周,所以请不要使用高级代码。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(261, 371)
        MainWindow.setMinimumSize(QtCore.QSize(261, 371))
        MainWindow.setMaximumSize(QtCore.QSize(261, 371))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../Dropbox/python/gui/icon.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setUnifiedTitleAndToolBarOnMac(False)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(261, 371))
        self.centralwidget.setMaximumSize(QtCore.QSize(261, 371))
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.action = QtGui.QComboBox(self.centralwidget)
        self.action.setGeometry(QtCore.QRect(104, 60, 91, 22))
        self.action.setObjectName(_fromUtf8("action"))
        self.action.addItem(_fromUtf8(""))
        self.action.addItem(_fromUtf8(""))
        self.action.addItem(_fromUtf8(""))
        self.action.addItem(_fromUtf8(""))
        self.action.addItem(_fromUtf8(""))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 10, 221, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(50, 60, 51, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.groupBox = QtGui.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 100, 241, 191))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.selectTime = QtGui.QRadioButton(self.groupBox)
        self.selectTime.setGeometry(QtCore.QRect(20, 30, 82, 17))
        self.selectTime.setObjectName(_fromUtf8("selectTime"))
        self.buttonGroup_2 = QtGui.QButtonGroup(MainWindow)
        self.buttonGroup_2.setObjectName(_fromUtf8("buttonGroup_2"))
        self.buttonGroup_2.addButton(self.selectTime)
        self.selectDate = QtGui.QRadioButton(self.groupBox)
        self.selectDate.setGeometry(QtCore.QRect(20, 110, 82, 17))
        self.selectDate.setObjectName(_fromUtf8("selectDate"))
        self.buttonGroup_2.addButton(self.selectDate)
        self.label_3 = QtGui.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(116, 60, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_3.setFont(font)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.relativeTime = QtGui.QTimeEdit(self.groupBox)
        self.relativeTime.setGeometry(QtCore.QRect(40, 60, 61, 22))
        self.relativeTime.setObjectName(_fromUtf8("relativeTime"))
        self.label_4 = QtGui.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(40, 140, 21, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_4.setFont(font)
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.dateTime = QtGui.QDateTimeEdit(self.groupBox)
        self.dateTime.setGeometry(QtCore.QRect(70, 140, 111, 22))
        self.dateTime.setObjectName(_fromUtf8("dateTime"))
        self.forceCheck = QtGui.QCheckBox(self.centralwidget)
        self.forceCheck.setGeometry(QtCore.QRect(10, 300, 81, 21))
        self.forceCheck.setObjectName(_fromUtf8("forceCheck"))
        self.startButton = QtGui.QPushButton(self.centralwidget)
        self.startButton.setGeometry(QtCore.QRect(170, 300, 71, 23))
        self.startButton.setObjectName(_fromUtf8("startButton"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(20, 330, 211, 20))
        self.label_5.setObjectName(_fromUtf8("label_5"))

        print(self.selectTime.isChecked())
        print(self.selectDate.isChecked())

        if self.selectTime.isChecked() == True:
            self.greyOut2(MainWindow)

        if self.selectDate.isChecked() == True:
            self.greyOut1(MainWindow)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Auto Shutdown", None))
        self.action.setItemText(0, _translate("MainWindow", "Shut Down", None))
        self.action.setItemText(1, _translate("MainWindow", "Restart", None))
        self.action.setItemText(2, _translate("MainWindow", "Sleep", None))
        self.action.setItemText(3, _translate("MainWindow", "Log Off", None))
        self.action.setItemText(4, _translate("MainWindow", "Lock", None))
        self.label.setText(_translate("MainWindow", "Please chose an action and then a time for it.", None))
        self.label_2.setText(_translate("MainWindow", "Action:", None))
        self.groupBox.setTitle(_translate("MainWindow", "Time Schedule", None))
        self.selectTime.setText(_translate("MainWindow", "Relative Time", None))
        self.selectDate.setText(_translate("MainWindow", "Date", None))
        self.label_3.setText(_translate("MainWindow", "From now", None))
        self.label_4.setText(_translate("MainWindow", "On", None))
        self.forceCheck.setText(_translate("MainWindow", "Force Action", None))
        self.startButton.setText(_translate("MainWindow", "Start", None))
        self.label_5.setText(_translate("MainWindow", "Your computer will", None))



    def greyOut1(self, MainWindow): #selectDate on
        print("go1")
        self.label_3.setEnabled(False)
        self.relativeTime.setEnabled(False)
        self.label_4.setEnabled(True)
        self.dateTime.setEnabled(True)

    def greyOut2(self, MainWindow): #selectTime on
        print("go2")
        self.lebel_3.setEnabled(True)
        self.relativeTime.setEnabled(True)
        self.label_4.setEnabled(False)
        self.dateTime.setEnabled(False)



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

0 个答案:

没有答案