The module control_dialog.py provides a simplified interface to Tkinter dialogs.
The module auto_dialog.py is built on (that is it inherits behaviour) from module control dialog and extends functionality to allow automatic updating of the dialog when the data changes and also to automatically gather values from the dialog fields.
Unfortunately in order to achieve this self managed behaviour, the variables to be managed must be within a class.
This is one of the three layers which can be called from a menu item using the tkfront module.
from managed_dialog import *
def main():
# test code follows
def change_callback(dialog):
dialog.change()
print "change callback"
print "int_val:",var_group.int_val
print "float_val:",var_group.float_val
print "bool_val:",var_group.bool_val
print "str_val:",var_group.str_val
print "selector_val:",var_group.selector_val
print "list_val:",var_group.list_val
print "radio_val:",var_group.radio_val
def test_press():
print "button pressed"
root = Tk()
class var_class:
int_val = 1
float_val = 2.34574563
bool_val = True
str_val = "Hello"
selector_options = ("Aust","B","C")
selector_val = "B"
list_val = [False,True,True]
radio_val = 0
var_group = var_class()
min_val = 1.3
max_val = 3.4
var_list = (("Just some text>>>",),
("Test button",None,test_press),
("Test Float:",(var_group,'float_val')),
("Test Bool:",(var_group,'bool_val')),
("Test String:",(var_group,'str_val')),
(("Test selector box+-",)+var_group.selector_options,(var_group,'selector_val')),
(("Test list>>>","Sydney","Melbourne","Adelaide"),(var_group,'list_val')),
(("Test radio","One","Two","3"),(var_group,'radio_val')),
("Test Int:",(var_group,'int_val')))
status_line = "This is the status line"
dialog = ManagedDialog(root,var_list,"Control Dialog Test",change_callback,status_line,False)
frame = 0
while not dialog.exit_request:
dialog.update()
dialog.lift()
sys.stdout.flush()
dialog.setStatusLine('%10d' % frame)
frame += 1
dialog.destroy() # don't need to do this here but it is good practice
if __name__ == '__main__': main()
From the users point of view there is no difference in the behaviour. But for the programmer you will note that when the initial value is a tuple containing the parent class and a string with the member name, then this variable will be managed.
The change_callback(dialog) is necessary both to call the dialog(change) function and to inform the application that a change may have occurred.
The variable list protocol can be identical to that for controlDialog but those variables will not be managed. In order for a variable to be managed, the initial value must be replaced with a tuple containing the class instance and the member name of the variable to be managed.
As already shown in the example, it is necessary to call the change() function from the apply callback in order for the managed variables to be updated. It might have been possible to avoid this but this suite of dialog helpers is intended for real time applications, so the application needs to know that something has changed anyway - or it might forbid the change, for some reason, just by not calling change().
Apart from the essential constructor and the change()there are three useful functions:
First off grab the module managed_dialog.py here or the complete tkfront suite of modules as a zip file here. Test programs used on these webpages can also be downloaded here.
Installation is similar to control dialog except that both the auto_dialog.py and control_dialog.py modules must be in the path of your application.
Documentation can be generated by running python help("managed_dialog").
In case you didn't notice them, please heed the warnings on the microde page.
Copyright Robert Parker November 2009