Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Execute Python code in QM
#3
If the QM macro is a console exe, Python print() will write to its console.

Else:

Function PythonPrintToQM
Code:
Copy      Help
;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
Code:
Copy      Help
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
Code:
Copy      Help
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__


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)