Python 将PyQt4复选框函数组合为单个函数

Python 将PyQt4复选框函数组合为单个函数,python,pyqt,pyqt4,qcheckbox,Python,Pyqt,Pyqt4,Qcheckbox,我一直在使用XCOPY GUI,并在复选框中添加了选择所需XCOPY开关的选项。我所有的工作都在进行中,但我正在尝试压缩代码,因为我确信有一种方法可以做到这一点,我只是不知道如何做到。基本上,对于每个开关(E、C、H、Y、I),我执行一个函数调用来检查复选框的状态,并为我的x_copy函数的子进程调用返回值 我怎样才能调用单个函数并让它检查我所有复选框小部件的状态 import sys from PyQt4 import QtGui import os import subprocess c

我一直在使用XCOPY GUI,并在复选框中添加了选择所需XCOPY开关的选项。我所有的工作都在进行中,但我正在尝试压缩代码,因为我确信有一种方法可以做到这一点,我只是不知道如何做到。基本上,对于每个开关(E、C、H、Y、I),我执行一个函数调用来检查复选框的状态,并为我的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)

我找到了一种组合复选框值检查并通过循环运行它的方法

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.myList = []

        self.E_chk = QtGui.QCheckBox('E')
        self.E_chk.stateChanged.connect(self.chk_box_value)
        self.C_chk = QtGui.QCheckBox('C')
        self.C_chk.stateChanged.connect(self.chk_box_value)
        self.H_chk = QtGui.QCheckBox('H')
        self.H_chk.stateChanged.connect(self.chk_box_value)
        self.Y_chk = QtGui.QCheckBox('Y')
        self.Y_chk.stateChanged.connect(self.chk_box_value)
        self.I_chk = QtGui.QCheckBox('I')
        self.I_chk.stateChanged.connect(self.chk_box_value)

        # 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()
        subLayout = QtGui.QHBoxLayout()
        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, 1, 2)

        #mainLayout.addWidget(switch_lbl, 0, 2)

        subLayout.addWidget(self.E_chk)
        subLayout.addWidget(self.C_chk)
        subLayout.addWidget(self.H_chk)
        subLayout.addWidget(self.Y_chk)
        subLayout.addWidget(self.I_chk)

        # Declare ToolTips

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        #self.setToolTip('This switch does XXX')
        self.E_chk.setToolTip('Copies directories and subdirectories, including empty ones.')
        self.C_chk.setToolTip('Continues copying even if errors occur.')
        self.H_chk.setToolTip('Copies hidden and system files also.')
        self.Y_chk.setToolTip('Suppresses prompting to confirm you want to overwrite an existing destination file.')
        self.I_chk.setToolTip('If destination does not exist and copying more than one file, assumes that destination must be a directory.')


        mainLayout.addLayout(subLayout, 5, 0, 1, 5)

        # 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 ' + "".join(self.myList))
        subprocess.call(['xcopy', src_path, dst_path, "".join(self.myList)])

    #
    def chk_box_value(self):
        letter = ['/E', '/C', '/H', '/Y', '/I']
        value = [self.E_chk.checkState(),
                 self.C_chk.checkState(),
                 self.H_chk.checkState(),
                 self.Y_chk.checkState(),
                 self.I_chk.checkState()]
        dictionary = dict(zip(letter, value))
        for k, v in dictionary.items():
            if v == 2 and k not in self.myList:
                self.myList.append(k)
            elif v == 0 and k in self.myList:
                self.myList.remove(k)

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

我找到了一种组合复选框值检查并通过循环运行它的方法

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.myList = []

        self.E_chk = QtGui.QCheckBox('E')
        self.E_chk.stateChanged.connect(self.chk_box_value)
        self.C_chk = QtGui.QCheckBox('C')
        self.C_chk.stateChanged.connect(self.chk_box_value)
        self.H_chk = QtGui.QCheckBox('H')
        self.H_chk.stateChanged.connect(self.chk_box_value)
        self.Y_chk = QtGui.QCheckBox('Y')
        self.Y_chk.stateChanged.connect(self.chk_box_value)
        self.I_chk = QtGui.QCheckBox('I')
        self.I_chk.stateChanged.connect(self.chk_box_value)

        # 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()
        subLayout = QtGui.QHBoxLayout()
        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, 1, 2)

        #mainLayout.addWidget(switch_lbl, 0, 2)

        subLayout.addWidget(self.E_chk)
        subLayout.addWidget(self.C_chk)
        subLayout.addWidget(self.H_chk)
        subLayout.addWidget(self.Y_chk)
        subLayout.addWidget(self.I_chk)

        # Declare ToolTips

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        #self.setToolTip('This switch does XXX')
        self.E_chk.setToolTip('Copies directories and subdirectories, including empty ones.')
        self.C_chk.setToolTip('Continues copying even if errors occur.')
        self.H_chk.setToolTip('Copies hidden and system files also.')
        self.Y_chk.setToolTip('Suppresses prompting to confirm you want to overwrite an existing destination file.')
        self.I_chk.setToolTip('If destination does not exist and copying more than one file, assumes that destination must be a directory.')


        mainLayout.addLayout(subLayout, 5, 0, 1, 5)

        # 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 ' + "".join(self.myList))
        subprocess.call(['xcopy', src_path, dst_path, "".join(self.myList)])

    #
    def chk_box_value(self):
        letter = ['/E', '/C', '/H', '/Y', '/I']
        value = [self.E_chk.checkState(),
                 self.C_chk.checkState(),
                 self.H_chk.checkState(),
                 self.Y_chk.checkState(),
                 self.I_chk.checkState()]
        dictionary = dict(zip(letter, value))
        for k, v in dictionary.items():
            if v == 2 and k not in self.myList:
                self.myList.append(k)
            elif v == 0 and k in self.myList:
                self.myList.remove(k)

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

您能够实现的任务可以以更优雅的方式完成。我们可以使用
sender()
函数返回生成信号的对象

def chk_box_value(self, state):
    k = '/'+self.sender().text()
    if state == QtCore.Qt.Checked: # similar to 2
        self.myList.append(k)
    elif state == QtCore.Qt.Unchecked: # similar to 0
        self.myList.remove(k)

完整代码:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
import os
import subprocess


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

        self.myList = []

        # 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.chk_box_value)
        self.C_chk = QtGui.QCheckBox('C')
        self.C_chk.stateChanged.connect(self.chk_box_value)
        self.H_chk = QtGui.QCheckBox('H')
        self.H_chk.stateChanged.connect(self.chk_box_value)
        self.Y_chk = QtGui.QCheckBox('Y')
        self.Y_chk.stateChanged.connect(self.chk_box_value)
        self.I_chk = QtGui.QCheckBox('I')
        self.I_chk.stateChanged.connect(self.chk_box_value)

        # 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, "".join(self.myList)])

    def chk_box_value(self, state):

        k = '/'+self.sender().text()
        if state == QtCore.Qt.Checked:
            self.myList.append(k)
        elif state == QtCore.Qt.Unchecked:
            self.myList.remove(k)



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

您能够实现的任务可以以更优雅的方式完成。我们可以使用
sender()
函数返回生成信号的对象

def chk_box_value(self, state):
    k = '/'+self.sender().text()
    if state == QtCore.Qt.Checked: # similar to 2
        self.myList.append(k)
    elif state == QtCore.Qt.Unchecked: # similar to 0
        self.myList.remove(k)

完整代码:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
import os
import subprocess


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

        self.myList = []

        # 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.chk_box_value)
        self.C_chk = QtGui.QCheckBox('C')
        self.C_chk.stateChanged.connect(self.chk_box_value)
        self.H_chk = QtGui.QCheckBox('H')
        self.H_chk.stateChanged.connect(self.chk_box_value)
        self.Y_chk = QtGui.QCheckBox('Y')
        self.Y_chk.stateChanged.connect(self.chk_box_value)
        self.I_chk = QtGui.QCheckBox('I')
        self.I_chk.stateChanged.connect(self.chk_box_value)

        # 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, "".join(self.myList)])

    def chk_box_value(self, state):

        k = '/'+self.sender().text()
        if state == QtCore.Qt.Checked:
            self.myList.append(k)
        elif state == QtCore.Qt.Unchecked:
            self.myList.remove(k)



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

…您可以更好地解释您的答案,并只输入改进响应所需的代码。您可以更好地解释您的答案,并只输入改进响应所需的代码。太棒了,谢谢!我知道还有更多的重构工作要做,但我花了很长时间才完成第一次修订。下一次我就把修订版发出去。我也改变了一些其他的事情,这就是为什么我又把所有的事情都写进去了。太棒了,谢谢你!我知道还有更多的重构工作要做,但我花了很长时间才完成第一次修订。下一次我就把修订版发出去。我还改变了一些其他的事情,这就是为什么我又把所有的事情都写进去了。