Chinaunix

标题: 请教一个POE+ Gtk2的例子 [打印本页]

作者: perlw01f    时间: 2010-12-14 19:50
标题: 请教一个POE+ Gtk2的例子
POE: Cookbook - Gtk2 Counter Glade
源程序链接 http://poe.perl.org/?POE_Cookbook/Gtk2_Counter_glade
glade文件 http://tipi.sf.net/cookbook.glade
  1. #!/usr/bin/perl
  2. # http://poe.perl.org/?POE_Cookbook/Gtk_Interfaces
  3. #
  4. # This sample program creates a very simple Gtk counter.  Its
  5. # interface consists of three widgets: A label, a rapidly increasing
  6. # counter, and a button to reset that counter.
  7. # the 1 - 4 here corresponds to the points in the cookbook entry without
  8. # glade
  9. # 4. Because with POE::Session::GladeXML2, you create the gui through glade
  10. # instead of code, and because it expects all the gtk signal handlers to
  11. # be in a seperate package, it becomes even easier to seperate the GUI
  12. # specific code from the non-gui-dependant code.
  13. package Foo;
  14. use warnings;
  15. use strict;

  16. # POE::Loop::Glib doesn't initialize Gtk2 (for obvious reasons), so we
  17. # do it here
  18. use Gtk2-init;
  19. use POE;
  20. use POE::Session::GladeXML2;

  21. sub new {
  22.   my ($class) = @_;
  23.   my $self = bless {}, $class;
  24.   my $session = POE::Session::GladeXML2->create(
  25.     glade_object => $self,
  26.     glade_file   => 'cookbook.glade',

  27. # 1. You can use the glade_mainwin parameter to create() to tell which
  28. # widget should be considered the main window.
  29.     glade_mainwin => 'window1',
  30.     inline_states => {
  31.       _start   => \&ui_start,
  32.       ev_count => \&ui_count,
  33.     }
  34.   );
  35.   return $self;
  36. }

  37. sub ui_start {
  38.   my ($kernel, $heap) = @_[KERNEL, HEAP];
  39.   $heap->{counter} = 0;
  40.   $kernel->yield("ev_count");
  41. }

  42. # Handle the "ev_count" event by increasing a counter and displaying
  43. # its new value.
  44. sub ui_count {
  45.   my ($session, $kernel, $heap) = @_[SESSION, KERNEL, HEAP];

  46. # 2. When using Gtk2::GladeXML, you can get any widget by name. Thus
  47. # there is no more need to store them yourself.
  48.   my $label = $session->gladexml->get_widget('counter_label');
  49.   $label->set_text(++$heap->{counter});
  50.   $kernel->yield("ev_count");
  51. }

  52. # 3. Instead of having to manually connect gtk signals with poe event
  53. # handlers, POE::Session::GladeXML2 automatically connects the handler
  54. # name you set in glade with the corresponding method in the object you
  55. # pass as glade_object.
  56. sub ui_clear {
  57.   $_[HEAP]->{counter} = 0;
  58. }

  59. package main;
  60. use POE;

  61. # Run the program until it is exited.
  62. my $foo = Foo->new;
  63. $poe_kernel->run();
  64. exit 0;
复制代码
运行之后 没报错 界面上只有一个白块

拉伸后

时间稍长一点 终止时 会显示 "Out of memory!"
不知道原因
麻烦 帮我看看 谢谢
Win 7 32 bit, Active Perl 5.10

small.JPG (10.1 KB, 下载次数: 17)

small.JPG





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2