import uiautomation,random
#找到计算器这个窗口
# window = uiautomation.WindowControl(searchDepth=1,Name="计算器")
# print(window)
#判断窗口是否存在
# if window.Exists():
# print("找到")
#置顶窗口
# window.SetTopmost(isTopmost=True)
# 最大化
# window.Maximize()
# 让窗口激活(从最小化返回桌面)
# window.SetActive()
# 返回左上角和右下角的坐标
# res = window.BoundingRectangle
# x=[res.left,res.right]
# y=[res.top,res.bottom]
# button = uiautomation.ButtonControl(searchDepth=1,Name="7")
# button.Click()
def test():
tester = uiautomation.WindowControl(searchDepth=1,Name="计算器")
if tester.Exists():
uiautomation.ButtonControl(searchDepth=4,Name="7").Click()
uiautomation.ButtonControl(searchDepth=4,Name="加").Click()
uiautomation.ButtonControl(searchDepth=4,Name="3").Click()
uiautomation.ButtonControl(searchDepth=4,Name="等于").Click()
res=uiautomation.TextControl(searchDepth=4,ControlType="ControlType.Text",AutomationId="158")
# print(type(res.Name))
if res.Name=="10":
print("成功")
def show():
tester = uiautomation.WindowControl(searchDepth=1,Name="计算器")
rad1=random.randint(1,9)
rad2=random.randint(1,9)
methods=["加","减","乘","除"]
rad_mtd=random.randint(0,3)
if tester.Exists():
uiautomation.ButtonControl(searchDepth=4,Name=str(rad1)).Click()
uiautomation.ButtonControl(searchDepth=4,Name=methods[rad_mtd]).Click()
uiautomation.ButtonControl(searchDepth=4,Name=str(rad2)).Click()
uiautomation.ButtonControl(searchDepth=4,Name="等于").Click()
res = uiautomation.TextControl(searchDepth=4,AutomationId="158")
# print(type(res.Name))
if rad_mtd==0:
if res.Name=="%d"%(rad1+rad2):
print("成功")
else:
print("失败")
print("%d%s%d=%d" % (rad1, methods[rad_mtd], rad2,rad1+rad2))
elif rad_mtd==1:
if res.Name=="%d"%(rad1-rad2):
print("成功")
else:
print("失败")
print("%d%s%d=%d" % (rad1, methods[rad_mtd], rad2, rad1 - rad2))
elif rad_mtd==2:
if res.Name=="%d"%(rad1*rad2):
print("成功")
else:
print("失败")
print("%d%s%d=%d" % (rad1, methods[rad_mtd], rad2, rad1 * rad2))
elif rad_mtd==3:
if round(float(res.Name),4)==round(rad1/rad2,4):
print("成功")
else:
print("失败")
print("%d%s%d=%f" % (rad1, methods[rad_mtd], rad2, rad1 / rad2))
else:
print("出错啦!!!")
if __name__ == '__main__':
# test()
for i in range(10):
show()