如何在函数中使用QComboBox选择

时间:2016-01-18 12:25:40

标签: python pyqt

我有以下代码使用pyqt,它显示两个QComboBox,我想在我的应用程序中使用选择值:

class TACRG(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        super(TACRG, self).__init__(parent)
        self.setupUi(self)

        self.CList.addItems(["A", "B", "C", "D"])
        self.connect(self.CList, QtCore.SIGNAL('activated(QString)'), self.c_chosen)

        self.RList.addItems(["Q1", "Q2", "Q3", "Annual"])
        self.connect(self.RList, QtCore.SIGNAL('activated(QString)'), self.r_chosen)

    def r_chosen(self, text):
        report_start, report_end = report_period(text)

    def c_chosen(self, text):
        accs = get_ucs(text)

def report_period(r_period):
    year=date.today().year
    if r_period == 'Q1':
        return (str(year)+'0101',str(year)+'0331')
    elif r_period == 'Q2':
        return (str(year)+'0401',str(year)+'0630')
    elif r_period == 'Q3':
        return (str(year)+'0701',str(year)+'0930')
    elif r_period == 'Annual':
        return (str(year-1)+'0101',str(year-1)+'1231')

def get_ucs(c_name):
    """DO something""
    return """some string"""

现在我不想在另一个函数中使用report_period和get_ucs函数(report_start,report_end,accs)返回的值,这两个函数必须在执行这两个函数后调用。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

使用self在班级数据成员中存储值,在other_methodr_chosen中调用c_chosen,并在使用前检查other_method中的有效值。

class TACRG(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        # Your init code
        self.report_start, self.report_end, self.accs = [None] * 3

    def r_chosen(self, text):
        self.report_start, self.report_end = report_period(text)
        self.other_method()

    def c_chosen(self, text):
        self.accs = get_ucs(text)
        self.other_method()

    def other_method(self):
        #validate if your fields have valid values
        if self.report_start and self.report_end and self.accs:
            print self.report_start, self.report_end, self.accs

编辑(课外的其他方法):

class TACRG(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        # Your init code
        self.report_start, self.report_end, self.accs = [None] * 3

    def r_chosen(self, text):
        self.report_start, self.report_end = report_period(text)
        other_method(self.report_start, self.report_end, self.accs)

    def c_chosen(self, text):
        self.accs = get_ucs(text)
        other_method(self.report_start, self.report_end, self.accs)

def other_method(report_start, report_end, accs):
    #validate if your fields have valid values
    if report_start and report_end and accs:
        print report_start, report_end, accs