The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Tk::GtkSettings - Give Tk applications the looks of Gtk applications


     use Tk::GtkSettings;
     use Tk::GtkSettings qw(initDefaults export2xrdb);
     #do your adjustments here
     #then initialize your perl/Tk app.
     use Tk;
     my $w = new MainWindow;
     #Do your stuff here


Apply Gtk colors and fonts to your perl/Tk application


Tk::GtkSettings attempts to overcome some very old complaints about Tk:

 - It's ugly!
 - It's complicated to adjust colors and fonts to your desktop style

Tk::GtkSettings loads your Gtk configuration files and applies it's font and color settings to your perl/Tk application.

initDefaults loads some nice (at least we think so) default settings that copies your Gtk theme pretty well.

However, it gives plenty of tools for you to adjust it and mess it up any way you like.

It is harmless to install on Windows or Mac. It just will not do anything on these systems. That makes it smooth to add as a dependency to your own package if you want it to be able to run on Windows and Mac as well.

In working with colors it assumes 8-bit color depth.



Usefull for testing and debugging. export2xrdb exports to a file which then is sent to xrdb. It checks if this file should be deleted when done. Default value is 1.


Usefull for testing. Default value is ~/.config/gtk-3.0/. That is the location where the Gtk configuration files reside. This variable is not defined when on Windows or Mac.


Default value ~/.tkgtksettings. Used by export2xrdb. This variable is not defined on Windows or Mac.


Usefull for testing and debugging. Default value is 0. If set Tk::GtkSettings will complain about everything not in order. Otherwise it will quietly fail.

alterColor($hexcolor, $offset)

Adjusts $hexcolor by $offset. It takes every color chanel and adds or substracts $offset. If the channel value is greater than 127 it will substract, otherwise it will add.

 alterColor('#000000', 1) returns #010101
 alterColor('#FFFFFF', 1) returns #FEFEFE

    Just making life easy. Call this one and your done, unless you require adjustments. It calls initDefaults and exports the whole bunch to xrdb. Exported by default.


    Sets and returns your application name. By default it is set to the basename of what is in $0. Your Gtk settings will only be applied to your application in xrdb. You can set it to an empty string. Then it will apply your Gtk settings to all your perl/Tk applications.

convertColorCode('rgb(255, 0, 0)')

    Some color settings in the Gtk configuration files are in the format 'rgb(255, 255, 255)'. convertColorCode converts these to a hex color string.


    Converts the font string in gtk to something Tk can handle

export2file($file, ?$removeflag?)

    Exports your Gtk settings to $file in a format recognized by xrdb. It looks for a section in the file marked by appName . "Tk::GtkSettings section\n". If it finds it it will replace this section. Otherwise it will append your Gtk settings to the end of the file. If $file does not yet exist it will create it. if $removeflag is true it will not export but remove the section from $file.


    Same as export2file, however the file is always '~/.Xdefaults'.


    Same as export2file, however the file is always '~/.Xresources'.


    exports your Gtk settings directly to the xrdb database.


    Generates the output used by the export functions. Returns a string.

groupAdd($groupname, \@members, \%options)

    Adds $groupname to the groups hash. If @members or %options are not specified, it will leave them empty.


    Returns a list of all available groups.


    Removes $groupsname from the groups hash. You cannot delete the 'main' group.


    Returns true if $groupname is available.


    Returns the list of existing members of $groupname. It will return an empty list if $groupname equals 'main'.

groupMembersAdd($groupname, @newmembers)

    Adds new members to $groupname. You cannot add members to the 'main' group.

groupMembersReplace($groupname, @members)

    Replaces the list of members in $groupsname by @members. You cannot modify the members list of the 'main' group.

groupOption($groupname, $option, ?$value?)

    Sets and returns the value of $option in $groupname. $value should be a corresponding key from the Gtk hash. If that key is not found, it assumes a direct value.


    Returns a list of all available options in $groupname.

groupOptionDelete($groupname, $option)

    Removes $option from $groupname

gtkKey($key, ?$value?)

    Sets and returns the value of $key in the Gtk hash


    Returns a list of all available keys in the Gtk hash.


    Delets $key from the Gtk hash.


    Initializes some sensible defaults. Also does a full reset and loads Gtk configuration files.


    Returns and array with the decimal values of red, green and blue.


    Return the hexadecimal representation of $num in a two character string.


    Empties the Gtk hash and (re)loads the Gtk configuration files.


    Returns true if you are not on Windows or Mac.


    Same as export2file($file, 1)


    Same as export2Xdefaults(1)


    Same as export2Xresources(1)


    Removes all the settings previously defined from the xrdb database


    Removes all groups and options. The group 'main' will remain, but all its options are also deleted. This does not affect the Gtk hash.

rgb2hex($red, $green, $blue)

    Converts the decimval values $red, $green and $blue into a hex color string.


Copyright 2022 - 2023 by Hans Jeuken

Same as Perl, in your option.


Hans Jeuken (jeuken dot hans at gmail dot com)


If you find any bugs, please contact the author.


1 POD Error

The following errors were encountered while parsing the POD:

Around line 309:

'=item' outside of any '=over'