在日常使用中,复制粘贴的使用频率甚高,复制了上一次的文本,没有保存只能在重新复制。由于这个原因,借此使用该库中的一些功能:
其实
具体需求如下:
1.随时监控剪切板中的的复制文本内容,并添加进listbox中
2.选定listbox中的复制文本内容,即设置剪切板的粘贴内容
3.添加开始、清除、停止的按钮功能
实现需求如下;
实现1.:初步在每0.2秒就检查一次listbox中的是否含有该文本内容、判断剪切板的格式。有即吧内容返回就lisbox中并选中该文本。
实现2.:添加指定的触发listbox函数,设置剪切板内容
按钮功能部分实现代码如下:
def clipboard_set(self,data):
"""设置剪贴板数据"""
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, data)
win32clipboard.CloseClipboard()
def set_listbox_fun(self,event):
current_string=self.textcontent.GetStringSelection()
self.clipboard_set(current_string)
def start_listbox_fun(self,event):
global flag
flag = 1
content_copyphase_panel(self.textcontent, self.startbt)
def stop_listbox_fun(self,event):
global flag
flag = 0
def clean_listbox_fun(self,event):
self.textcontent.Clear()
contentlist.clear()
主要判断复制功能如下:
使用了多线程。
同时如果复制大段文字,并且对文字进行适当的处理可以修改一下的代码。可通过正则re等
class content_copyphase_panel(Thread):
def __init__(self, listbox,button):
Thread.__init__(self)
self.listbox = listbox
self.button=button
self.start()
def clipboard_get(self):
"""获取剪贴板数据"""
win32clipboard.OpenClipboard()
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_UNICODETEXT):#判断剪切板中的格式是否为指定的相关格式
data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
# win32clipboard.CF_UNICODETEXT
win32clipboard.CloseClipboard()
else:
data = ''
win32clipboard.CloseClipboard()
return data
def run(self):
# self.clipboard_set(recent_txt)
while True:
try:
# print(flag)
if flag == 0:
self.button.Enable(True)#灰掉start按钮
break
else:
self.button.Enable(False)
recent_txt = self.clipboard_get()#获取剪切板的内容
# replace_txt=
# print(recent_txt)
# 剪切板内容和上一次对比如有变动,再进行内容判断,判断后如果发现有指定字符在其中的话,再执行替换
if recent_txt not in contentlist:
self.listbox.Append(recent_txt)
contentlist.append(recent_txt)
else:
numindex = contentlist.index(recent_txt)
self.listbox.Select(numindex)
except:
continue
# 检测间隔(延迟0.2秒)
time.sleep(0.2)
完整代码: