07-03-2016, 11:16 AM
If the QM macro is a console exe, Python print() will write to its console.
Else:
Function PythonPrintToQM
Macro test PythonPrintToQM
Or alternative:
Macro Python print output to QM - alternative
Else:
Function PythonPrintToQM
;Redirects Python print() function output to QM output.
;REMARKS
;print() will be redirected in all Python code added by PythonExec or PythonAddCode that are called after calling this function in current thread.
;EXAMPLE
;PythonPrintToQM
;PythonExec ""
;
;#ret
;print("test")
;print("") # empty line
;print("one\ntwo") # multiline
;print(2) # number
;print("list", 1, 2, 3)
;print("list2", 1, 2, 3, sep=", ")
PythonAddCode ""
#ret
import sys
import ctypes
import win32gui
def printQM(*a, sep = ' '):
,s = ""
,for v in a:
,,if(s != ""): s += sep
,,s += str(v)
,
,if(printQM.hwndQM == 0): printQM.hwndQM=win32gui.FindWindow("QM_Editor", None)
,
,ctypes.windll.user32.SendMessageW(printQM.hwndQM, 12, -1, s)
printQM.hwndQM = 0
print = printQM
Macro test PythonPrintToQM
out
PythonPrintToQM
PythonExec ""
#ret
print("test")
print("") # empty line
print("one\ntwo") # multiline
print(2) # number
print("list", 1, 2, 3)
print("list2", 1, 2, 3, sep=", ")
Or alternative:
Macro Python print output to QM - alternative
out
PythonExec ""
#ret
def Script():
,print("test")
,print("") # empty line
,print("one\ntwo") # multiline
,print(2) # number
# ---------------------------------------------------------------------------------
# Put your Python script in the Script() function. Don't need to change other code.
import sys
import ctypes
import os
import win32gui
class PrintRedirector(object):
,def __init__(self):
,,self.hwndQM=win32gui.FindWindow("QM_Editor", None)
,
,def write(self, message):
,,if(message=='\n'): return
,,ctypes.windll.user32.SendMessageW(self.hwndQM, 12, -1, message)
sys.stdout = PrintRedirector()
try: Script()
finally: sys.stdout = sys.__stdout__