Python - capture a Button Click

Smart people ~ Great Scripts
User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.
Contact:

Python - capture a Button Click

Post by DesignDevil »

Hi together.

I go my first steps on scripting (Python + TrueSpace 6.6). I wrote a small script and now i try to bring this in a more usable form thrue a window with a Listbox and a few Buttons etc.

All samples that i could find on the net have just a modal dialog with Editboxes, a OK and a CANCEL button. But i need more buttons for more actions.

Creating all objects in the window is not the problem. But i can't find a script which captures the "actions" from these object like Button Clicks etc.

The first question is:
- How can i capture the "OnClick" Event from Buttons?
- Where can i find a source which Element (ListBox, EditBox, Button etc.) has which events?

-------------------

Another way could be to find a god and detailed tutorial about "writing TSX" for trueSpace 6.6. In this case i could try to write directly a TSX because to code is not my problem. I write programs in many languages (Delphi, BlitzBasic, FreeBasic, PureBasic, Visual Basic, PHP, JavaScript) - so i'm flexible. But the TS SDK is still a mystery for me.

I hope anybody can help - thanks a lot.
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6

User avatar
First Light
Senior Chief Petty Officer
Posts: 169
Joined: 22 May 2009, 11:17
Type the number ten into the box: 0
Location: Michigan, U.S.
Contact:

Re: Python - capture a Button Click

Post by First Light »

Hi DesignDevil,

I'm not sure if this will help, but I ran across a couple of old "ScriptManager" Python tutorials on the Caligari website. They are by Jonathan W. Taylor from 1998/1999 (trueSpace 4).

You probably already know this, but just in case you don't: The home for Python is www.python.org. They have lots of documentation and a "Community" link with info on other websites with help.

Finally, while Python isn't used much with trueSpace any more (trueSpace moved on to JScript and VBScript with version 7), there is a 3D program that still uses it as its main scripting language today: Blender. Therefore, you may find that www.blender.org is helpful.

Best regards, First Light

User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.
Contact:

Re: Python - capture a Button Click

Post by DesignDevil »

Thanks, so far.

Yes i read the tutorials of course but they not really helpful because they say no word about using Window Forms etc. Anyway, the most common problem is that the python version from TS6.6 is "a bit older" AND they use the MFC (Microsoft Foundation Classes) to create Objects like Windows, Buttons etc. (i believe)

In python is this MFC system maybe to diffcult but you can't find any example for this. The most python applications uses different Frameworks (wx or tinker) to create this.

To find older tutorials or examples is also very difficult. I searched the old TS Forum himself - which is extra difficult because it no longer exists - i have done this by using the "wayback machine" - but also i could not find good examples.

I'm a bit tired about that but maybe anyone can give me a introduction into writing "real" TSX's for TS than this would be the better way.

A type of "step by step" to your first Window with button would be cool (maybe also for other users).

Well, in TS7.x it's easier of course but........ i love my TS6.6 :)
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6

BooxieMalone
Chief Petty Officer
Posts: 127
Joined: 14 Jul 2009, 22:34

Re: Python - capture a Button Click

Post by BooxieMalone »

Hi DesignDevil,

maybe you know this already, otherwise this tutorial might be "the step by step" thing you're looking for - Writing TSX In C++ by Ken.

http://www.truespaceplugins.com/forum.h ... id=8&id=72" onclick="window.open(this.href);return false;

Regards
-BM.
.

User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.
Contact:

Re: Python - capture a Button Click

Post by DesignDevil »

hui ... thanks a lot BooxieMalone - that's exactly for what i'm looking for. (why the f**k could i not find this)
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6

clintonman
Captain
Posts: 3547
Joined: 21 May 2009, 21:08
Type the number ten into the box: 0
Location: California
Contact:

Re: Python - capture a Button Click

Post by clintonman »

Hi, Design Devil. I answered your email about this. Sorry I didn't have better news, but I was mistaken about python being completely broken in tS7.6 I put a fix in the bugs and workaround section so if you solve your python gui problem 7.61 users can benefit from your work.

Clinton
Clinton Reese

http://clintons3d.com

User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.
Contact:

Re: Python - capture a Button Click

Post by DesignDevil »

Thanks a lot Clintonman. Now i am so far that i can capture it. The comment lines in the "window.py" script was the magic thing. With that lines you have a Windows typical MessageHandler to get all Events.

For all others here which found this thread, here a code sample. Copy & Paste this into the Python Script Editor, Press the "start the script" Button. A Window appears with a few Elements. Click on the ADD Button to get the Eventmessage.

Code: Select all

import trueSpace
import win32con
import win32ui
import dialog

ButtonAdd = 35

dlgButton  = 128
dlgEdit    = 129
dlgStatic  = 130
dlgListbox = 131

doc = trueSpace.GetActiveDocument()
doc.ClearOutTxt()

def MakeDlgTemplate():
	style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT
	cs    = win32con.WS_CHILD | win32con.WS_VISIBLE
	s     = cs | win32con.WS_TABSTOP | win32con.WS_BORDER 

	dlg = [ ["Wheely", (0, 0, 110, 190), style, None, (8, "MS Sans Serif")], ]

	dlg.append([dlgStatic, "Objects:", -1, (5, 5, 100, 9), cs | win32con.SS_LEFT])						#Label
	dlg.append([dlgListbox, None, win32ui.IDC_LIST1, (5, 15, 100, 100), s | win32con.LBS_STANDARD])			#ListBox
	dlg.append([dlgButton, "Add", ButtonAdd, (5, 110, 30, 14), cs | win32con.BS_PUSHBUTTON])			#Button: Add

	dlg.append([dlgButton, "Del", win32ui.IDC_BUTTON2, (40, 110, 30, 14), cs | win32con.BS_PUSHBUTTON])			#Button: Del
	dlg.append([dlgButton, "Del All", win32ui.IDC_BUTTON3, (75, 110, 30, 14), cs | win32con.BS_PUSHBUTTON])		#Button: Del All

	dlg.append([dlgStatic, "Animation Range:", -1, (5, 135, 100, 9), cs | win32con.SS_LEFT])				#Label
	dlg.append([dlgStatic, "Start:", -1, (5, 150, 25, 9), cs | win32con.SS_LEFT])						#Label
	dlg.append([dlgStatic, "End:", -1, (60, 150, 25, 9), cs | win32con.SS_LEFT])						#Label

	dlg.append([dlgEdit, None, win32ui.IDC_EDIT1, (25, 148, 25, 12), s]) 										#Edit: Start
	dlg.append([dlgEdit, None, win32ui.IDC_EDIT2, (80, 148, 25, 12), s]) 										#Edit: End

	dlg.append([dlgButton, "Del Anim", win32ui.IDC_BUTTON4, (5, 170, 48, 14), cs | win32con.BS_PUSHBUTTON])		#Button: Delete Anim
	dlg.append([dlgButton, "Create Anim", win32ui.IDC_BUTTON5, (57, 170, 48, 14), cs | win32con.BS_PUSHBUTTON])	#Button: Create Anim

	return dlg

# subclass of dialog - title variable passes through to the init portion of class
class WheelyDlg(dialog.Dialog):
	Cancel = 0
	def __init__(self):
		dialog.Dialog.__init__(self, MakeDlgTemplate() )
		self.AddDDX(win32ui.IDC_EDIT1,'Start')
		self.AddDDX(win32ui.IDC_EDIT2,'End')
		self.DoModal()

	def OnInitDialog(self):
		edittext=self.GetDlgItem(win32ui.IDC_EDIT1)	
		edittext.SetWindowText("0")
		edittext=self.GetDlgItem(win32ui.IDC_EDIT2)	
		edittext.SetWindowText("0")

	def WindowProc(self, msg, wParam, lParam):
		# Button Events
		if msg == win32con.WM_COMMAND:
			#print wParam,"\n"
			if wParam == ButtonAdd:
				print "ADD Button\n"

		rc, lResult = self._obj_.OnWndMsg(msg, wParam, lParam)
		if not rc: lResult = self._obj_.DefWindowProc(msg, wParam, lParam)
		return lResult

	def OnOK(self):
		print "ok\n"

	def OnCancel(self):
		print "Cancel\n"
		self.Cancel = 1
		self._obj_.OnCancel()

def Main():
	res = WheelyDlg()
	if res.Cancel:
		return (None, None)
	else:
		return (res['Start'],res['End'])

#--------------------------------------------------------------------------------
# START
#--------------------------------------------------------------------------------
Start, End = Main()
print Start
print End
trueSpace.Stop()
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6

User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.
Contact:

Re: Python - capture a Button Click

Post by DesignDevil »

Here another try, Clintonman - you are here ? ;)

i try'd to create a "real" window - not a dialog. The problem: Python gives me the error: AttributeError WNDCLASS Traceback (innermost last):

It seems to be that WNDCLASS is not defined. Is it possible to resolve this problem?

Code: Select all

# Creates a task-bar icon.  Run from Python.exe to see the
# messages printed.
import trueSpace
import win32api
import win32ui
import win32con

class MainWindow:
 def __init__(self):
  message_map = {
   win32con.WM_DESTROY: self.OnDestroy,
  }
  # Register the Window class.
  wc = win32ui.WNDCLASS()
  hinst = wc.hInstance = GetModuleHandle(None)
  wc.lpszClassName = "PythonTaskbarDemo"
  wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
  wc.hCursor = LoadCursor( 0, win32con.IDC_ARROW )
  wc.hbrBackground = win32con.COLOR_WINDOW
  wc.lpfnWndProc = message_map # could also specify a wndproc.
  classAtom = win32ui.RegisterClass(wc)
  # Create the Window.
  style = win32con.WS_VISIBLE | win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
  self.hwnd = CreateWindow( win32ui.classAtom, "My Window", style, 100, 100, 100, 200, 0, 0, hinst, None)
  ShowWindow(self.hwnd, win32con.SW_SHOW)
  UpdateWindow(self.hwnd)

 def OnDestroy(self, hwnd, msg, wparam, lparam):
  win32ui.PostQuitMessage(0) # Terminate the app.

def main():
 w=MainWindow()
 win32ui.PumpMessages()

doc = trueSpace.GetActiveDocument()
doc.ClearOutTxt()
main()
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6

clintonman
Captain
Posts: 3547
Joined: 21 May 2009, 21:08
Type the number ten into the box: 0
Location: California
Contact:

Re: Python - capture a Button Click

Post by clintonman »

My error is KeyErrorTSP4: with copy and paste of your code.

I'm getting this error after adjusting the spacing in the code. I'm not familiar with the message map.:

AttributeErrorOnDestroyTraceback (innermost last):
File "C:\trueSpace761\tS\TSP3.py", line 39, in ?
main()
File "C:\trueSpace761\tS\TSP3.py", line 34, in main
w=MainWindow()
File "C:\trueSpace761\tS\TSP3.py", line 12, in __init__
message_map = {

Code: Select all

import sys
sys.path.append('PythonScripts')
# Creates a task-bar icon.  Run from Python.exe to see the
# messages printed.
import trueSpace
import win32api
import win32ui
import win32con

class MainWindow:
      def __init__(self):
      	message_map = { 
			win32con.WM_DESTROY: self.OnDestroy 
			}
      	# Register the Window class.
      	wc = win32ui.WNDCLASS()
      	hinst = wc.hInstance = GetModuleHandle(None)
      	wc.lpszClassName = "PythonTaskbarDemo"
      	wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
      	wc.hCursor = LoadCursor( 0, win32con.IDC_ARROW )
      	wc.hbrBackground = win32con.COLOR_WINDOW
      	wc.lpfnWndProc = message_map # could also specify a wndproc.
      	classAtom = win32ui.RegisterClass(wc)
      	# Create the Window.
      	style = win32con.WS_VISIBLE | win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
      	self.hwnd = CreateWindow( win32ui.classAtom, "My Window", style, 100, 100, 100, 200, 0, 0, hinst, None)
      	ShowWindow(self.hwnd, win32con.SW_SHOW)
      	UpdateWindow(self.hwnd)

def OnDestroy(self, hwnd, msg, wparam, lparam):
  win32ui.PostQuitMessage(0) # Terminate the app.

def main():
    w=MainWindow()
    win32ui.PumpMessages()

doc = trueSpace.GetActiveDocument()
doc.ClearOutTxt()
main()
Clinton Reese

http://clintons3d.com

clintonman
Captain
Posts: 3547
Joined: 21 May 2009, 21:08
Type the number ten into the box: 0
Location: California
Contact:

Re: Python - capture a Button Click

Post by clintonman »

I forgot to mention that first code with the dialog box works great for processing the windows messages. Good job. :bananacool2:
Clinton
Clinton Reese

http://clintons3d.com

Post Reply