使用wxPython获取系统剪贴板中的数据的教程

作者:goldensun 时间:2023-11-05 18:43:41 

涉及到开发桌面程序,尤其是文本处理,剪贴板就很常用,不像 java 中那么烦锁,wxpython 中访问剪贴板非常简单,寥寥几句足以。


# 取得剪贴板并确保其为打开状态
text_obj = wx.TextDataObject()
wx.TheClipboard.Open()
if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
 # do something...
 wx.TheClipboard.Close()

取值:


if wx.TheClipboard.GetData(text_obj):
 text = text_obj.GetText()

写值:


text_obj.SetText(‘要写入的值')
wx.TheClipboard.SetData(text_obj)

下面的例子中,点击 Copy 会将文本框中的值复制到剪贴板,点击 Paste 会将剪贴板中的文本粘贴到文本框中。


"""
Get text from and put text on the clipboard.
"""

import wx

class MyFrame(wx.Frame):
 def __init__(self):
   wx.Frame.__init__(self, None, title='Accessing the clipboard', size=(400, 300))

# Components
   self.panel = wx.Panel(self)
   self.text = wx.TextCtrl(self.panel, pos=(10, 10), size=(370, 220))
   self.copy = wx.Button(self.panel, wx.ID_ANY, label='Copy', pos=(10, 240))
   self.paste = wx.Button(self.panel, wx.ID_ANY, label='Paste', pos=(100, 240))

# Event bindings.
   self.Bind(wx.EVT_BUTTON, self.OnCopy, self.copy)
   self.Bind(wx.EVT_BUTTON, self.OnPaste, self.paste)

def OnCopy(self, event):
   text_obj = wx.TextDataObject()
   text_obj.SetText(self.text.GetValue())
   if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
     wx.TheClipboard.SetData(text_obj)
     wx.TheClipboard.Close()

def OnPaste(self, event):
   text_obj = wx.TextDataObject()
   if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
     if wx.TheClipboard.GetData(text_obj):
       self.text.SetValue(text_obj.GetText())
     wx.TheClipboard.Close()

app = wx.App(False)
frame = MyFrame()
frame.Show(True)
app.MainLoop()

标签:Python
0
投稿

猜你喜欢

  • Django框架实现在线考试系统的示例代码

    2021-05-24 23:07:00
  • JS实现动态添加外部js、css到head标签的方法

    2024-05-02 16:29:45
  • SQL语句练习实例之七 剔除不需要的记录行

    2024-01-17 21:15:04
  • 教你用eclipse连接mysql数据库

    2024-01-19 23:30:41
  • Pytorch中index_select() 函数的实现理解

    2023-11-26 16:24:32
  • IE7下 filter:Alpha(opacity=xx) 的小问题

    2008-12-02 16:24:00
  • Python爬虫——爬取豆瓣电影Top250代码实例

    2022-01-31 02:47:22
  • 便捷提取python导入包的属性方法

    2022-05-11 05:07:17
  • Python算法输出1-9数组形成的结果为100的所有运算式

    2022-05-02 22:45:48
  • Ubuntu16安装Python3.9的实现步骤

    2021-01-23 14:04:53
  • Web标准学习:CSS样式书写风格

    2008-03-25 09:37:00
  • js拦截alert对话框另类应用

    2024-04-16 09:53:52
  • Python实现自动装机功能案例分析

    2022-05-16 12:35:48
  • Python3中的2to3转换工具使用示例

    2022-04-26 17:33:36
  • python学习实操案例(二)

    2022-09-04 01:36:41
  • 在SQL Server数据库开发中的十大问题

    2008-12-18 14:39:00
  • Go语言题解LeetCode下一个更大元素示例详解

    2024-05-21 10:25:33
  • Python编程入门的一些基本知识

    2023-09-07 00:07:24
  • js读写COOKIE实现记住帐号或密码的代码(js读写COOKIE)

    2024-04-18 10:11:12
  • matplotlib绘图实例演示标记路径

    2021-10-18 08:51:04
  • asp之家 网络编程 m.aspxhome.com