2017-02-10 6 views
0

私はXCOPY GUIで作業していましたが、チェックボックスに追加して、目的のxcopyスイッチを選択しました。私はそれがすべて働いているが、私はそれを行う方法があると確信しているので、コードを凝縮しようとしている、私はちょうど方法がわからない。基本的には、各スイッチ(E、C、H、Y、I)に対してCheckBoxの状態をチェックし、x_copy関数のサブプロセス呼び出しの値を返す関数呼び出しを行います。PyQt4 CheckBox関数を単一の関数に結合する

私は単一の関数を呼び出して、すべてのチェックボックスウィジェットの状態をチェックすることができますか?

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

[QButtonGroup(https://doc.qt.io/qt-4.8:我々は、信号

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) 

完全なコードを生成するオブジェクトを返すsender()関数を使用することができ/qbuttongroup.html)。 – ekhumoro

答えて

0

実行可能なタスクは、より洗練された方法で実行できます。

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) 
+0

素晴らしい、感謝eyllanesc!私はリファクタリングがもっと必要であることを知っていましたが、最初のリビジョンを考え出すのは永遠に私を必要としました。次回はリビジョンを投稿します。私は何か他のものも変えてしまったので、私は再びすべてを汲み上げました。 –

0

チェックボックスの値チェックを組み合わせてループで実行する方法が見つかりました。

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) 
+0

あなたの答えをよりよく説明し、あなたの反応を改善するために必要なコードだけを入れることができます。 – eyllanesc

関連する問題