undef不是Gtk2 :: Gdk :: Drawable类型 - gtk2-perl自定义小部件 - 调用GtkWidget的窗口方法

时间:2016-03-14 19:30:18

标签: gtk gtk2

我正在尝试制作自定义Perl Gtk2小部件。当我尝试访问GtkDrawingArea的window属性时,它返回undef而不是GdkDrawable对象。根据{{​​3}},此方法可以返回窗口对象或undef,但它不会在什么条件下返回undef。有人可以澄清一下吗?

#!/usr/bin/perl

use strict;
use warnings;

package Gtk2::MIDIPlot;

use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;

sub new {
  my $class = shift;
  my $this = bless Gtk2::DrawingArea->new(), $class;

  $this->set_size_request(14400, 768);

  return $this;
}

sub draw {
  my $drawArea = shift;

  my $thisCairoSurface = Gtk2::Gdk::Cairo::Context->create($drawArea->window);
}

package main;

use Gtk2 -init;

my $window = Gtk2::Window->new();

my $mainWidgetScroll = Gtk2::ScrolledWindow->new();
my $mainWidget = Gtk2::MIDIPlot->new();
$mainWidget->draw($mainWidget);
$mainWidgetScroll->add_with_viewport($mainWidget);
$mainWindow->add($mainWidgetScroll);

$window->signal_connect(destroy => sub{Gtk2->main_quit()});
$window->show_all();
Gtk2->main();

0;

1 个答案:

答案 0 :(得分:1)

查看this example我发现我需要等待窗口小部件的公开事件才能绘制它,因为在该事件之前尚未创建窗口。此外,代码需要更好地适应GTK设置方式,然后在回调中执行操作。

#!/usr/bin/perl

use strict;
use warnings;

package Gtk2::MIDIPlot;

use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;

sub new {
  my $class = shift;
  my $this = bless Gtk2::DrawingArea->new(), $class;

  $this->signal_connect(expose_event => 'Gtk2::MIDIPlot::draw');

  return $this;
}

sub draw {
  my $drawArea = shift;

  $drawArea->set_size_request(14400, 768);
  my $thisCairoSurface = Gtk2::Gdk::Cairo::Context->create($drawArea->get_window());
}