That is exactly what you need to do. As I said before:
The key to using Tk is:
- First create all the widgets that you will use.
- Configure each widget to respond to various events as you desire.
- Start the main loop.
In this case:
- you want to create a Text or ROText widget to contain your text and start an repeat that will monitor your text file.
- The subroutine that the repeat calls should update the text display widget when it finds new text. It will need to insert the text at the end of the widget and also adjust the vertical scoll position.
Here's some very rough pseudocode.
# Create widgets.
mw = create_main_window
display = create_display(mw)
# open the file we monitor.
filehandle = open_file_for_monitoring('filename')
# set up monitor
mw->repeat( 1000, [monitor_file, filehandle, display ] )
# start main loop
MainLoop
sub monitor_file {
new_text = <filehandle>
display->insert('end', new_text)
display->adjust_vertical_scroll
}
It's very important to remember that the MainLoop is just a routine that looks something like:
sub MainLoop {
while (1) {
my $next_event = Get_Next_Event_From_Queue();
$next_event->execute;
}
}
So if your code takes a long time do ANYTHING, like it processes thousands of datapoints, reads large files, downloads a webpage or sleeps, then NO events will be processed, and the app will lock up.
Events are generated when you move the mouse, when you click, drag, move or resize widgets, on a scheduled basis, by subroutines. Any time you interact with the program, you are generating events. If you want your program to be responsive, it must be able to respond to any incoming events quickly.
The repeat you create works with the main loop by triggering an event at a scheduled interval. So if your repeat is set to do something every 10 seconds, at time = 10, it will generate an event that calls the subroutine you specified.
When you assign a subroutine to an event is is called "binding it to the event". So when a given event is triggered, your subroutine will be called. Until your routine returns, no other events can be processed, and the program will become unresponsive. That's why it is important to break your tasks up into very small, quickly executed chunks when working in an event driven environment like POE or Tk.
I hope this helps you to understand what exactly is going on in your Tk programs. Remember that you are not writing a script that proceeds in a straight line from start to stop. Instead you are setting up a system that must respond to events.
|