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