Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Sometimes I need to make multiple copies of code with incrementing numbers. In a form I'm coding, I need to make upwards of 12 checkboxes, each of which requires the following code:

    self.checkBox1 = QtGui.QCheckBox()
    self.checkBox1.setGeometry(QtCore.QRect(20, 20, 70, 17))
    self.checkBox1.setObjectName(_fromUtf8("checkBox1"))

The script below enables me to avoid the boring task of manually changing the numbers for each checkbox. I just copy the 3 lines above into the windows clipboard, and then... I insert "checkBox" into the first field in the form, "1" into the second field, and 12 into the third field. When I hit the "ok" button, the 12 sequentially numbered copies of the 3 lines appear in the 4th field in the form. I hope this provides some help to other people.

Here's my code:

    # -*- coding: latin-1 -*-
duplicate_text_with_incrementing_nos_for_programming_and_paste_to_clipboard.py
Harvest text from clipboard and run functions below, and then paste back to clipboard
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import (Qt, SIGNAL)
from PyQt4.QtGui import (QApplication, QDialog, QHBoxLayout, QLabel,
        QPushButton)
class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.initUI()
    def initUI(self):
        okButton01 = QtGui.QPushButton("OK")
        cancelButton01 = QtGui.QPushButton("Cancel")
        okButton01.clicked.connect(self.fn_okButton01_clicked)
        cancelButton01.clicked.connect(QtCore.QCoreApplication.instance().quit)
        self.cancelButton01 = cancelButton01
        prefix_label = QtGui.QLabel('Prefix')
        digit_label = QtGui.QLabel('Digit')
        iterations_label = QtGui.QLabel('Iterations')
        clip_label = QtGui.QLabel('Clip')
        prefixEdit = QtGui.QLineEdit()
        digitEdit = QtGui.QLineEdit()
        iterationsEdit = QtGui.QLineEdit()
        reviewEdit = QtGui.QTextEdit()
        self.prefix_label = prefix_label
        self.digit_label = digit_label
        self.iterations_label = iterations_label
        self.clip_label = clip_label
        self.prefixEdit = prefixEdit
        self.digitEdit = digitEdit
        self.iterationsEdit = iterationsEdit
        self.reviewEdit = reviewEdit
        hbox01 = QtGui.QHBoxLayout()
        hbox01.addWidget(prefix_label)
        hbox01.addWidget(prefixEdit)
        hbox01.addWidget(digit_label)
        hbox01.addWidget(digitEdit)
        hbox01.addWidget(iterations_label)
        hbox01.addWidget(iterationsEdit)
        hbox03 = QtGui.QHBoxLayout()
        hbox03.addWidget(clip_label)
        hbox03.addWidget(reviewEdit)
        self.reviewEdit.setText(fn_getText())
        hbox00 = QtGui.QHBoxLayout()
        hbox00.addStretch(1)
        hbox00.addWidget(okButton01)
        hbox00.addWidget(cancelButton01)
        vbox0 = QtGui.QVBoxLayout()
        vbox0.addLayout(hbox01)
        vbox0.addStretch(1)
        vbox0.addLayout(hbox03)
        vbox0.addStretch(1)
        vbox0.addLayout(hbox00)
        self.setLayout(vbox0)
        self.setGeometry(300, 300, 600, 300) #class PySide.QtCore.QRectF(left, top, width, height)   http://srinikom.github.com/pyside-docs/PySide/QtCore/QRectF.html#PySide.QtCore.QRectF
        self.setWindowTitle('Duplicate Code Strings W/Increasing Numbers')  
        self.show()
    def fn_okButton01_clicked(self):
        prefixEditText = str(self.prefixEdit.text())
        digitEditText = str(self.digitEdit.text())
        iterationsEditText = str(self.iterationsEdit.text())
        nutext = prefixEditText + ' ' + digitEditText + ' ' + iterationsEditText 
        print 'Line 89: nutext = ' + str(nutext)
        original_clip = self.reviewEdit.toPlainText() # PySide.QtGui.QLineEdit.text(),  http://srinikom.github.com/pyside-docs/PySide/QtGui/QLineEdit.html
        txt2paste2clipbd = fn_duplicate_code_with_increments(texte=str(original_clip), no_of_copies=str(iterationsEditText), string_b4_digits=str(prefixEditText), digits=str(digitEditText))
        self.reviewEdit.setPlainText(txt2paste2clipbd)
        setWinClipText(txt2paste2clipbd)
        #self.deleteLater()
        #event.accept() #http://www.qtcentre.org/threads/20895-PyQt4-Want-to-connect-a-window-s-close-button
        #self.destroy()
def formm():
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
def fn_getText(): 
    # get text from clipboard
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
    win32clipboard.CloseClipboard()
    return text
def setWinClipText(aString):
    # Send text to clipboard
    import win32clipboard
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(aString)
    win32clipboard.CloseClipboard()
def fn_duplicate_code_with_increments(texte, no_of_copies, string_b4_digits, digits):
    to do: combine args 2 and 3, and use re module to determine with chars are the digits to increment
    import re
    import string
    i = 0
    tempclipDup = texte[:]
    temp_instance = ''
    accumulator = ''
    while i <= int(no_of_copies) - 1:
        i +=1
        orig_str = string_b4_digits + str(digits)
        replact_st = string_b4_digits + str(i)
        temp_instance = tempclipDup.replace(orig_str, replact_st)
        if len(accumulator) > 2:
            accumulator = accumulator + '\n' + temp_instance
        else:
            accumulator = temp_instance
    return accumulator
if 1 == 1:
    import os
    import sys
    import subprocess
    import win32clipboard
    import win32con
    fn_operation_log = ''
    arg_sent_2this_script = ''
    alternative = 1
    if alternative == 1:
        formm()
    elif alternative == 2:
        txt2paste2clipbd = fn_duplicate_code_with_increments(texte=fn_getText(), no_of_copies=3, string_b4_digits='hbox', digits=1)
        setWinClipText(txt2paste2clipbd)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.