我一直在研究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)
您已经能够实现的任务可以用更优雅的方式完成。我们可以使用返回生成信号的对象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)
我找到了一种方法来组合我的复选框值检查并通过循环运行它。
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)