Mon, Nov 25, 1:34 AM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: running two scripts (with tkinter GUI) at the same time


tromnek ( ) posted Sat, 04 December 2004 at 7:29 PM · edited Sun, 24 November 2024 at 11:16 PM

When I have two scripts running each with a GUI (tkinter).

If I close one, the other crashes. I've been using (somewhat blindly) the application setup I generally see in Okham's stuff see big snippet below.

Is there something special I should do to fix this?
tia,
ken

...snip...
class App:
def init(self, master, textMessage):

self.master = master
master.title("TromNek's Library Executor")

self.buttonGo = Button(self.master, text="Load", command=self.handleGo)
self.buttonGo.grid(row=1, column=3)
...snip...

self.master.protocol('WM_DELETE_WINDOW', self.handleCancel)

def check_conn(self):
conn, (remotehost, remoteport) = sock.accept()
...snip...

def handleCancel(self):
self.master.destroy()

def Update(self):
try:
self.check_conn()
except:
pass
if scene: scene.ProcessSomeEvents(1)
root.lift()
root.after(100, self.Update)

Activate the loop

root = Tk()
app = App(root, '')
app.Update()
root.mainloop()

End TK loop.


duckmango ( ) posted Mon, 06 December 2004 at 12:17 PM · edited Mon, 06 December 2004 at 12:18 PM

It's just speculation, but perhaps Poser's Python interpreter is passing the WM_Delete_Window Message to both scripts, causing one to crash unexpectedly. There was a recent thread here (http://www.renderosity.com/messages.ez?ForumID=12390&Form.ShowMessage=1986945) about the lack of thread support in PoserPython, so perhaps that's related.

In regards to a workaround, can you combine the gui's into one script, perhaps using the Toplevel widget to house your second script? Toplevel works just like Frame, but creates a separate "toplevel" window where you can load other widgets into it, etc.

Then, after setting up your separate gui's, you can use just one repeater function to do your housekeeping, heavy lifting, etc. Just a thought...Good luck.

Message edited on: 12/06/2004 12:18


PoseWorks ( ) posted Mon, 06 December 2004 at 6:32 PM · edited Mon, 06 December 2004 at 6:34 PM

When you have two Tk apps, you should use Toplevel for the newest window.

I like to build my window classes with an argument to take a root window. That way I can feed them an appropriate root window:

from Tkinter import *

class NewWin:
-----def init(self, root_win=None):
----------if root_win:
---------------self.root = root_win
----------else:
---------------self.root = Tk()
---------------self.root.protocol('WM_DELETE_WINDOW',self.destroy)
-----def mainloop(self):
----------self.root.mainloop()
-----def destroy(self):
----------self.root.destroy()

if name == 'main':
-----test_win = NewWin()

-----test_win.top = Toplevel()
-----test_win.son_of_test_win = NewWin(test_win.top)

-----test_win.mainloop()

#PoserPython hates it when you end on a tabbed line

You end up with a few more lines of code, but it may be worth it in a multi-window environment. (Remember, any extra windows should ultimately be slaved under the master window [master.slave], that way you can be absolutely sure that everything gets destroyed once the master window is destroyed.)

Message edited on: 12/06/2004 18:34


tromnek ( ) posted Mon, 06 December 2004 at 8:21 PM

thanks all. My main problem is running other persons scripts with mine. Is there a way to get a brand new instance of Tk() so that independent scripts done destroy each other?


tromnek ( ) posted Mon, 06 December 2004 at 9:59 PM

My specific problem right now is that I want to run a background process to listen for an incomming socket connection. Threads seems to be broken so I can't

kickit=threading.timer( 1.0, check_for_connection).

I had my script running under tkinter, but had problems when I ran somebody else's script with tkinter, while my script was running.
Any Ideas?


PoseWorks ( ) posted Tue, 07 December 2004 at 12:17 AM

You could try setting a callback with after_idle([,args]) that would set WM_DELETE_WINDOW to a function of your choosing (one that didn't delete windows at all) after the alien script had gone to the mainloop. An example would be: ------------------- > def nullify(): > -----pass > def reset_x(master): #may need an event arg too, not sure > -----master.protocol('WM_DELETE_WINDOW',nullify) > master.after_idle(reset_x, master) > exec("script.py") ------------------- This has the unhappy side-effect of making the user unable to destroy windows through the system's close button though. If you know that the script that you want to execute is safe, you could do something like this: ------------------- file = open("script.py",'r') file_data = file.read() file.close() import re new_data = re.compile("""Tk()""").sub("Toplevel()",file_data) file = open("temp_script.py",'w') file.write(new_data) file.close() exec("temp_script.py") ------------------- But it's somewhat dangerous to monkey with someone else's code. What's the reason for trying to execute someone else's script, and would be consistently be the same script?


tromnek ( ) posted Tue, 07 December 2004 at 7:23 AM

hmmm.
I have a script that listens on a network socket for a connection from an external application. The external application sends filepath names to the script. If the filepath is a poser library file or a python script, it loads (library file) or executes (python script) it.
Since this script will be running all the time, I want to be able to load any other script while it's running (either sent over the network socket, or manually run in poser by the user).
I don't really need tkinter for my 'listening' script. I'd rather had used 'threading' (but it seems broken in poser).
I don't want to use the poser 'SetEventCallBack()' because it calls back excessively and it doesn't call back until I get back to poser from an external application.


mkrueger ( ) posted Tue, 07 December 2004 at 8:34 PM

Hi tromnek, the thread duckmango mentioned above was mine, I had the problem that the threading module wasn't even there in Poser, so I installed Python 2.2 and exchanged Poser's python22.dll with the standard Python one. This takes care of the availability of threading. I still have some issues with threads, but I suspect this is caused by the Tkinter mainloop. So if you don't need a GUI, you should be fine. BTW, does anyone know how to query if a Tkinter app has the focus or not? Martin


tromnek ( ) posted Tue, 07 December 2004 at 9:29 PM

I tried running my 2.2.3 version when I was trying to get the pythoncom to work. It seemed buggy. I even tried putting the whole 2.2.3 lib tree, but it still wasn't working good (maybe i'll try again). Anyway, I want normal pp and p5 poser users to be able to use my script. Seno Software (P3dO Explorer) want's to hook into it so their users can send poses (and other stuff) directly to poser. For that matter, any program could send poser file names directly to my script thru a standard tcp/ip socket. We could even have Poser and Shade7 talk to each other directly using a defined protocol. Thanks (maybe Poser 6 will have threads)


tromnek ( ) posted Fri, 21 January 2005 at 3:49 PM

Attached Link: http://www.renderosity.com/messages.ez?ForumID=12390&Form.ShowMessage=2087664

I found some neat stuff about this so, I posted a new thread on this topic.


Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.