deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to build a Gtk2 program that resides in the system tray. I want the program to write the default settings to a file. When I click accept nothing is wrote to the file. How can I get the program to write to the file? The code to write the file is at the end of the snippet.

#!/usr/bin/perl -w use strict; use warnings; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use Gtk2::TrayIcon; #user variables my $icon_file = Gtk2::Gdk::Pixbuf->new_from_file("/home/deadpickle/Des +ktop/Gtk2/test.png"); my $time = 1200000; #global variables my $user = 'uas'; my $server = 'updraft.unl.edu'; my $local_waypoint = '/local'; my $remote_waypoint = '/remote'; my $waytemp = 'waytemp'; my $wayfinal = 'wayfinal'; #icon goes in box, box goes in tray my $icon = Gtk2::Image->new_from_pixbuf($icon_file); my $eventbox = Gtk2::EventBox->new; $eventbox->add($icon); my $tray = Gtk2::TrayIcon->new('Test'); $tray->add($eventbox); #tooltip my $tooltip = Gtk2::Tooltips->new; $tooltip->set_tip($tray, "GRRUVI v1.0"); #events and timeouts $eventbox->signal_connect('button_release_event', \&click); #show tray $tray->show_all; #end event loop Gtk2->main; #handles tray clicks sub click { #left mouse button if ($_[1]->button == 1) { #what should this do? #&open_woot(); } #right mouse button elsif ($_[1]->button == 3) { &menu(); } return 1; } #right click menu sub menu { my $menu = Gtk2::Menu->new(); #configure my $menu_pref = Gtk2::ImageMenuItem->new_with_label("Configure"); $menu_pref->signal_connect(activate => \&configure); $menu_pref->set_image(Gtk2::Image->new_from_stock('gtk-preferences +', 'menu')); $menu->add($menu_pref); #refresh my $menu_refresh = Gtk2::ImageMenuItem->new_with_label("Refresh"); $menu_refresh->signal_connect(activate => \&refresh); $menu_refresh->set_image(Gtk2::Image->new_from_stock('gtk-refresh' +, 'menu')); $menu->add($menu_refresh); #separator my $menu_sep = Gtk2::SeparatorMenuItem->new(); $menu->add($menu_sep); #Quit my $menu_quit = Gtk2::ImageMenuItem->new_with_label("Quit"); $menu_quit->signal_connect(activate => sub { Gtk2->main_quit;} ); $menu_quit->set_image(Gtk2::Image->new_from_stock('gtk-quit', 'men +u')); $menu->add($menu_quit); $menu->show_all; #popup menu, the three is for right mouse button $menu->popup(undef,undef,undef,3,undef,undef); return 1; } #configuration dialog window (not filled) sub configure { #Create the new window my $config_window = Gtk2::Window->new('toplevel'); $config_window->set_title("Configuration"); #VBox container my $table_config = Gtk2::Table->new(3, 5, FALSE); #Create Notebook my $config_notebook = Gtk2::Notebook->new; $config_notebook->set_tab_pos('top'); #the First Page; Connection my $connect_vbox = Gtk2::VBox->new( FALSE, 1); my $label_user = Gtk2::Label->new('Username'); my $entry_user = Gtk2::Entry->new; my $label_server = Gtk2::Label->new('Server'); my $entry_server = Gtk2::Entry->new; $entry_user->set_text($user); $entry_server->set_text($server); $connect_vbox->pack_start($label_user, FALSE, FALSE, 1); $connect_vbox->pack_start($entry_user, FALSE, FALSE, 1); $connect_vbox->pack_start($label_server, FALSE, FALSE, 1); $connect_vbox->pack_start($entry_server, FALSE, FALSE, 1); #the Second Page; Waypoint my $table_waypoint = Gtk2::Table->new(4, 2, FALSE); my $label_temp_waypoint = Gtk2::Label->new('Temp Waypoint Filename +'); my $entry_temp_waypoint = Gtk2::Entry->new; my $label_final_waypoint = Gtk2::Label->new('Final Waypoint Filena +me'); my $entry_final_waypoint = Gtk2::Entry->new; my $label_local_waypoint = Gtk2::Label->new('Local Waypoint Folder +'); my $entry_local_waypoint = Gtk2::Entry->new; my $label_remote_waypoint = Gtk2::Label->new('Remote Waypoint Fold +er'); my $entry_remote_waypoint = Gtk2::Entry->new; $entry_temp_waypoint->set_text($waytemp); $entry_final_waypoint->set_text($wayfinal); $entry_local_waypoint->set_text($local_waypoint); $entry_remote_waypoint->set_text($remote_waypoint); $entry_temp_waypoint->set_editable(0); $entry_final_waypoint->set_editable(0); $table_waypoint->attach_defaults($label_temp_waypoint, 0, 1, 0, 1) +; $table_waypoint->attach_defaults($entry_temp_waypoint, 1, 2, 0, 1) +; $table_waypoint->attach_defaults($label_final_waypoint, 0, 1, 1, 2 +); $table_waypoint->attach_defaults($entry_final_waypoint, 1, 2, 1, 2 +); $table_waypoint->attach_defaults($label_local_waypoint, 0, 1, 2, 3 +); $table_waypoint->attach_defaults($entry_local_waypoint, 1, 2, 2, 3 +); $table_waypoint->attach_defaults($label_remote_waypoint, 0, 1, 3, +4); $table_waypoint->attach_defaults($entry_remote_waypoint, 1, 2, 3, +4); #append pages $config_notebook->append_page($connect_vbox, "Connection"); $config_notebook->append_page($table_waypoint, "Waypoint"); #add button to the main window my $button_accept = Gtk2::Button->new_with_mnemonic("_Accept"); my $button_cancel = Gtk2::Button->new_with_mnemonic("_Cancel"); #pack them into the dialog window $table_config->attach_defaults($config_notebook, 0, 5, 0, 1); $table_config->attach_defaults($button_accept, 1, 2, 2, 3); $table_config->attach_defaults($button_cancel, 3, 4, 2, 3); $config_window->add($table_config); $config_window->show_all; #Button Functions $button_cancel->signal_connect('clicked' => sub {$config_window->d +estroy}); $button_accept->signal_connect('clicked' => sub {$user = $entry_us +er->get_text; $server = $entry_server->get_text; $local_waypoint = $e +ntry_local_waypoint->get_text; $remote_waypoint = $entry_remote_waypo +int->get_text; my @settings = ($user, $server, $local_waypoint, $remo +te_waypoint);save_default(@settings) ; $config_window->destroy}); } sub save_default { open DEFAULT, '<', 'default.cfg' or die "Could not Open file!! $!" +; my $num_setting = @_; for ( my $count = 0; $count <= ($num_setting-1); $count++) { print "$_[$count]\n"; } close DEFAULT; }

Replies are listed 'Best First'.
Re: Gtk2 question(s)
by moritz (Cardinal) on Jul 17, 2007 at 07:37 UTC
    You are opening the file for reading (<), and you are not printing into the Filehandle.

    Try

    my @settings = @_; open my $default, '>', 'default.cfg' or die $!; for (@settings){ print $default $_, "\n"; } close $default;

    BTW I don't have a system tray under linux...

      BTW I don't have a system tray under linux...

      I do. The Gtk2::TrayIcon will put an entry into the ICEWM control bar ( or whatever you call it where the minimized icons appear, with the clock and virtual desktop selector). I'm fairly sure it does the same with the KDE and Gnome desktops( or fvwm Blackbox, etc. )


      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        Aye, I just wanted to hint that not every window manager under linux supports something like a system tray, so it's a feature you shouldn't rely on if you want your app to stay portable.
      Thanks for the help! I am trying to use Gtk2::FileChooser to select a configuration file to load. I noticed that that module is not documented very well and does not have a "$widget = Gtk2::FileChooser->new;" line so I was wondering if anyone had an example on how to use this widget? Also is there a way I can save the settings after closing the program without writing them to a file? Possibly to memory? Also, I am trying to add in threads to the program. What is the best way to do this? I cant seem to get the program to close correctly when I click on "Quit" in the menu, what do I need to do to get a clean exit? ( the tread is at the bottom and the quit is in the menu section)
        Gtk2::FileChooser is just an interface (that is, a GInterface). See the manpage for Gtk2::FileChooserDialog, which is a concrete class that you can construct.