By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

returning control back to calling function

P: 3
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
Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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.
Nov 16 '06 #2

bartonc
Expert 5K+
P: 6,596
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.  
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.