将PyQt4 CheckBox函数组合成单个函数

时间:2017-02-10 16:32:37

标签: python pyqt pyqt4 qcheckbox

我一直在使用XCOPY GUI,我在复选框中添加了选择所需的xcopy开关。我已经完成了所有工作,但我正在尝试压缩代码,因为我确信有办法实现它,我只是不确定如何。基本上,对于每个开关(E,C,H,Y,I),我做一个函数调用来检查CheckBox的状态并返回我的x_copy函数的子进程调用的值。

我怎样才能调用单个函数并检查所有复选框小部件的状态?

import sys
from PyQt4 import QtGui
import os
import subprocess


class XcopyMain(QtGui.QWidget):
    def __init__(self):
        super(XcopyMain, self).__init__()

        # Declare Widgets
        src_btn = QtGui.QPushButton('Source')
        dst_btn = QtGui.QPushButton('Destination')
        prev_btn = QtGui.QPushButton('Preview File(s)')
        x_copy_btn = QtGui.QPushButton('Start XCOPY')
        switch_lbl = QtGui.QLabel('Switches:')

        # self.progress = QtGui.QProgressBar(self)

        self.src_line = QtGui.QLineEdit()
        self.dst_line = QtGui.QLineEdit()
        self.selected_files = QtGui.QTextEdit()

        self.e_chk = QtGui.QCheckBox('E')
        self.e_chk.stateChanged.connect(self.e_apply)
        self.c_chk = QtGui.QCheckBox('C')
        self.c_chk.stateChanged.connect(self.c_apply)
        self.h_chk = QtGui.QCheckBox('H')
        self.h_chk.stateChanged.connect(self.h_apply)
        self.y_chk = QtGui.QCheckBox('Y')
        self.y_chk.stateChanged.connect(self.y_apply)
        self.i_chk = QtGui.QCheckBox('I')
        self.i_chk.stateChanged.connect(self.i_apply)

        # Declare Emit / Slot
        src_btn.clicked.connect(self.src_select)
        dst_btn.clicked.connect(self.dst_select)
        prev_btn.clicked.connect(self.list_files)
        x_copy_btn.clicked.connect(self.x_copy)

        # Declare Layout
        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(src_btn, 0, 0)
        mainLayout.addWidget(dst_btn, 0, 1)
        mainLayout.addWidget(prev_btn, 2, 0)
        mainLayout.addWidget(x_copy_btn, 2, 1)

        mainLayout.addWidget(self.src_line, 1, 0)
        mainLayout.addWidget(self.dst_line, 1, 1)
        mainLayout.addWidget(self.selected_files, 3, 0)

        mainLayout.addWidget(switch_lbl, 0, 2)

        mainLayout.addWidget(self.e_chk, 1, 2)
        mainLayout.addWidget(self.c_chk, 2, 2)
        mainLayout.addWidget(self.h_chk, 3, 2)
        mainLayout.addWidget(self.y_chk, 4, 2)
        mainLayout.addWidget(self.i_chk, 5, 2)

        # mainLayout.addWidget(self.progress,4,0)

        self.setLayout(mainLayout)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('X Copy 3.0')
        self.show()

    def src_select(self):
        src_fldr = QtGui.QFileDialog.getExistingDirectory(self, 'Select Directory')
        self.src_line.setText(src_fldr)

    def dst_select(self):
        dst_fldr = QtGui.QFileDialog.getExistingDirectory(self, 'Select Directory')
        self.dst_line.setText(dst_fldr)

    def list_files(self):
        src_path = self.src_line.text()
        for f in (os.listdir(src_path)):
            self.selected_files.append(f)

    def x_copy(self):
        src_path = self.src_line.text()
        dst_path = self.dst_line.text()
        #print(src_path + ' plus ' + dst_path + ' plus ' + self.attr_check())
        subprocess.call(['xcopy', src_path, dst_path, '/' + self.e_apply() + '/' +
                                                            self.c_apply() + '/' +
                                                            self.h_apply() + '/' +
                                                            self.y_apply() + '/' +
                                                            self.i_apply()])

    def e_apply(self):
        state = self.e_chk.checkState()
        if state == 2:
            return 'E'
        else:
            print('E not selected')

    def c_apply(self):
        state = self.e_chk.checkState()
        if state == 2:
            return 'C'
        else:
            print('C not selected')

    def h_apply(self):
        state = self.e_chk.checkState()
        if state == 2:
            return 'H'
        else:
            print('H not selected')

    def y_apply(self):
        state = self.e_chk.checkState()
        if state == 2:
            return 'Y'
        else:
            print('Y not selected')

    def i_apply(self):
        state = self.e_chk.checkState()
        if state == 2:
            return 'I'
        else:
            print('I not selected')

app = QtGui.QApplication(sys.argv)
mainWindow = XcopyMain()
status = app.exec_()
sys.exit(status)

0 个答案:

没有答案