Connecting Tech Pros Worldwide Help | Site Map

returning control back to calling function

Newbie
 
Join Date: Oct 2006
Location: chennai
Posts: 3
#1: Nov 16 '06
Hello friends,

I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I created the instance of this class and executed, I require the selected item to be sent back to the calling function, and the window should not be destroyed. When the button is pressed I require the control to get back to calling function and process further, but the problem is unless I close this tkinter window the control is not returned to the calling class. Can any one suggest solution how to handle this.






Description:

calling program:
1.create instance of tkinter class
2.get the selected item and process further.



Called tkinter class:
1. display the window,with items placed in list box, user selects the item,

require the control to be sent to step 2 of calling program when a button on tkinter class is pressed.

In general case,the control is only transferred when we close the tkinter gui window. Please suggest the solution.


regards,
sumanth
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#2: Nov 16 '06

re: returning control back to calling function


Quote:

Originally Posted by sumanthsclsdc

Hello friends,

I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I created the instance of this class and executed, I require the selected item to be sent back to the calling function, and the window should not be destroyed. When the button is pressed I require the control to get back to calling function and process further, but the problem is unless I close this tkinter window the control is not returned to the calling class. Can any one suggest solution how to handle this.






Description:

calling program:
1.create instance of tkinter class
2.get the selected item and process further.



Called tkinter class:
1. display the window,with items placed in list box, user selects the item,

require the control to be sent to step 2 of calling program when a button on tkinter class is pressed.

In general case,the control is only transferred when we close the tkinter gui window. Please suggest the solution.


regards,
sumanth

Your stipulation "the window should not be destroyed" makes it tough.
TKs mainloop() must run (and won't return). If you look at the way IDLE is set up, you'll set that it is possible to start a gui in a seperate thread and communicate with the calling process. That gives you the treading module, the queue module, etc to study. OR

In a word: data persistance (well two words). You can use pickle or sql database (or text file for that matter) saved by the gui. Then, after mainloop() returns when the window is destroyed, you can retrieve the data. Then recreat the window and call mainloop() when you need it again.
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#3: Nov 16 '06

re: returning control back to calling function


Quote:

Originally Posted by bartonc

Your stipulation "the window should not be destroyed" makes it tough.
TKs mainloop() must run (and won't return). If you look at the way IDLE is set up, you'll set that it is possible to start a gui in a seperate thread and communicate with the calling process. That gives you the treading module, the queue module, etc to study. OR

In a word: data persistance (well two words). You can use pickle or sql database (or text file for that matter) saved by the gui. Then, after mainloop() returns when the window is destroyed, you can retrieve the data. Then recreat the window and call mainloop() when you need it again.

In wxPython, the gui spawns the worker thead and wx.CallAfter() always runs in the main thread. Here is sample code form Robin Dunn's book (although I don't like all the practices used here, it does demo the threading module nicely):

Expand|Select|Wrap|Line Numbers
  1.  
  2. import wx
  3. import threading
  4. import random 
  5. class WorkerThread(threading.Thread):
  6.     """
  7.     This just simulates some long-running task that periodically sends
  8.     a message to the GUI thread.
  9.     """
  10.     def __init__(self, threadNum, window):
  11.         threading.Thread.__init__(self)
  12.         self.threadNum = threadNum
  13.         self.window = window
  14.         self.timeToQuit = threading.Event()
  15.         self.timeToQuit.clear()
  16.         self.messageCount = random.randint(10,20)
  17.         self.messageDelay = 0.1 + 2.0 * random.random()
  18.     def stop(self):
  19.         self.timeToQuit.set()
  20.     def run(self):
  21.         msg = "Thread %d iterating %d times with a delay of %1.4f\n" \
  22.              % (self.threadNum, self.messageCount, self.messageDelay)
  23.         wx.CallAfter(self.window.LogMessage, msg)
  24.         for i in range(1, self.messageCount+1):
  25.             self.timeToQuit.wait(self.messageDelay)
  26.             if self.timeToQuit.isSet():
  27.                 break
  28.             msg = "Message %d from thread %d\n" % (i, self.threadNum)
  29.             wx.CallAfter(self.window.LogMessage, msg)
  30.         else:
  31.             wx.CallAfter(self.window.ThreadFinished, self)
  32.  
Reply


Similar Python bytes