繁体   English   中英

python for 循环意外停止

[英]python for loop unexpectedly stopping

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


不需要数字的点击 function 将设置为在鼠标当前点击 position

我正在为程序提供的.txt 文件是

CL CL,1600,600

ST,3

氯,1600,400

氯,1400,600

氯,1600,400

氯,1400,600

氯,1600,400

我收到的输出(包括在被调用位置的点击)

点击

3

无效的密钥

我认为这很奇怪,因为延迟运行正确并且只运行一系列点击#来测试这个删除 ST,3 似乎并没有停止程序,所以我对停止运行 rest 的程序表单的原因感到困惑即使确实得到了这个无效的键,点击

我知道这可能是一些新手错误或判断失误我为我缺乏 python 和英语技能而道歉" 在 x 或 "3" ...

我认为这会做你想要的。

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" )

我对代码做了一些小的改动。 随意测试

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