欢迎加入学习交流QQ群:657341423
pywinauto基于Windows api的操作。以下一些限制条件:
Windows上支持的辅助功能技术列表:
Win32 API(backend=“win32”) - 一个默认后端现在
MFC,VB6,VCL,简单的WinForms控件和大多数旧的旧应用程序
MS UI自动化(backend=“uia”)
WinForms,WPF,Store应用程序,Qt,浏览器
注意:Chrome --force-renderer-accessibility在启动之前需要cmd标志。不支持自定义属性和控件,因为Python库限制。
不支持:Java AWT / Swing,GTK +,Tkinter。
详细可以看官网
https://pywinauto.readthedocs.io/en/latest/index.html
代码:
from pywinauto import application
app = application.Application()#实例化
app=app.connect(title_re="Cy", class_name="wxWindowNR")#指定程序,connect链接现运行的程序,star是启动程序
app['Cy']['Edit2'].TypeKeys('aaa')#输入值,不会清空原来的数据
app['Cy']['Edit3'].SetEditText('bbbb')
#app['Cy']['Edit3'].SetText('bbbb')
app['Cy']['Edit4'].set_edit_text('你好')
#3种输入值,与.TypeKeys区别在于,这个如果文本框禁止输入也可强制输入
a=app['Cy']['Edit1'].WindowText()#获取值
b=app['Cy']['Edit3'].texts()#获取值,返回一个数组
c=app['Cy']['Edit4'].text_block()#获取值
print (a)
print (b)
print (c)
#app['Cy'][u'启动'].click()#点击控件
#app['Cy'].close_alt_f4()#关闭窗口
代码中['Cy']['Edit1']
可以通过Spy++ Lite(UISpy.exe)等工具查找出控件的名称属性等。
软件代码:
import wx
class MyFrame ( wx.Frame ):
def __init__( self ):
wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(100,50), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
gSizer2 = wx.GridSizer( 0, 2, 0, 0 )
self.SetTitle(u'Cy')
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"方法", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
gSizer2.Add( self.m_staticText1, 0, wx.ALL, 5 )
m_comboBox3Choices = ["Seturl"]
self.m_comboBox3 = wx.ComboBox( self, wx.ID_ANY, m_comboBox3Choices[0], wx.DefaultPosition, wx.DefaultSize, m_comboBox3Choices, 0 )
gSizer2.Add( self.m_comboBox3, 0, wx.ALL, 5 )
self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"参数", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText2.Wrap( -1 )
gSizer2.Add( self.m_staticText2, 0, wx.ALL, 5 )
self.m_textCtrl13 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_textCtrl13, 0, wx.ALL, 5 )
self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, u"参数", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText3.Wrap( -1 )
gSizer2.Add( self.m_staticText3, 0, wx.ALL, 5 )
self.m_textCtrl14 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_textCtrl14, 0, wx.ALL, 5 )
self.m_staticText4 = wx.StaticText( self, wx.ID_ANY, u"参数", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText4.Wrap( -1 )
gSizer2.Add( self.m_staticText4, 0, wx.ALL, 5 )
self.m_textCtrl15 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_textCtrl15, 0, wx.ALL, 5 )
self.m_staticText5 = wx.StaticText( self, wx.ID_ANY, u"参数", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText5.Wrap( -1 )
gSizer2.Add( self.m_staticText5, 0, wx.ALL, 5 )
self.m_textCtrl16 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_textCtrl16, 0, wx.ALL, 5 )
self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"返回值", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText6.Wrap( -1 )
gSizer2.Add( self.m_staticText6, 0, wx.ALL, 5 )
self.m_textCtrl17 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_textCtrl17, 0, wx.ALL, 5 )
self.m_button1 = wx.Button( self, wx.ID_ANY, u"启动", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_button1, 0, wx.ALL, 5 )
self.m_button4 = wx.Button( self, wx.ID_ANY, u"执行", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_button4, 0, wx.ALL, 5 )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"创建", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_button2, 0, wx.ALL, 5 )
self.m_button3 = wx.Button( self, wx.ID_ANY, u"定制", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_button3, 0, wx.ALL, 5 )
self.m_textCtrl14.SetEditable(False)
self.m_textCtrl15.SetEditable(False)
self.m_textCtrl16.SetEditable(False)
self.SetSizer( gSizer2 )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
app=wx.App()
frame=MyFrame()
frame.Show()
app.MainLoop()