The knoda scripting tutorial

Horst Knorr

knoda, or more exactly the hk_classes library, has an integrated python interpreter. For further information about python see http://www.python.org. This tutorial will not teach python, it will only show the hk_classes/python interface. This tutorial is by no means complete, it only exists now because the final documentation hasn't been written yet.

The examples in this tutorial are based on the tables as described in the knoda tutorial.


Table of Contents
1. The various actions
2. General object methods- the class hk_visible
3. Data sensitive methods
Objects of type hk_dsvisible - the class hk_datasource
Objects of type hk_dsdatavisible - the class hk_column
Objects of type hk_presentation - forms and reports
Handling forms
Handling reports
4. using hk_classes as a Python module
hk_drivermanager
hk_connection
hk_database
5. Tips and tricks
How to display the data
How to execute a query
How to create a table
How to create a calculated value field
How to define macros
How to format numbers
How to print rows in alternating colours
How to set the focus in a form
How to react on pressed keys
How to load a file content into a field using a filedialog
List of Figures
2-1. Geometry specific methods
2-2. Look and Feel methods
2-3. Miscelleanous methods
2-4. Predefined colour names
3-1. hk_classes overview
3-2. description of the most important classes
3-3. hk_datasource methods
3-4. hk_column data methods
3-5. hk_column type methods
3-6. hk_dsdatavisible methods
3-7. Type casting
5-1. hk_column type methods
List of Examples
1-1. Hello world-program
2-1. Changing colour and position
3-1. rowposition
3-2. find a specific column
3-3. Read data
3-4. Write data
3-5. Search data
3-6. Read data
3-7. Write data
3-8. Search data
3-9. displaying a form
3-10. displaying a report
3-11. reportdata onprint
4-1. Python module basics
5-1. Show data
5-2. Execute a "SELECT" query
5-3. Execute an action query
5-4. create table
5-5. calculated values
5-6. Macro example
5-7. Macro example (part II)
5-8. Formatting a number
5-9. Printing alternate colours
5-10. Setting the taborder
5-11. Setting the taborder for a specific field
5-12. Reacting on keyboard input
5-13. Loading a file content into a field

Chapter 1. The various actions

Each object in a form has its own programmable actions. General actions are

On click

script that will be executed when a mouse click on this object occurs

On doubleclick

script that will be executed when a mouse doubleclick on this object occurs

On open

script that will be executed when the form is created or put into viewmode

On close

script that will be executed when the form is closed or put into designmode

On getfocus

script that will be executed when this object gets the keyboard focus

On loosefocus

script that will be executed when this object looses the keyboard focus

On key

script that will be executed when this object has the keyboard focus and a key on the keyboard is pressed

Let's write a 'hello world' program. Create a new form and add a button. Select the button and click on the 'On click'-actionbutton in the property editor. Now add the following program into the program editor

Example 1-1. Hello world-program

hk_this.show_warningmessage("Hello world")
Now close the window and change to viewmode. When you push the button you will see a dialog window with "Hello world".

The global variable hk_this is set from hk_classes and represents the current object, in this case the button. show_warningmessage() is a method of this object, that allows you to display a string in a GUI independent way (as knoda is a KDE application it will be displayed as a KDE window but if you start it within a command line program it will be displayed on the standard output).

Important: Other GUI-independent ways for user interaction are

show_warningmessage(message)

displays 'message'

bool show_yesnodialog(message, bool default_value)

displays 'message' and returns true if you answer 'yes'. The parameter 'default_value' is used for conevience to preselect 'yes' or 'no' in dialog boxes

hk_string show_stringvaluedialog(text)

displays 'text' as a question and returns your response

In forms another global variable is hk_thisform. It represents the form. In reports the variable name is hk_thisreport.


Chapter 2. General object methods- the class hk_visible

Each visible object of hk_classes is inherited from a class called hk_visible, so we will now have a look on the general available methods

Figure 2-1. Geometry specific methods

set_size(x, y,width,height)

lets you to set position and size of an object

set_size(width,height)

lets you to set size of an object

set_position(x,y)

lets you to set position of an object

set_x(x)

lets you to set the horizontal position of an object

set_y(y)

lets you to set the vertical position of an object

set_width(width)

set_height(height)

x()

returns the x co-ordinate of the object

y()

returns the y co-ordinate of the object

width()

returns the width of the object

height()

returns the height of the object

Figure 2-2. Look and Feel methods

set_font(fontname,size)

sets the font, e.g. set_font("Arial",12)

set_font(font)

hk_font font()

returns a font object

set_foregroundcolour(colour)

foregroundcolour()

set_backgroundcolour(colour)

hk_colour backgroundcolour()

Figure 2-3. Miscelleanous methods

set_label(labeltext)

label()

enum enum_visibletype {textlabel,button,rowselector,boolean,lineedit,memo,combobox,grid,form,report,reportsection,reportdata,other}

enum_visibletype type(void)

identifier()

hk_presentation* presentation()

returns the parent presentation object (either a form or a report)

The following example shows how to move the button within the form, how to change the colour and how to display different text on the button.

Example 2-1. Changing colour and position

redcolour =hk_colour(255,0,0)
greencolour =hk_colour(0,255,0)
if hk_this.foregroundcolour().red()!=255:
	hk_this.set_foregroundcolour(redcolour)
	hk_this.set_backgroundcolour(greencolour)
	hk_this.set_label("green button")
else:
	hk_this.set_foregroundcolour(greencolour)
	hk_this.set_backgroundcolour(redcolour)
	hk_this.set_label("red button")

hk_this.set_position(hk_this.x()+50,hk_this.y()+10)

Figure 2-4. Predefined colour names

  • hk_aqua

  • hk_beige

  • hk_black

  • hk_blue

  • hk_brown

  • hk_fuchsia

  • hk_darkgrey

  • hk_green

  • hk_grey

  • hk_lightgrey

  • hk_lime

  • hk_maroon

  • hk_navy

  • hk_olive

  • hk_orange

  • hk_pink

  • hk_purple

  • hk_red

  • hk_silver

  • hk_teal

  • hk_white

  • hk_yellow


Chapter 3. Data sensitive methods

Before we start writing programs, we will first have some theory. Below you see the (c++)- structure of hk_classes. There are classes that handle the contact to the database (see the left side of the graphic), while others handle the interaction with the user (we have already seen the hk_visible class in the previous section).

Figure 3-1. hk_classes overview

Figure 3-2. description of the most important classes


Objects of type hk_dsvisible - the class hk_datasource

All data-sensitive objects are of type hk_dsvisible (or its child hk_dsdatavisible). hk_dsvisible inherits from hk_visible. The most important method of hk_dsvisible is datasource(). This method returns a class of type hk_datasource, representing the whole datasource (either a table or a query)

Figure 3-3. hk_datasource methods

name()

returns the name of the datasource

goto_row(rownumber)

moves the row selector (row cursor) to 'rownumber'

goto_first()

goto_last()

goto_next()

goto_previous()

row_position()

returns the row number of the current row

max_rows()

returns the total number of existing rows

enable()

disable()

set_enabled(e)

is_enabled()

hk_column*column_by_name(name)

returns an hk_column object of the column with the name 'name'

store_changed_data()

hk_database*database()

The following example shows how to move between rows in a datasource. For this, create a button in the form and set a datasource. Add the script to the "On click"-action.

Example 3-1. rowposition

ds=hk_this.datasource()
ds.goto_row(ds.row_position() + 2)
How to get a specific hk_column object can be seen in the following example. For what you can do with this object, please see the next chapter.

Example 3-2. find a specific column

col=hk_this.datasource().column_by_name("author")


Objects of type hk_dsdatavisible - the class hk_column

For the next example, create a form, set a datasource, create a lineedit field and connect it to a column of the datasource (for details see the knoda tutorial at http://hk-classes.sourceforge.net/tutorials.

A lineeditfield is of type hk_dsdatavisible. hk_dsdatavisible inherits from hk_dsvisible. The most important method of hk_dsdatavisible is column(). This method returns a class of type hk_column, representing a specific column of a table.

Figure 3-4. hk_column data methods

set_asstring(value)

lets you set a new value for this object

asstring

returns the current value as a string value

set_asdouble(value)

lets you set a new value for this object

asdouble()

returns the current value as a double value

set_asinteger(value)

lets you set a new value for this object

asinteger()

returns the current value as a integer value

is_readonly()

returns true if this column is read-only; if data can be changed it returns false

unsigned int find(from_rownumber,to_rownumber,searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)

searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found

unsigned int find(searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)

searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.

hk_datasource* datasource()

hk_dsdatavisible contains the convenience function set_value(const hk_string&) which displays the value and sets the value by calling hk_column.set_asstring(const hk_string&) if the column is set. The convenience function hk_string value() returns the currently displayed value.

Figure 3-5. hk_column type methods

hk_string name(void)

void set_name(const hk_string& n)

sets the column name

enum_columntype {textcolumn,auto_inccolumn,smallintegercolumn,integercolumn,smallfloatingcolumn, floatingcolumn,datecolumn,datetimecolumn,timecolumn,timestampcolumn,binarycolumn, memocolumn,boolcolumn,othercolumn}

columntype()

returns the type of the column

size()

returns the column size (e.g. if this column was created as CHAR(10) it will return 10)

bool is_primary(void)

returns true if this column is part of a primary key

bool set_primary(bool i)

bool is_notnull(void)

bool set_notnull(bool i)

if true the column needs a value

Example 3-3. Read data

col=hk_this.datasource().column_by_name("name")
hk_this.show_warningmessage(col.asstring())

Example 3-4. Write data

col=hk_this.datasource().column_by_name("name")
col.set_asstring("my new value")
This changes the value of the current column. The data is saved either when

  • the row position changes (e.g. by calling hk_datasource.goto_row())

  • the datasource is disabled (e.g. by calling hk_datasource.disable())

  • the changes are manually stored by calling hk_datasource.store_changed_data()

Example 3-5. Search data

col=hk_this.datasource().column_by_name("name")
result=col.find("Schiller")
if result > hk_this.datasource().max_rows():
     hk_this.show_warningmessage("value not found")
else:
     hk_this.show_warningmessage("Value found at row position: "+str(result))
As this is working well, it is a bit inconvenient to handle. For this hk_dsdatavisible provides some convenience functions:

Figure 3-6. hk_dsdatavisible methods

set_value(newvalue)

sets the current value,where 'value' is a string. If a column is set, the datasource will be changed, if not it will be only displayed

value()

returns the displayed string (the current value)

find(from_rownumber,to_rownumber,searchtext[,wholephrase[,casesensitive[,backwards]]])

searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found

find(searchtext[,wholephrase[,casesensitive[,backwards]]])

searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.

The same examples as before, but now only using the hk_dsdatavisible functions:

Example 3-6. Read data

hk_this.show_warningmessage(hk_this.value())

Example 3-7. Write data

hk_this.set_value("my new value")

Example 3-8. Search data

result=hk_this.find("Schiller")
if result > hk_this.datasource().max_rows():
     hk_this.show_warningmessage("value not found")
else:
     hk_this.show_warningmessage("Value found at row position: "+str(result))


Objects of type hk_presentation - forms and reports

Both forms and reports inherit from hk_presentation. The most import function of hk_presentation is set_mode(mode). The two possible modes are hk_presentation.designmode and hk_presentation.viewmode


Handling forms

If you want to get a reference to a specific object in a form you have two possibilities

  • hk_visible* get_visible(unique_number)

  • hk_visible* get_visible(const hk_string& identifier)

The first function needs the unique identifier number of the wanted object. Each visible object of type hk_visible has a unique number as an identifier, which can't be changed.

Important: To find this number, click on the object. In the caption of the property editor you can see the number in brackets.

The second function uses a unique text identifier. This is a user-defined unique identifier and can be set in the property editor.

Both functions return a reference of type hk_visible. To change it to the type you need there are some type casting functions

Figure 3-7. Type casting

  • hk_button *cast_button(hk_visible*);

  • hk_dslineedit *cast_dslineedit(hk_visible*);

  • hk_dsmemo *cast_dsmemo(hk_visible*);

  • hk_dsgrid *cast_dsgrid(hk_visible*);

  • hk_dscombobox *cast_dscombobox(hk_visible*);

  • hk_dsboolean *cast_dsboolean(hk_visible*);

  • hk_dsvisible *cast_dsvisible(hk_visible*);

  • hk_form *cast_form(hk_visible*);

  • hk_report *cast_report(hk_visible*);

The next program shows you how to start a form:

Example 3-9. displaying a form

myform=hk_this.datasource().database().new_formvisible()
myform.load_form("authorform")
myform.set_mode(myform.viewmode)


Handling reports

Here is how to start a report:

Example 3-10. displaying a report

myreport=hk_this.datasource().database().new_reportvisible()
myreport.load_report("complexreport")
myreport.set_mode(myreport.viewmode)

A visible object in a report is of type hk_reportdata, which inherits from hk_dsdatavisible. The main methods are

  • set_data(const hk_string& d)

  • hk_string data(void)

The data property contains the value that is displayed. See the knodatutorial chapter "The report field" for details.

The following example shows how to print numbers in different colours. For this we use the "onprint" action

Example 3-11. reportdata onprint

value=hk_this.column().asdouble()
if value<0:
   hk_this.set_foregroundcolour(hk_red)
else:
   if value==0:
       hk_this.set_foregroundcolour(hk_black)
   else:
      hk_this.set_foregroundcolour(hk_blue)


Chapter 4. using hk_classes as a Python module

You can use hk_classes within Python. You can write your own Python applications using all the elements of hk_classes or interactively explore your data.

Example 4-1. Python module basics

horst@horstnotebook:~> python
Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
>>> dr=hk_drivermanager()
>>> con=dr.new_connection("mysql")
>>> con.set_password("mypassword")
>>> con.connect()
1
>>> db=con.new_database("exampledb")
>>> mytable=db.new_table("authors")
>>> mytable.enable()


hk_drivermanager

hk_drivermanager handles the database drivers. You need just one object of this type for your whole application.

  • vector<hk_string>* driverlist(void): returns a list of all available database drivers

  • hk_connection* new_connection(const hk_string& drivername): creates a new object of type hk_connection;


hk_connection

hk_connection connects to the SQL Server. The most important functions are set_host(), set_user(), set_password() and connect().

void set_host(const hk_string& )

sets the host name or host IP number

hk_string host(void)

void set_user(const hk_string& )

sets the user name used on the host

hk_string user(void)

void set_password(const hk_string& p)

sets the password for the user

void set_tcp_port(unsigned int t)

sets the TCP port

unsigned int tcp_port(void)

virtual unsigned int default_tcp_port(void) const

returns the default TCP port for this database server

bool connect(enum_interaction c=interactive)

connects to the server using the user, host and TCP data

bool disconnect(void)

disconnects from the server

bool is_connected(void)

returns true if this connection is connected to the server

vector<hk_string>* dblist(void)

returns a list of all existing databases in this connection

hk_database* new_database(const hk_string& name="")

creates a new hk_database object

bool delete_database(const hk_string& dbase)

deletes an exisiting database

bool database_exists(const hk_string& databasename)

returns true if the database "databasename" exists


hk_database

hk_database represents a particular database on the SQL Server

vector<hk_string>* tablelist(void)

returns a list of all existing tables in this database

vector<hk_string>* querylist(void)

returns a list of all existing queries in this database

vector<hk_string>* formlist(void)

returns a list of all existing forms in this database

vector<hk_string>* reportlist(void)

returns a list of all existing reports in this database

hk_datasource* new_table(const hk_string& name="",hk_presentation* p=NULL)

gets a new table object of type hk_datasource (read and write)

hk_datasource* new_resultquery(hk_presentation* p=NULL)

gets a new query object of type hk_datasource (readonly)

hk_actionquery* new_actionquery(void)

gets a hk_actionquery object. It can execute SQL statements that don't return data and are only successful or not successful (e.g. CREATE TABLE)

hk_datasource* load_datasource(const hk_string& name,bool query=false,hk_presentation* p=NULL)

a convenience function for new_table and new_resultquery, that loads an existing datasource

bool delete_table(const hk_string& table,enum_interaction x=interactive)

deletes a table

bool table_exists(const hk_string& tablename)

returns true if the table 'tablename' exists

bool query_exists(const hk_string& queryname)

returns true if the query 'queryname' exists


Chapter 5. Tips and tricks


How to display the data

Example 5-1. Show data

horst@horstnotebook:~> python
Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
>>> dr=hk_drivermanager()
>>> con=dr.new_connection("mysql")
>>> con.set_password("secret")
>>> con.connect()
1
>>> db=con.new_database("exampledb")
>>> table=db.new_table("authors")
>>> i=0
>>> table.enable()
SQL : SELECT  *  FROM  `authors`
1
>>> table.goto_first()
1
>>> while i< table.max_rows():
    ...    table.show_currentrow()
    ...    table.goto_next()
    ...    i=i+1
['1', 'Goethe,Johann Wolfgang', '1749', '1832', 'FALSE']
1
['2', 'Schiller, Friedrich von', '1759', '1805', 'TRUE']
1
['3', 'Lessing, Gotthold Ephraim', '1729', '1781', 'TRUE']
1
['4', 'Kleist', '1400', '0', 'FALSE']


How to execute a query

Result queries (SELECT statement) return data when they are executed.

Example 5-2. Execute a "SELECT" query

horst@horstnotebook:~> python
Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
d>>> dr=hk_drivermanager()
>>> con=dr.new_connection("mysql")
>>> con.set_password("secret")
>>> con.connect()
1
>>> db=con.new_database("exampledb")
>>> query=db.new_resultquery()
>>> query.set_sql("SELECT * FROM authors")
1
>>> query.enable()
SQL : SELECT  *  FROM  authors
1
Many queries, such as data definition queries, don't return data. They only report whether execution of the query was successful or whether it failed.

Example 5-3. Execute an action query

horst@horstnotebook:~> python
Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
d>>> dr=hk_drivermanager()
>>> con=dr.new_connection("mysql")
>>> con.set_password("secret")
>>> con.connect()
1
>>> db=con.new_database("exampledb")
>>> query=db.new_actionquery()
>>> query.set_sql("CREATE TABLE `another new table` 
( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) )")
>>> query.execute()
CREATE TABLE `another new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT ,
 `name` BIGINT, PRIMARY KEY ( `id` ) )
1


How to create a table

To create a table, first get a new table object, set a name and set the mode to "createtable".

After that you can define new columns. First create it with new_column() and then set the type, name etc. When finished, create the table with create_table_now().

Example 5-4. create table

>>> table = db.new_table()
>>> table.set_name("my new table")
>>> table.setmode_createtable()
>>> col=table.new_column()
>>> col.set_columntype(hk_column.auto_inccolumn)
>>> col.set_name("id")
>>> col=table.new_column()
>>> col.set_name("name")
>>> table.create_table_now()
CREATE TABLE `my new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , 
 BIGINT, PRIMARY KEY ( `id` ) )
Table created
1
And here are the creation relevant methods of hk_column

Figure 5-1. hk_column type methods

name()

returns the name of this column

set_name(name)

sets the column name

set_columntype(type)

sets the type of the column

Possible values are

  • textcolumn

  • auto_inccolumn

  • smallintegercolumn

  • integercolumn

  • smallfloatingcolumn

  • floatingcolumn

  • datecolumn

  • datetimecolumn

  • timecolumn

  • timestampcolumn

  • binarycolumn

  • memocolumn

  • boolcolumn

  • othercolumn

columntype()

returns the type of the column.

set_size()

sets the column size (e.g. if this column was should be a textcolumn with 10 characters set the type with set_type and the size with this function)

size()

returns the column size (e.g. if this column was created as CHAR(10) it will return 10)

set_primary(primary)

if 'primary' is true the column will be part of the primary key (primary index). Can only be edited if the datasource is in the mode ALTER or CREATE.

is_primary()

returns true if this column is part of a primary key

set_notnull(notnull)

if 'notnull' true the column needs a value

is_notnull()

returns True if this column has to have a value


How to create a calculated value field

Create a lineedit field with the name "calculated_field" and don't set any datasource or field property. Put the following code in the onpush action of a button. The value you want to display has first be formatted with format_number and then be set with hk_dsdatavisible.set_value.

Example 5-5. calculated values

col1=hk_this.datasource().column_by_name("field_1")
col2=hk_this.datasource().column_by_name("field_2")
result=(col1.curval_asdouble()*col2.curval_asdouble())
calcfield=cast_dslineedit(hk_thisform.get_visible("calculated_field"))
calcfield.set_value(format_number(result,
calcfield.use_numberseparator(),calcfield.precision()))


How to define macros

You can define macros - that means global available Python functions - in the onopen() action of the form, and then call it from any other action:

Example 5-6. Macro example

def mymakro(hk_this,v):
  hk_this.show_warningmessage("Displaying the text: "+v)

This can be called from a buttons onclick action:

Example 5-7. Macro example (part II)

mymakro(hk_this," my text")


How to format numbers

To format a number you can use the function format_number(). It will return a string. The first parameter is the number you want to format, the second is a boolean value whether or not you want a thousands separator, the third parameter shows the number of digits, and with the last you can set the target locale.

Example 5-8. Formatting a number

horst@horstnotebook:~> python
Python 2.3.2 (#1, Oct 28 2003, 21:22:16)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
>>> number=12345.6789
>>> print number
12345.6789
>>> print format_number(number,1,2)
12.345,68
>>> print format_number(number,True,2)
12.345,68
>>> print format_number(number,True,2,"C")
12345.68
>>> print format_number(number,True,2,"de_DE")
12.345,68
>>> print format_number(number,False,2,"de_DE")
12345,68
>>>


How to print rows in alternating colours

When printing a report it is often wanted to have every second row printed with a different colour to make it easier to read the output. To do so put the following code in the on_print action of a report field

Example 5-9. Printing alternate colours

if hk_thisreport.datasource().row_position() % 2 ==0:
   hk_this.set_backgroundcolour(hk_white)
else:
   hk_this.set_backgroundcolour(hk_grey)   


How to set the focus in a form

Put the following code into the on_click action of a button:

Example 5-10. Setting the taborder

hk_this.datasource().setmode_insertrow()
hk_thisform.goto_taborder_first()
When you push the button the datasource will allow you to insert a new row and move the focus from the button to the first field. Other possible functions are:
set_focus(widget)

will set the focus to 'widget'

Example 5-11. Setting the taborder for a specific field

hk_thisform.set_focus(hk_thisform.get_visible(2))

set_taborder( taborder[,registerchange [, forcesetting]])

sets the tab order (the focus order) of the widgets. 'taborder' is a list of the presentation numbers of the widgets, which should get the focus

goto_taborder_next()

will move the the focus to the next widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )

goto_taborder_previous()

will move the the focus to the previous widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )

goto_taborder_first()

will move the the focus to the first widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )

goto_taborder_last()

will move the the focus to the last widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )


How to react on pressed keys

Put the following code into the on_key action of a lineedit field:

Example 5-12. Reacting on keyboard input

key=hk_this.key()
print "on_key=(",key.key(),")(",key.text(),")"
if key.state()&key.state_ctrl:
  print "ctrl"
if key.state()&key.state_shift:
  print "shift"
if key.state()&key.state_alt:
  print "alt"

if key.key()==key.key_F5:
 show_warningmessage("F5 pressed. This will be ignored")
 key.set_accept_key(False)
 
if key.key()==key.key_M and key.state()&key.state_ctrl:
 show_warningmessage("ctrl m pressed") 
when the on_key action is triggered an object of type hk_key is created and can be called via hk_this.key(). If you set the hk_key.set_accept_key() method to 'False', the key will not be accepted as input.


How to load a file content into a field using a filedialog

Put the following code into the on_click action of a button:

Example 5-13. Loading a file content into a field

filename=hk_this.show_filedialog()
file=open(filename)
data=file.read()
datafield=cast_dsmemo(hk_thisform.get_visible("datafield"))
datafield.set_value(data)
This example lets the user select a file(line 1) and load its content (line 2 and 3) into a field (line 4 and 5).