我想同时运行2个循环,可以吗?

时间:2019-04-03 06:04:29

标签: python

我要运行2个循环,即loop_1和loop_2 我想同时运行它们 但它不起作用,它只能运行1次循环

# i've tried this one
import pyautogui
import time
import loop_1

pyautogui.FAILSAFE = True
times = interval=input("Enter speed: ")
text = input("Enter Text you want to repeat: ")
loops = int(input("Amount of repeats: "))

time.sleep(5)

for x in range(loops):
    pyautogui.press('enter', interval=0.01)
    pyautogui.typewrite(text, times)
    pyautogui.press('enter')
    pyautogui.typewrite(text, times)
    pyautogui.press('enter')

# loop_1

for i in range(100):
        pyautogui.press('enter', interval=0.01)

# loop_2

for x in range(loops):
    pyautogui.press('enter', interval=0.01)
    pyautogui.typewrite(text, times)
    pyautogui.press('enter')
    pyautogui.typewrite(text, times)
    pyautogui.press('enter')

它不起作用,只是停留在loop_1中,我希望它同时运行两个循环

1 个答案:

答案 0 :(得分:1)

使用多线程同时运行循环。搜索关键字,您将找到许多有关该关键字的教程。例如:

class Main(QDialog):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        # Main window setup
        self.setWindowTitle("Stacked widget example")
        self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogNewFolder))
        self.setMinimumSize(400, 400)
        self.setMaximumSize(640, 480)

        rootVBox = QVBoxLayout(self)
        rootHBox = QHBoxLayout()
        rootHBox.addStretch()
        rootVBox.addStretch()

        self.pages = [FirstPage, SecondPage]
        self.stacked = QStackedWidget(self)

        for i in self.pages: self.stacked.addWidget(i(self))

        self.buttonNext = QPushButton("Next")
        self.buttonNext.clicked.connect(self.buttonNextConnect)

        self.buttonBack = QPushButton("Back")
        self.buttonBack.clicked.connect(self.buttonBackConnect)

        rootHBox.addWidget(self.buttonBack)
        rootHBox.addWidget(self.buttonNext)

        rootVBox.addLayout(rootHBox)

        self.stacked.currentChanged.connect(self.on_currentChanged)

    def buttonNextConnect(self):
        if self.stacked.currentIndex() == self.stacked.count() -1:
            self.finish()
        if self.stacked.currentIndex() < self.stacked.count() -1:
            self.stacked.setCurrentIndex(self.stacked.currentIndex() + 1)

    def buttonBackConnect(self):
        if self.stacked.currentIndex() > 0:
            self.stacked.setCurrentIndex(self.stacked.currentIndex() - 1)

    def on_currentChanged(self, index):
        if index == self.stacked.count() -1:
            self.buttonNext.setText("Finish")
        else:
            self.buttonNext.setText("Next")

    def finish(self):
        self.close()