returning control back to calling function | Newbie | | Join Date: Oct 2006 Location: chennai
Posts: 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
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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.
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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): -
-
import wx
-
import threading
-
import random
-
class WorkerThread(threading.Thread):
-
"""
-
This just simulates some long-running task that periodically sends
-
a message to the GUI thread.
-
"""
-
def __init__(self, threadNum, window):
-
threading.Thread.__init__(self)
-
self.threadNum = threadNum
-
self.window = window
-
self.timeToQuit = threading.Event()
-
self.timeToQuit.clear()
-
self.messageCount = random.randint(10,20)
-
self.messageDelay = 0.1 + 2.0 * random.random()
-
def stop(self):
-
self.timeToQuit.set()
-
def run(self):
-
msg = "Thread %d iterating %d times with a delay of %1.4f\n" \
-
% (self.threadNum, self.messageCount, self.messageDelay)
-
wx.CallAfter(self.window.LogMessage, msg)
-
for i in range(1, self.messageCount+1):
-
self.timeToQuit.wait(self.messageDelay)
-
if self.timeToQuit.isSet():
-
break
-
msg = "Message %d from thread %d\n" % (i, self.threadNum)
-
wx.CallAfter(self.window.LogMessage, msg)
-
else:
-
wx.CallAfter(self.window.ThreadFinished, self)
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,366 network members.
|