Python自动输入

因为考试没法复制粘贴,故弄了个可以模拟输入法输入的程序
只需要将要输入的文字放到程序的输入框中,然后点击开始按钮,出现“进入按键监听阶段”后,
进入到待输入界面,按空格键即可。不过此代码准确率不高,或许有更好的办法,望大家指点。

import pyautogui as gui
import time
from xpinyin import Pinyin
import jieba
import re
from tkinter import Tk,Button,filedialog,scrolledtext,Label
from threading import Thread
from pynput.keyboard import Key, Listener

class Mm:
    def __init__(self):
        self.button_tf = False
        self.ui()

    def ui(self):
        self.window = Tk()
        self.window.title('自动输入')
        self.window.geometry('400x200')
        self.window.config(background='gray')

        self.button_1 = Button(self.window,text='开始',command=lambda :self.thread1())
        self.button_1.place(x=20,y=60,height=50,width=100)

        self.scr = scrolledtext.ScrolledText(self.window)
        self.scr.place(x=150,y=0,height=200,width=250)

        self.label_1 = Label(self.window,background='gray')
        self.label_1.place(x=0,y=150,width=150,height=40)

        self.window.mainloop()

    def is_symbol(self,char):
        return bool(re.match(r"\W", char))

    def on_press(self,key):
        if key == Key.space:
            self.button_tf = True

    def cz(self):
        self.label_1.config(text='进入按键监听阶段')
        self.button_tf = False
        time.sleep(1)
        while True:
            if self.button_tf is True:
                text = self.scr.get(1.0, 'end')
                print(text)
                a = []
                p = Pinyin()
                seg_list = jieba.lcut(text)
                result = ','.join(seg_list)
                new = result.split(',')

                for ci in new:
                    print(ci)
                    py = p.get_pinyin(ci, '')
                    gui.typewrite(py)
                    time.sleep(0.3)
                    gui.press('space')
                self.button_tf = False
                break
        self.label_1.config(text='任务结束!')


    def jc(self):

        with Listener(on_press=self.on_press) as listener:
            listener.join()

    def thread1(self):
        thread_1 = Thread(target=self.cz)
        thread_1.start()
        thread_2 = Thread(target=self.jc)
        thread_2.start()




if __name__ == '__main__':
    Mm()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值