Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362915
  • 博文数量: 35
  • 博客积分: 2176
  • 博客等级: 大尉
  • 技术积分: 797
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-13 14:03
文章分类

全部博文(35)

文章存档

2012年(9)

2009年(14)

2008年(12)

我的朋友

分类:

2008-07-02 15:21:57

文件: glade-article.pdf
大小: 141KB
下载: 下载

按部就班的来,还有问题,先放在这里,想到办法,再改进,欢迎留下评论。

talk_timer.pl

#!C:/perl/bin/perl.exe -w

use strict;
use warnings;

use FindBin;
use lib $FindBin::Bin;

use TalkTimer;

TalkTimer->run;

TalkTimer.pm

#!C:/perl/bin/perl.exe -w

package TalkTimer;

use strict;
use warnings;

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
use Gtk2::GladeXML;

use constant TIMER_MAXIMUM => 5 * 60;
use constant TIMER_WARNING => 4 * 60;

use constant WARNING_SOUND => 'WARNING.WAV';
use constant TIMES_UP_SOUND => 'GONG.WAV';

use base 'Class::Accessor::Fast';

sub run { my $self = shift->new; Gtk2->main; }

sub new { return bless({}, shift)->init; }

sub init {
    my $self = shift;
    my $glade_file = __FILE__;
    $glade_file =~ s/TalkTimer.pm/talk_timer.glade/;
    my $gui = Gtk2::GladeXML->new($glade_file);

    $gui->signal_autoconnect_from_package($self);

    $self->label ($gui->get_widget('timer_label') );
    $self->progress ($gui->get_widget('progress_bar'));
    $self->start_stop($gui->get_widget('start_stop') );
##说说有问题的几个地方

#1. $self->label->modify_font
    $self->label->modify_font(
        Gtk2::Pango::FontDescription->from_string("Sans 40")
    );

    $gui->get_widget('appwin')->show();
    return $self;
}

sub on_appwin_delete_event {
    Gtk2->main_quit;
}

__PACKAGE__->mk_accessors(qw(
  label progress start_stop
  start_time running warned times_up
));

sub on_start_stop_clicked {
    my $self = shift;

    if ($self->running) {
        $self->stop_timer;
    }
    else {
        $self->start_timer;
    }
}

sub start_timer {
    my $self = shift;

    $self->start_time(time);
    $self->running(TRUE);
    $self->warned(FALSE);
    $self->times_up(FALSE);
    $self->start_stop->set_label('Stop');
    Glib::Timeout->add(1000, sub { $self->tick });
}

sub stop_timer {
    my $self = shift;
    $self->running(FALSE);

#2.$self->label->set_text
    $self->label->set_text('0:00');
    $self->progress->set_fraction(0);
    $self->start_stop->set_label('Start');
}

sub tick {
    my $self = shift;
    return FALSE unless $self->running;

    my $secs = time - $self->start_time;

#3.应该来说是label->set_text这个导致的,因为不认识
    $self->label->set_text(
        sprintf('%d:%02d', int($secs/60), $secs % 60)
    );

    my $frac = $secs > TIMER_MAXIMUM ? 1 : $secs/TIMER_MAXIMUM;
    $self->progress->set_fraction($frac);

    if($secs >= TIMER_MAXIMUM and !$self->times_up) {
        $self->times_up(TRUE);
        $self->play_times_up_sound;
    }
    elsif($secs >= TIMER_WARNING and !$self->warned) {
        $self->warned(TRUE);
        $self->play_warning_sound;
    }
    
    return TRUE;
}

sub play_warning_sound { play_sound(WARNING_SOUND) }
sub play_times_up_sound { play_sound(TIMES_UP_SOUND)}

sub play_sound {
    my $filename = shift;

    system("play $filename");
}
1;

当然最终的结果就不对了,而且应该.glade文件也有些问题,因为我用的glade3中文版的,与文中说的不对应。

希望有高手路过方便指导下。

阅读(1216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~