Greetings, my master.<br><br>I'm writing a game based on curses.<br><br>I have my own screen object and several child objects to handle sub windows with e.g. menues, board/map/views and log outputs. All user input is done with
screen.getch and later sent to the dynamic menu for selecting menu points.<br><br>My imidiate problem is when I select "Quit" from the menu, I need to send the string back to the caller/parent class for evaluation.
<br><br>Later I will need to design all the menues and the related methods, other sub windows in different threads for individual updates. But first I need a working UI.<br><br>Off topic: I must say that I'm amazed by this tutor thing. To really have my "own" tutor in this new programming language I'm learning, is kinda blowing my mind. I hope I can repay the python community some day when I'm smart enough. :-)
<br><br>Thanks in advance<br><br>/karneevor<br><br><div class="gmail_quote">On Dec 29, 2007 6:39 PM, Alan Gauld <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>"Michael Bernhard Arp Sørensen" <<a href="mailto:michaelarpsorensen@stevnstrup.dk">michaelarpsorensen@stevnstrup.dk</a>><br></div>wrote<br><div class="Ih2E3d"><br>> I want to learn about callbacks because we use it at work in our
<br>> software.<br><br></div>Can you be more specific about what you want to know. Callbacks are<br>used in many different ways from event handling methods in a GUI<br>to network programming to simulating synchronous protocols over
<br>an asynchronous connection.<br><div class="Ih2E3d"><br>> I there a short "hello world"-like version of a callback example?<br><br></div>See almost any GUI tutorial.<br>The recent thread "Closing GUI program" had the following example
<br>from Michael Goldwasser<br><br>#-------------------------------------------<br>from Tkinter import Tk,Label<br><br>def onClose():<br> root.destroy() # stops the main loop and interpreter<br><br>root = Tk()<br>root.protocol
("WM_DELETE_WINDOW", onClose) # handle event when window<br>is closed by user<br>z = Label(root, text="Hello World!")<br>z.grid()<br>root.mainloop()<br>#-------------------------------------------<br>
<br><br>In this example the onClose() event handler is a callback function.<br><br>The folowing pseusdo code shows how the principle can be used for<br>asynchronous network programming:<br><br>waiting = {} # list of objects awaiting responses
<br>id = 0<br><br>def sendToServer(msg, callback)<br> msgSent = prepareMessage(msg)<br> id = server.send(msgSent)<br> waiting[id] = (msg, callback)<br><br>def func1()<br> msg = prepareData()<br> sendToServer(msg, func1_cont)
<br><br>def func1_cont(original, result)<br> x,y,z = result.getValues()<br> processData(x,y,z,original.p,original.q)<br><br>while server.listen()<br> msg = server.recv()<br> id = msg.getID()<br> oldMessage = waiting[id][0]
<br> callback = waiting[id][1]<br> callback(oldmessage, msg)<br> del(waiting[id])<br><br>In this example we can think of the main application calling func.<br>func1 needs to send a message to a server and process the response
<br>but the server has an asynchronous protocol so we split the function<br>into func1 and func1_cont at the point of calling the server. Then<br>when the server send us the response we pull the stored state out<br>of the dictionary and combine it with the server data to complete
<br>the func1 processing via the func1_cont callback.<br><br>In practice we'd probably store the date/time with the transaction<br>data so that we can check for timeouts etc in a separate thread...<br><br>The important thing with all callbacks is that you match up the
<br>data expected by the callback with the data actually available<br>at the point of calling it. In this case we take the architectural<br>decision to pass callback functions the old and new data structures.<br>We could alternatively have passed the transaction id and let the
<br>callback retrieve (and delete) the data from the waiting list.<br><br>I hope that all makes sense.<br><font color="#888888"><br>--<br>Alan Gauld<br>Author of the Learn to Program web site<br><a href="http://www.freenetpages.co.uk/hp/alan.gauld" target="_blank">
http://www.freenetpages.co.uk/hp/alan.gauld</a><br></font><div><div></div><div class="Wj3C7c"><br><br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org
</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br></div></div></blockquote></div><br><br clear="all"><br>-- <br>Med venlig hilsen/Kind regards
<br><br>Michael B. Arp Sørensen<br>Programmør / BOFH<br>I am /root and if you see me laughing you better have a backup.