Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
pywinauto example for drag-n-drop
# requirements: Python 2.7 or 3.4, pyWin32, pywinauto 0.5.0+
# download the repo:
# place this script to repo root folder
import sys, os
os.chdir(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]))) # running at repo root folder
import pywinauto
mfc_samples_folder = os.path.join(
os.path.dirname(sys.argv[0]), r"apps\MFC_samples")
if pywinauto.sysinfo.is_x64_Python():
mfc_samples_folder = os.path.join(mfc_samples_folder, 'x64')
app = pywinauto.Application.start(os.path.join(mfc_samples_folder, u"CmnCtrl1.exe"))
tree = app.Common_Controls_Sample.TreeView.WrapperObject()
birds = tree.GetItem(r'\Birds')
dogs = tree.GetItem(r'\Dogs')
# drag-n-drop without focus on the window
#tree.DragMouse("left", birds.Rectangle().mid_point(), dogs.Rectangle().mid_point())
# most natural drag-n-drop (with real moving mouse, like real user)
tree.DragMouseInput("left", birds.Rectangle().mid_point(), dogs.Rectangle().mid_point())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment