pyautogui图形界面自动化

抱歉这么久没更新,首先得承认主要原因是过年嗨得心收不回来,

费了好大劲才沉下来着手准备python程序控制鼠标和键盘。

首先是调整参数(落点的坐标,自动化的节奏)花费了很多时间,本以为大功告成之际竟然被一个中文输出(中文字符保存到剪切板,继而输出到文本框,然后就是乱码乱码乱不完的码)给卡住了,

换了几个办法查了很多资料都无功而返,所以我现在状态是这样婶滴:

和这样色儿滴:

我明天继续找解决办法,实在不行先避开这块

这个实例做完做类和对象,然后就暂停我好把前面讲过的基础部分仔细整理下

 

# for demostration for autoGUI

import pyautogui, time, csv

# Dragging the mouse
# https://www.sumopaint.com/paint/

# time.sleep(3)
# pyautogui.click()    # click to put drawing program in focus
# distance = 200
# interval = 5
#
# D = 0.1
#
# # Dragging the Mouse
#
# while distance > 0:
#     pyautogui.dragRel(distance, 0, duration=D)   # move right
#     distance = distance - interval
#     pyautogui.dragRel(0, distance, duration=D)   # move down
#     pyautogui.dragRel(-distance, 0, duration=D)  # move left
#     distance = distance - interval
#     pyautogui.dragRel(0, -distance, duration=D)  # move up


# from tkinter import Tk
#
# customers = []
#
# partyFile = open('party.csv', encoding='utf-8')
# partyReader = csv.reader(partyFile)
#
# for row in partyReader:
# 	if partyReader.line_num == 1:
# 		continue
#
# 	food = row[3]
# 	foods = food.split()
# 	customer = {
# 		'school': row[0],
# 		'attend': row[1],
# 		'headcount': row[2],
# 		'foodOption': foods,
# 		'taboos': row[4],
# 		'contact': row[5]
# 	}
# 	customers.append(customer)
# partyFile.close()
#
# pyautogui.click((100, 100))
#
# for customer in customers:
#
# 	r = Tk()
# 	r.withdraw()
# 	r.clipboard_clear()
# 	r.clipboard_append(customer['taboos'])
# 	r.update()  # now it stays on the clipboard after the window is closed
#
# 	pyautogui.hotkey('command', 'v')
# 	pyautogui.press('\n')
#
# 	r.destroy()

import subprocess
customers = []

partyFile = open('party.csv', encoding='utf-8')
partyReader = csv.reader(partyFile)

for row in partyReader:
	if partyReader.line_num == 1:
		continue

	food = row[3]
	foods = food.split()
	customer = {
		'school': row[0],
		'attend': row[1],
		'headcount': row[2],
		'foodOption': foods,
		'taboos': row[4],
		'contact': row[5]
	}
	customers.append(customer)
partyFile.close()

pyautogui.click((100, 100))

# add: empty clipboard, doesn't work
# subprocess.Popen(['pbcopy'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
#
# for customer in customers:
#
# 	p1 = subprocess.Popen(['echo', customer['taboos']], stdout=subprocess.PIPE)
# 	p2 = subprocess.Popen(['pbcopy'], stdin=p1.stdout, stdout=subprocess.PIPE)
#
# 	pyautogui.hotkey('command', 'v')

# add index, work
for customer in customers:

	p1 = subprocess.Popen(['echo', customer['taboos']], stdout=subprocess.PIPE)
	p2 = subprocess.Popen(['pbcopy'], stdin=p1.stdout, stdout=subprocess.PIPE)
	pyautogui.press(str(customers.index(customer)))
	pyautogui.press('\b')
	pyautogui.hotkey('command', 'v')


import pyautogui, time

#  screen’s width and height in pixels
# print(pyautogui.size())
# width, height = pyautogui.size()
# print(pyautogui.position())

# Moving the Mouse
# for i in range(10):
#       pyautogui.moveTo(100, 100, duration=0.25)
#       pyautogui.moveTo(200, 100, duration=0.25)
#       pyautogui.moveTo(200, 200, duration=0.25)
#       pyautogui.moveTo(100, 200, duration=0.25)

# for i in range(10):
#       pyautogui.moveTo(100, 100)
#       pyautogui.moveTo(200, 100)
#       pyautogui.moveTo(200, 200)
#       pyautogui.moveTo(100, 200)

# time.sleep(2)
# for i in range(10):
#       pyautogui.moveRel(100, 0, duration=0.25)
#       pyautogui.moveRel(0, 100, duration=0.25)
#       pyautogui.moveRel(-100, 0, duration=0.25)
#       pyautogui.moveRel(0, -100, duration=0.25)

# Getting the Mouse Position
# time.sleep(1)
# print(pyautogui.position())

# Click the mouse

# Dragging the mouse

# https://www.sumopaint.com/paint/
#
# time.sleep(2)
# pyautogui.click()    # click to put drawing program in focus
# distance = 200
# interval = 10
# dur = 0.1
#
# # Dragging the Mouse
#
# while distance > 0:
#     pyautogui.dragRel(distance, 0, dur)   # move right
#     distance = distance - interval
#     pyautogui.dragRel(0, distance, dur)   # move down
#     pyautogui.dragRel(-distance, 0, dur)  # move left
#     distance = distance - interval
#     pyautogui.dragRel(0, -distance, dur)  # move up


# Scroll the mouse

# Getting a screenshot
# im = pyautogui.scre)enshot()
# print(im.getpixel((50, 200))

# Analyzing the screenshot
# print(pyautogui.pixelMatchesColor(50, 200, (222, 186, 197, 255)))

# Image recognition
# imageLocation = pyautogui.locateOnScreen('flower.png')
# print(imageLocation)
# #
# coordination = pyautogui.center(imageLocation)
# print(coordination)
# #
# pyautogui.click(coordination)
#
# print(list(pyautogui.locateAllOnScreen('flower.png')))

# Sending a string from the keyboard
# pyautogui.click(100, 100)
# pyautogui.typewrite('Hello world!\n')
# pyautogui.typewrite('Hello world!\n', 0.25)
# pyautogui.typewrite(['a', 'b', 'left', 'left', 'X', 'Y'], 0.25)

# Key Names
# print(pyautogui.KEYBOARD_KEYS)
# import pprint
# pprint.pprint(pyautogui.KEYBOARD_KEYS)

# Pressing and Releasing the Keyboard
time.sleep(2)
# pyautogui.keyDown('shift')
# pyautogui.press('2')
# pyautogui.keyUp('shift')

# pyautogui.press('@')

# typewrite(): type a string into a text field
# press(): single-key commands

# Hotkey Combinations
pyautogui.PAUSE = 1
pyautogui.click(100, 100)
pyautogui.hotkey('command', 'a')
pyautogui.hotkey('command', 'c')
pyautogui.hotkey('option', 'right')
pyautogui.hotkey('command', 'v')



# mouseNow.py - Displays the mouse cursor's current position.

import pyautogui
import time

print('Press fn+command+F2(Mac) or Ctrl+C(windows) to quit.')
positionStr = ''
try:
    while True:

        time.sleep(3)

        # Get and print the mouse coordinates.
        x, y = pyautogui.position()
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        positionStr = 'X: {:4};  Y: {:4}; RGB:{}'.format(x, y, pixelColor)

        print(positionStr)

except KeyboardInterrupt:
    print('\nDone.')


# Get current coordination and color of the mouse cursor
#
from pynput import mouse
import pyautogui

# def on_click(x, y, button, pressed):
#
#     # if button == mouse.Button.right:
#     #     return False
#     #
#     if x<50 and y<50:
#         return False
#
#     if pressed:
#         coordinate = (int(x), int(y))
#         pixelColor = pyautogui.screenshot().getpixel(coordinate)
#         print(coordinate, pixelColor)
#
# # Collect events until released
# with mouse.Listener(
#         on_click=on_click,
# ) as listener:
#         listener.join()


# ctrl+c(windows) or command+f2(Mac) to quit
# def on_click(x, y, button, pressed):
#
#     if pressed:
#         coordination = (int(x), int(y))
#         pixelColor = pyautogui.screenshot().getpixel(coordination)
#         print(coordination, pixelColor)
#
# print('Press fn+command+F2(Mac) or Ctrl+C(windows) to quit.')
# try:
#     # Collect events
#     with mouse.Listener(on_click=on_click) as listener:
#         listener.join()
#
# except KeyboardInterrupt:
#         listener.stop()






# Class

# from pynput import mouse
# import pyautogui
#
#
# class MyException(Exception):
#     pass
#
#
# def on_click(x, y, button, pressed):
#
#     if button == mouse.Button.right:
#         raise MyException(button)
#     # if x<10 and y<10:
#     #     raise MyException(button)
#
#     if pressed:
#         x, y = pyautogui.position()
#         pixelColor = pyautogui.screenshot().getpixel((x, y))
#         print((x, y), pixelColor)
#
# # Collect events until released
# with mouse.Listener(
#         on_click=on_click,
# ) as listener:
#     try:
#         listener.join()
#
#     except MyException:
#         listener.stop()