python 基于win32clipboard、wxpython下的剪切板工具

在日常使用中,复制粘贴的使用频率甚高,复制了上一次的文本,没有保存只能在重新复制。由于这个原因,借此使用该库中的一些功能:

其实

具体需求如下:

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)
完整的图例

 

完整代码:

https://download.csdn.net/download/weixin_41341221/11777767

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值