Graphical User Interfaces (GUIs) are the easy way to input data to an application sofware and to view data produced by the application. The management of data exchanges between GUI and application is a central problem in GUI programming, it absorbs a relevant part of the programming effort. AVC is a python module that makes the programming of this data exchanges very easy, it supports the principal widget toolkits.
AVC, the Application View Controller, is a fully transparent and automatic connector between the values displayed and entered by GUI widgets and the variables of an application using the GUI. The connection is bidirectional. If the application sets a new value into a connected variable, AVC copies the new value into all widgets connected to the variable. If a new value is entered by a widget, AVC copies the new value into all other widgets connected the variable, into the variable and optionally notifies the change to the application. The connections are autogenerated by looking for matching names between widget names and variable names.
The application is completely unaware of the presence of the connected variables, it reads and writes them as normal variables. Only if the application requires to be immediately notified when a connected variable changes value, a notify handler must be added to the application.
AVC is released under the GNU General Public License .
Current version is AVC 0.1.0, alpha status, released 10-Jan-2007.
Tested on Debian GNU/Linux Etch, FP 10-Jan-2007.
For any question, suggestion, contribution contact the author Fabrizio Pollastri, e-mail: pollastri (at) iriti.cnr.it.
Th AVC web site is hosted at http://avc.iriti.cnr.it/.
Essential instructions to get started with AVC. This instructions are for the GTK toolkit, the usage with the other supported toolkits is very similar. AVC module is supposed already installed. For full details, see the documentation pages.
Import the AVC module for GTK.
from avc.avcgtk import *Derive the application class from AVC class. Let suppose that the application class name is "theApp".
class theApp(AVC):Design the GUI with Glade or create it in the application, naming the widgets with the rule described below.
Define all variables to be connected in the application. Each variable must have a name equal to the matching name of the widgets that are to be connected to the variable. A widget matching name is the widget name itself, if it does not contain a double underscore '__', otherwise is the name part before the double underscore.
In the application, after the creation of GUI and all variables to be connected, call the instance method 'avc_init'. Let suppose that the application instance name is "the_app".
the_app.avc_init()All is done for AVC. From this point, AVC takes full control over data exchange between the connected variables and widgets.
Download AVC tarball from http://avc.iriti.cnr.it/dist/ or from http://cheeseshop.python.org/pypi.
To run AVC, Python 2.2 or later must already be installed. The latest release is recommended. Python is available from http://www.python.org/.
The first step is to expand the .tgz archive in a temporary directory (not directly in Python's site-packages). It contains a distutils setup file "setup.py".
Open a shell.
Unpack the tarball in a temporary directory (not directly in Python's site-packages). Commands:
tar zxf avc-X.Y.tar.gz
X and Y are the major and minor version numbers of the tarball.
Go to the directory created by expanding the tarball:
cd avc-X.Y
Get root privileges and install the package:
su
(enter root password)
python setup.py install
If the python executable isn't on your path, you'll have to specify the complete path, such as /usr/local/bin/python.
This is the part of the reference manual common to all supported widget toolkits: GTK, Qt3, Qt4.
AVC connects widgets and variables using a names matching procedure with the following rules.
The matching name for a variable is the variable name itself.
widget name | matching name |
button_ok | button_ok |
toggle__button | toggle |
check_button_1 | check_button_1 |
radio_button__2 | radio_button |
The matching name for a widget is the widget name itself, if the name does not contain a double underscore ('__'), otherwise the matching name is the part of the widget name before the double underscore. This allow to differentiate widget names for widgets that are to be connected to the same variable.
Each widget having a matching name equal to a variable matching name is connected to that variable.
A widget can be connected to one variable. A variable can be connected to one or more widgets.
The application that uses AVC must be instantiated from an application class that is derived from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(AVC):.
The AVC class is derived from the builtin object class that is the base of all new style classes introduced with python 2.2. So, also the application becomes a new style class.
AVC start its job just after it is initialized. AVC initialization can take place in the application after the creation of GUI and after the instantiation of all variables to be connected. AVC initialization is done by calling the instance method avc_init. Let suppose that the application instance name is "the_app", the AVC init statement will be the_app.avc_init():.
When the value of a connected variable is changed, the values displayed by the widgets connected to it are updated by AVC in one of two allowed modes: immediate or periodic. Mode selection is done at AVC initialization specifying the "view_period" argument. If the argument is omitted, like in the_app.avc_init():, it is assigned a default value of 0.1 seconds, selecting a periodic views update with that period. If the argument is assigned a value, like in the_app.avc_init(view_period=0.2):, views will be updated every "view_period" seconds. If the argument is assigned to zero or to "None" value, like in the_app.avc_init(view_period=0):, views will be updated immediately after each change of the variable value.
The memoryless press button, its connected variable must be a boolean. In normal state (button not pressed) the variable is "False", in pressed state (mouse pointer over button and mouse button 1 pressed) the variable is "True". Names for button widget in supported toolkits: GTK "Button", Qt3 and Qt4 "PushButton" with toggle attribute off.
The behaviour of the check button widget is the same of the toggle button widget. See toggle button. Names for check button widget in supported toolkits: GTK "CheckButton", Qt3 and Qt4 "CheckBox".
The text entry, its connected variable can be integer, float or string. Text input must conform to the type of the connected variable. If the connected variable is of type string, its value is copied to the entry widget "as is", if type is integer or float, the value is converted to string before copy. Names for text entry widget in supported toolkits: GTK "Entry", Qt3 and Qt4 "LineEdit".
The text label, its connected variable can be boolean, integer, float, string, list or tuple. If the connected variable is of type string, its value is copied to the label widget "as is", if type is boolean, integer or float, the value is converted to string before copy. If the label is created with a default text that is a valid python formatting string, this is saved by AVC and used to format the label text updates when the connected variable value changes. If the connected variable type is list or tuple, a valid python formatting string matching all the elements of list or tuple is mandatory in default lebel text.
The radio buttons come always in groups of two or more radio buttons. Each radio button behaves like a check button, but only one radio button at a time can be checked in each group. A variable of type integer can be connected to each group of radio buttons, its value is the index of the checked radio button in the group.
The spin button, its connected variable can be integer or float. The GTK "SpinButton" support both types. On the contrary, Qt3 and Qt4 differentiate integer or float support with two widgets: "SpinBox" and "DoubleSpinBox". Names for spin button widget in supported toolkits: GTK "SpinButton", Qt3 and Qt4 "SpinBox" for integer and "DoubleSpinBox" for float. Remember that in python floats are always doubles.
The toggle button, a button with memory, its connected variable must be a boolean. Each time the button is pressed, it changes its state: from on to off or viceversa. In off state the variable is "False", in on state the variable is "True". Names for toggle button widget in supported toolkits: GTK "ToggleButton", Qt3 and Qt4 "PushButton" with toggle attribute on.
This is the part of the reference manual specific to GTK widgets toolkit.
AVC GTK depends on PyGTK the python wrapper for GTK libraries. AVC GTK imports the following modules from PyGTK.
import gtk |
import gobject |
Glade a GTK allow duplicated naming of widgets.
This is the part of the reference manual specific to Qt3 widgets toolkit.
AVC Qt3 depends on PyQt v3 the python bindings for Qt v3 application framework. AVC Qt3 imports the following modules from PyQt.
import qt |
Qt3 Designer and Qt3 do not allow duplicated naming of widgets. So use the 'double underscore' mechanism to differentiate widgets names.
The application that uses AVC must be instantiated from an application class that is derived from the QApplication class and from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(QApplication,AVC):.
This is the part of the reference manual specific to Qt4 widgets toolkit.
AVC Qt4 depends on PyQt v4 the python bindings for Qt v4 application framework. AVC Qt4 imports the following modules from PyQt.
import PyQt4.Qt as qt |
Qt3 Designer and Qt3 do not allow duplicated naming of widgets. So use the 'double underscore' mechanism to differentiate widgets names.
The application that uses AVC must be instantiated from an application class that is derived from the QApplication class and from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(QApplication,AVC):.
This simple example shows how AVC can manage data
exchange from widget to widget without any specific code in the
application. The program creates a window with two widgets: a spin
button and a label. When the value in the spin button is changed by
clicking on up or down arrows or by entering it with the keyboard,
the new value is displayed into the label.
#!/usr/bin/python import gtk # gimp tool kit bindings import gtk.glade # glade bindings from avc.avcgtk import * # AVC for GTK GLADE_XML = 'example1_gtk.glade' # GUI glade descriptor class Example(AVC): "A spin button whose value is replicated into a label" def __init__(self): # create GUI self.glade = gtk.glade.XML(GLADE_XML) # autoconnect GUI signal handlers self.glade.signal_autoconnect(self) # the variable holding the spin button value self.spin_value = 0 def on_destroy(self,window): "Terminate program at window destroy" gtk.main_quit() #### MAIN example = Example() # instantiate the application example.avc_init() # connect widgets with variables gtk.main() # run GTK event loop until quit
The GUI layout was previously edited with Glade and saved to the file 'example1_gtk.glade'.
The key points of the example regarding AVC are the following.
Download the sample program example1_gtk.py
and the Glade descriptor example1_gtk.glade
This example shows how AVC can manage data
input from a check button widget to the application and from the
application to a label widget without any specific code in the
application. The program creates a window with two widgets: a check
button and a label. The label displays the value of an integer
counter. The check button controls the increment speed of the
counter. Initially, it is unchecked meaning that the increment speed
of the counter is 2 units per second. When the user checks the
check button the increment speed grows to 10 units per seconds and
returns to the initial value (2) when the check button is unchecked
again.
#!/usr/bin/python import gobject #-- import gtk #- gimp tool kit bindings import gtk.glade # glade bindings from avc.avcgtk import * # AVC for GTK GLADE_XML = 'example2_gtk.glade' # GUI glade descriptor LOW_SPEED = 500 #-- HIGH_SPEED = 100 #- low and high speed period (ms) class Example(AVC): """ A counter displayed in a label widget whose count speed can be accelerated by checking a check button. """ def __init__(self): # create GUI self.glade = gtk.glade.XML(GLADE_XML) # autoconnect GUI signal handlers self.glade.signal_autoconnect(self) # the counter variable and its speed status self.counter = 0 self.high_speed = False # start counter incrementer at low speed gobject.timeout_add(LOW_SPEED,self.incrementer) def incrementer(self): """ Counter incrementer: increment period = LOW_SPEED, if high speed is False, increment period = HIGH_SPEED otherwise. Return False to destroy previous timer. """ self.counter += 1 if self.high_speed: period = HIGH_SPEED else: period = LOW_SPEED gobject.timeout_add(period,self.incrementer) return False def on_destroy(self,window): "Terminate program at window destroy" gtk.main_quit() #### MAIN example = Example() # instantiate the application example.avc_init() # connect widgets with variables gtk.main() # run GTK event loop until quit
The GUI layout was previously edited with Glade and saved to the file 'example2_gtk.glade'.
The key points of the example regarding AVC are the following.
Download the sample program example2_gtk.py
and the Glade descriptor example2_gtk.glade
This example shows a table of all widget/variable type
combinations supported by AVC.
The program creates a window with three columns: the first shows
the type of the connected variable, the second shows all the widgets
that can be connected to that type of variable, the third shows the
current value of each variable.
Each row of the window represent a widgets/variable combination.
Row 1: memoryless button with boolean variable, pressed = True,
unpressed = False.
Row 2: buttons with memory, toggle and check buttons, pressed = True,
unpressed = False.
Row 3: radiobuttons numbered from 0 to 3, index variable = number
of checked radiobutton.
Row 4: integer input/output widgets, spin button and text entry.
Row 5: float input/output widgets, spin button and text entry.
Row 6: string input/output widget, text entry.
The text label widget is used in all output modes for the column of
the connected variable values.
The program increment the value of each connected variable looping
top-bottom at three rows per seconds. The user can also change the
connected variables interacting with the widgets.
#!/usr/bin/python import gobject #-- import gtk #- gimp tool kit bindings import gtk.glade # glade bindings from avc.avcgtk import * # AVC for GTK GLADE_XML = 'example3_gtk.glade' # GUI glade descriptor INCREMENTER_PERIOD = 333 # ms class Example(AVC): "A table of all supported widget/control type combinations" def __init__(self): # create GUI self.glade = gtk.glade.XML(GLADE_XML) # autoconnect GUI signal handlers self.glade.signal_autoconnect(self) # the control variables self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = '' # start variables incrementer increment = self.incrementer() gobject.timeout_add(INCREMENTER_PERIOD,increment.next) def incrementer(self): """ Booleans are toggled, radio button index is rotated from first to last, integer is incremented by 1, float by 0.5, string is appended a char untill maxlen when string is cleared. Return True to keep timer alive. """ while True: self.boolean1 = not self.boolean1 yield True self.boolean2 = not self.boolean2 yield True if self.radio >= 2: self.radio = 0 else: self.radio += 1 yield True self.integer += 1 yield True self.float += 0.5 yield True if len(self.string) >= 20: self.string = 'A' else: self.string += 'A' yield True def on_destroy(self,window): "Terminate program at window destroy" gtk.main_quit() #### MAIN example = Example() # instantiate the application example.avc_init() # connect widgets with variables gtk.main() # run GTK event loop until quit
The GUI layout was previously edited with Glade and saved to the file 'example3_gtk.glade'.
The key points of the example regarding AVC are the following.
widget | name |
---|---|
Row 1: | |
button | boolean1__button |
output value label | boolean1__var |
Row 2: | |
togglebutton | boolean2__togglebutton |
checkbutton | boolean2__checkbutton |
output value label | boolean2__var |
Row 3: | |
radiobutton0 | radio__radiobutton0 |
radiobutton1 | radio__radiobutton1 |
radiobutton2 | radio__radiobutton2 |
output value label | radio__var |
Row 4: | |
spinbutton | integer__spinbutton |
entry | integer__entry |
output value label | integer__var |
Row 5: | |
spinbutton | float__spinbutton |
entry | float__entry |
output value label | float__var |
Row 6: | |
entry | string__entry |
output value label | string__var |
self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = ''
Download the sample program example3_gtk.py
and the Glade descriptor example3_gtk.glade
This simple example shows how AVC can manage data
exchange from widget to widget without any specific code in the
application. The program creates a window with two widgets: a spin
button and a label. When the value in the spin button is changed by
clicking on up or down arrows or by entering it with the keyboard,
the new value is displayed into the label.
#!/usr/bin/python from qt import * # Qt interface from qtui import * # ui files realizer import sys # system support from avc.avcqt3 import * # AVC for Qt3 UI_FILE = 'example1_qt3.ui' class Example(QApplication,AVC): "A spin box whose value is replicated into a text label" def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = QWidgetFactory.create(UI_FILE) self.setMainWidget(self.root) self.root.show() # the variable holding the spinbox value self.spin_value = 0 #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_loop() # run Qt event loop until quit
The GUI layout was previously edited with Qt3 Designer and saved to the file 'example1_qt3.ui'.
The key points of the example regarding AVC are the following.
Download the sample program example1_qt3.py
and the UI descriptor example1_qt3.ui
This example shows how AVC can manage data
input from a check button widget to the application and from the
application to a label widget without any specific code in the
application. The program creates a window with two widgets: a check
button and a label. The label displays the value of an integer
counter. The check button controls the increment speed of the
counter. Initially, it is unchecked meaning that the increment speed
of the counter is 2 units per second. When the user checks the
check button the increment speed grows to 10 units per seconds and
returns to the initial value (2) when the check button is unchecked
again.
#!/usr/bin/python from qt import * # Qt interface from qtui import * # ui files realizer import sys # system support from avc.avcqt3 import * # AVC for Qt3 UI_FILE = 'example2_qt3.ui' LOW_SPEED = 500 #-- HIGH_SPEED = 100 #- low and high speed period (ms) class Example(QApplication,AVC): """ A counter displayed in a Label widget whose count speed can be accelerated by checking a check box. """ def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = QWidgetFactory.create(UI_FILE) self.setMainWidget(self.root) self.root.show() # the counter variable and its speed status self.counter = 0 self.high_speed = False # start counter incrementer at low speed self.timer = qt.QTimer(self) self.connect(self.timer,qt.SIGNAL("timeout()"),self.incrementer) self.timer.start(LOW_SPEED) def incrementer(self): """ Counter incrementer: increment period = LOW_SPEED, if high speed is False, increment period = HIGH_SPEED otherwise. """ self.counter += 1 if self.high_speed: period = HIGH_SPEED else: period = LOW_SPEED self.timer.stop() self.timer.start(period) #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_loop() # run Qt event loop until quit
The GUI layout was previously edited with Qt3 Designer and saved to the file 'example2_qt3.ui'.
The key points of the example regarding AVC are the following.
Download the sample program example2_qt3.py
and the UI descriptor example2_qt3.ui
This example shows a table of all widget/variable type
combinations supported by AVC.
The program creates a window with three columns: the first shows
the type of the connected variable, the second shows all the widgets
that can be connected to that type of variable, the third shows the
current value of each variable.
Each row of the window represent a widgets/variable combination.
Row 1: memoryless button with boolean variable, pressed = True,
unpressed = False.
Row 2: buttons with memory, toggle and check buttons, pressed = True,
unpressed = False.
Row 3: radiobuttons numbered from 0 to 3, index variable = number
of checked radiobutton.
Row 4: integer input/output widgets, spin button and text entry.
Row 5: float input/output widgets, spin button and text entry.
Row 6: string input/output widget, text entry.
The text label widget is used in all output modes for the column of
the connected variable values.
The program increment the value of each connected variable looping
top-bottom at three rows per seconds. The user can also change the
connected variables interacting with the widgets.
#!/usr/bin/python from qt import * # Qt interface from qtui import * # ui files realizer import sys # system support from avc.avcqt3 import * # AVC for Qt3 UI_FILE = 'example3_qt3.ui' INCREMENTER_PERIOD = 333 # ms class Example(QApplication,AVC): "A table of all supported widget/control type combinations" def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = QWidgetFactory.create(UI_FILE) self.setMainWidget(self.root) self.root.show() # the control variables self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = '' # start variables incrementer self.increment = self.incrementer() self.timer = qt.QTimer(self) self.connect(self.timer,qt.SIGNAL("timeout()"),self.timer_function) self.timer.start(INCREMENTER_PERIOD) def timer_function(self): self.increment.next() def incrementer(self): """ Booleans are toggled, radio button index is rotated from first to last, integer is incremented by 1, float by 0.5, string is appended a char untill maxlen when string is cleared. """ while True: self.boolean1 = not self.boolean1 yield True self.boolean2 = not self.boolean2 yield True if self.radio == 2: self.radio = 0 else: self.radio += 1 yield True self.integer += 1 yield True self.float += 0.5 yield True if len(self.string) >= 20: self.string = 'A' else: self.string += 'A' yield True #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_loop() # run Qt event loop until quit
The GUI layout was previously edited with Qt3 Designer and saved to the file 'example3_qt3.ui'.
The key points of the example regarding AVC are the following.
widget | name |
---|---|
Row 1: | |
button | boolean1__button |
output value label | boolean1__var |
Row 2: | |
togglebutton | boolean2__togglebutton |
checkbutton | boolean2__checkbutton |
output value label | boolean2__var |
Row 3: | |
radiobutton0 | radio__radiobutton0 |
radiobutton1 | radio__radiobutton1 |
radiobutton2 | radio__radiobutton2 |
output value label | radio__var |
Row 4: | |
spinbutton | integer__spinbutton |
entry | integer__entry |
output value label | integer__var |
Row 5: | |
spinbutton | float__spinbutton |
entry | float__entry |
output value label | float__var |
Row 6: | |
entry | string__entry |
output value label | string__var |
self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = ''
Download the sample program example3_qt3.py
and the UI descriptor example3_qt3.ui
This simple example shows how AVC can manage data
exchange from widget to widget without any specific code in the
application. The program creates a window with two widgets: a spin
button and a label. When the value in the spin button is changed by
clicking on up or down arrows or by entering it with the keyboard,
the new value is displayed into the label.
#!/usr/bin/python from PyQt4.QtCore import * # Qt core from PyQt4.QtGui import * # Qt GUI interface from PyQt4.uic import * # ui files realizer import sys # system support from avc.avcqt4 import * # AVC for Qt4 UI_FILE = 'example1_qt4.ui' class Example(QApplication,AVC): "A spin box whose value is replicated into a text label" def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = loadUi(UI_FILE) #self.root.show() # the variable holding the spin box value self.spin_value = 0 #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_() # run Qt event loop until quit
The GUI layout was previously edited with Qt4 Designer and saved to the file 'example1_qt4.ui'.
The key points of the example regarding AVC are the following.
Download the sample program example1_qt4.py
and the UI descriptor example1_qt4.ui
This example shows how AVC can manage data
input from a check button widget to the application and from the
application to a label widget without any specific code in the
application. The program creates a window with two widgets: a check
button and a label. The label displays the value of an integer
counter. The check button controls the increment speed of the
counter. Initially, it is unchecked meaning that the increment speed
of the counter is 2 units per second. When the user checks the
check button the increment speed grows to 10 units per seconds and
returns to the initial value (2) when the check button is unchecked
again.
#!/usr/bin/python from PyQt4.QtCore import * # Qt core from PyQt4.QtGui import * # Qt GUI interface from PyQt4.uic import * # ui files realizer import sys # system support from avc.avcqt4 import * # AVC for Qt4 UI_FILE = 'example2_qt4.ui' LOW_SPEED = 500 #-- HIGH_SPEED = 100 #- low and high speed period (ms) class Example(QApplication,AVC): """ A counter displayed in a Label widget whose count speed can be accelerated by checking a check box. """ def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = loadUi(UI_FILE) # the counter variable and its speed status self.counter = 0 self.high_speed = False # start counter incrementer at low speed self.timer = qt.QTimer(self) self.connect(self.timer,qt.SIGNAL("timeout()"),self.incrementer) self.timer.start(LOW_SPEED) def incrementer(self): """ Counter incrementer: increment period = LOW_SPEED, if high speed is False, increment period = HIGH_SPEED otherwise. """ self.counter += 1 if self.high_speed: period = HIGH_SPEED else: period = LOW_SPEED self.timer.stop() self.timer.start(period) #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_loop() # run Qt event loop until quit
The GUI layout was previously edited with Qt4 Designer and saved to the file 'example2_qt4.ui'.
The key points of the example regarding AVC are the following.
Download the sample program example2_qt4.py
and the UI descriptor example2_qt4.ui
This example shows a table of all widget/variable type
combinations supported by AVC.
The program creates a window with three columns: the first shows
the type of the connected variable, the second shows all the widgets
that can be connected to that type of variable, the third shows the
current value of each variable.
Each row of the window represent a widgets/variable combination.
Row 1: memoryless button with boolean variable, pressed = True,
unpressed = False.
Row 2: buttons with memory, toggle and check buttons, pressed = True,
unpressed = False.
Row 3: radiobuttons numbered from 0 to 3, index variable = number
of checked radiobutton.
Row 4: integer input/output widgets, spin button and text entry.
Row 5: float input/output widgets, spin button and text entry.
Row 6: string input/output widget, text entry.
The text label widget is used in all output modes for the column of
the connected variable values.
The program increment the value of each connected variable looping
top-bottom at three rows per seconds. The user can also change the
connected variables interacting with the widgets.
#!/usr/bin/python from PyQt4.QtCore import * # Qt core from PyQt4.QtGui import * # Qt GUI interface from PyQt4.uic import * # ui files realizer import sys # system support from avc.avcqt4 import * # AVC for Qt4 UI_FILE = 'example3_qt4.ui' INCREMENTER_PERIOD = 333 # ms class Example(QApplication,AVC): "A table of all supported widget/control type combinations" def __init__(self,sys_argv): # create GUI QApplication.__init__(self,sys_argv) self.root = loadUi(UI_FILE) self.root.show() # group all radio buttons into a button group. Button group not # managed by Qt4 Designer ?! self.radio_button0 = self.root.findChild(QWidget,'radio__button0') self.radio_button1 = self.root.findChild(QWidget,'radio__button1') self.radio_button2 = self.root.findChild(QWidget,'radio__button2') self.radio_button_group = QButtonGroup() self.radio_button_group.addButton(self.radio_button0,0) self.radio_button_group.addButton(self.radio_button1,1) self.radio_button_group.addButton(self.radio_button2,2) # the control variables self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = '' # start variables incrementer self.increment = self.incrementer() self.timer = QTimer(self) self.connect(self.timer,SIGNAL("timeout()"),self.timer_function) self.timer.start(int(INCREMENTER_PERIOD)) def timer_function(self): self.increment.next() def incrementer(self): """ Booleans are toggled, radio button index is rotated from first to last, integer is incremented by 1, float by 0.5, string is appended a char untill maxlen when string is cleared. """ while True: self.boolean1 = not self.boolean1 yield True self.boolean2 = not self.boolean2 yield True if self.radio == 2: self.radio = 0 else: self.radio += 1 yield True self.integer += 1 yield True self.float += 0.5 yield True if len(self.string) >= 20: self.string = 'A' else: self.string += 'A' yield True #### MAIN example = Example(sys.argv) # instantiate the application example.avc_init() # connect widgets with variables example.exec_() # run Qt event loop until quit
The GUI layout was previously edited with Qt4 Designer and saved to the file 'example3_qt4.ui'.
The key points of the example regarding AVC are the following.
widget | name |
---|---|
Row 1: | |
button | boolean1__button |
output value label | boolean1__var |
Row 2: | |
togglebutton | boolean2__togglebutton |
checkbutton | boolean2__checkbutton |
output value label | boolean2__var |
Row 3: | |
radiobutton0 | radio__radiobutton0 |
radiobutton1 | radio__radiobutton1 |
radiobutton2 | radio__radiobutton2 |
output value label | radio__var |
Row 4: | |
spinbutton | integer__spinbutton |
entry | integer__entry |
output value label | integer__var |
Row 5: | |
spinbutton | float__spinbutton |
entry | float__entry |
output value label | float__var |
Row 6: | |
entry | string__entry |
output value label | string__var |
self.boolean1 = False self.boolean2 = False self.radio = 0 self.integer = 0 self.float = 0.0 self.string = ''
Download the sample program example3_qt4.py
and the UI descriptor example3_qt4.ui© Copyright 2006-2007 by Fabrizio Pollastri