使用python.net自动化Windows窗体对话框

1 投票
1 回答
800 浏览
提问于 2025-04-18 14:13

我正在用python.net写一个事件处理程序,这个程序会在弹出对话框时被调用,我希望这个事件处理程序能够自动回答这些对话框。

我想知道有没有办法在对话框对象内部直接回答Windows窗体的对话框(比如说像args.Dialog.DialogResult='OK'这样的方式),还是说我必须模拟人类的操作(也就是把对话框放到最前面,找到按钮的位置,然后模拟鼠标点击)。

我附上了我用来处理事件的示例代码(来自Agilent - http://nbviewer.ipython.org/github/jonnojohnson/Agilent/blob/master/Python_Automation/N5990A_Python_Automation.ipynb):

def my_DialogPopUp(source, args):
#args.Dialog.ShowDialog()  #Uncomment to show Valiframe dialog box for all cases
msgbox = args.DialogText

# Definition for args.DialogType
#Member name           Value   Description 
#Form                  0       General Form if the dialog is not one of the other dialog types  
#MessageBox            1       Standard Windows.Forms.MessageBox.  
#ConnectionDialog      2       Connection dialog. The dialog which pops up if a connection change is required.  
#UserInformationDialog 3       User Information Dialog, which contains a text and one button.  
#UserDecisionDialog    4       User Decision Dialog, which contains one text and 2 buttons.  
#InfoDialog            5       Info Dialog, which is the same as the UserInformationDialog (obsolete, will be removed in one of the next releases).  

if args.DialogType == 0:
        raw_input('General Form Dialog: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
elif args.DialogType == 1:
        raw_input('Standard Windows.Forms.MessageBox: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
elif args.DialogType == 2:
        raw_input('Connection Dialog: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
elif args.DialogType == 3:
        raw_input('UserInformationDialog: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
elif args.DialogType == 4:
        raw_input('UserDecisionDialog: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
elif args.DialogType == 5:
        raw_input('InfoDialog: '+str(msgbox))
        #  add actions here.....
        #  args.Dialog.ShowDialog() #Uncomment to show dialog box for this case
else:
        raw_input('Message not handled: '+str(msgbox))

1 个回答

0

你可以用 AutoPy 来自动化和表单、图形用户界面(GUI)、键盘和鼠标的互动。我不太确定怎么给你的GUI设置默认值,但你可以加一个计时器,如果在一定时间内没有任何操作,就自动执行你的决定...

撰写回答