简体   繁体   English

python for 循环意外停止

[英]python for loop unexpectedly stopping

new to python trying to create a program you can feed a.txt file and have the program perform a specific list of actions code below python 的新手尝试创建一个程序,您可以提供一个 .txt 文件并让程序执行下面的特定操作列表

import pyautogui
import time
import keyboard
import random
import win32api, win32con
from tkinter import *
import re

File = open("data.txt", "r")
if File.mode == "r":
    myfile =File.read()

def Convert(string):
    li = list(string.split(" "))
    li = list(string.split("\n"))
    return li

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,1,1)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,1,1)

def wait(x):
    print(x)
    time.sleep(x)

  
instruccions = Convert(myfile)


    


for x in instruccions:
    if  "1" in x or "2" in x or "3" in x or "4" in x or "5" in x or "6" in x or "7" in x or "8" in x or "9" in x or "0" in x:
      x = list(x.split(","))
      if x[0] == "CL":
            action = (x[0])
            xcords = (int(x[1]))
            ycords = (int(x[2]))
            click(xcords,ycords)
      elif x[0] == "ST":
          TimeS = (int(x[1]))
          wait(TimeS)

            
        
    elif "1" not in x:
        
       if x == "CL":
           print("click")
       elif x == "PK":
           print("pressing key")
       elif x != "CL" or "PK":
           print("invalid key")


File.close


the click function that doesn't require numbers will be set to click at the mouse current position不需要数字的点击 function 将设置为在鼠标当前点击 position

the.txt file I'm feeding the program is我正在为程序提供的.txt 文件是

CL CL,1600,600 CL CL,1600,600

ST,3 ST,3

Cl,1600,400氯,1600,400

Cl,1400,600氯,1400,600

Cl,1600,400氯,1600,400

Cl,1400,600氯,1400,600

Cl,1600,400氯,1600,400

to which I receive the output(including the a click at the called position)我收到的输出(包括在被调用位置的点击)

click点击

3 3

invalid key无效的密钥

which I think is odd since the delay is correctly run and only running a series of clicks #to test this delete ST,3 doesn't seem to stop the program so I'm confused at what is stopping the program form running the rest of the clicks even if does get this invalid key我认为这很奇怪,因为延迟运行正确并且只运行一系列点击#来测试这个删除 ST,3 似乎并没有停止程序,所以我对停止运行 rest 的程序表单的原因感到困惑即使确实得到了这个无效的键,点击

I know its probably some rookie mistake or a lapse in judgment I apologize for my lacking python and English skills any other remark or criticism would be greatly appreciated like a better way to check for numbers instead of using if "1" in x or "2" in x or "3"...我知道这可能是一些新手错误或判断失误我为我缺乏 python 和英语技能而道歉" 在 x 或 "3" ...

I think this will do what you want.我认为这会做你想要的。

import pyautogui
import time
import keyboard
import win32api, win32con

File = open("data.txt", "r")

def click(x=None,y=None):
    if x is not None:
        win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,1,1)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,1,1)

def wait(x):
    print(x)
    time.sleep(x)

for line in myfile:
    parts = line.strip().split(',')
    if parts[0] == 'ST':
        TimeS = (int(x[1]))
        wait(TimeS)
    elif parts[0] in ("CL","Cl"):
        if len(parts) == 1:
            print( "click current position" )
            click()
        else:
            xcords = int(parts[1])
            ycords = int(parts[2])
            print( f"click at {xcords},{ycords}" )
            click(xcords,ycords)
    elif parts[0] == "PK":
        print( "press key" )
    else:
        print( "unknown command" )

I made some small change in code.我对代码做了一些小的改动。 Feel free to test随意测试

import time
import win32api, win32con

with open("data.txt", "r") as File:  
    # handles the closing of File automatically after 'with' block execution

    myfile = File.read()

    def Convert(string):
        string = string.replace("\n", " ")
        li = string.split(" ")

        return li


    def click(x, y):
        win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1, 1)
        time.sleep(0.1)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1, 1)
        print("clicked")


    def wait(x):
        print(x)
        time.sleep(x)

    instructions = Convert(myfile)

    number_list = [str(i) for i in range(0, 10)]

    for each_instruction in instructions:
        each_sub_instruction = []

        for each_number in number_list:
            if each_number in each_instruction:
                each_sub_instruction = each_instruction.split(",")

                if each_sub_instruction[0] == "CL":
                    action = (each_sub_instruction[0])
                    xcords = (int(each_sub_instruction[1]))
                    ycords = (int(each_sub_instruction[2]))
                    click(xcords, ycords)

                elif each_instruction[0] == "ST":
                    TimeS = (int(each_instruction[1]))
                    wait(TimeS)

                break

        if each_instruction == "CL":
            print("click")
        elif each_instruction == "PK":
            print("pressing key")
        elif each_sub_instruction and each_sub_instruction[0] != "CL" and each_sub_instruction[0] != "PK":
            print("invalid key")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM