【问题标题】:Why does `print` not work in this wxPerl program?为什么 `print` 在这个 wxPerl 程序中不起作用?
【发布时间】:2011-03-20 11:01:08
【问题描述】:

在我的框架构造函数中,我有一个函数可以轻松地为我创建一个菜单栏。

package Routines;

#This function will set up a menu
#REQUIRED: entries
#RETURNS:  id, menu
sub SetupMenu {
    $menuItemCount = 0;                     #Element number under the same menu
    $subMenuCount  = 0;                     #Number of menus
    $mbar          = Wx::MenuBar->new();    #Menu bar constructor
    for ($totalCount = 0; $totalCount < scalar($_[1]); $totalCount++) {    #Loop for each entry
        if ($menuItemCount == 0) {                                         #If this is the first entry in the menu
            $menuList[$subMenuCount] = Wx::Menu->new($_[$totalCount]);     #Construct a menu and make this the title
        } elsif ($_[$totalCount] == "---") {                               #If the entry is ---
                                                                           #Treat it as a separator, skip ID
        } elsif ($_[$totalCount] == "***") {                               #If the entry is ***
            $mbar->Append($menuList[$subMenuCount]);                       #Add the menu to the bar
            $menuItemCount = 0;                                            #Reset the number of elements
            $subMenuCount++;                                               #Increment the number of menus
        } else {                                                           #On normal operation
            $menuList[$subMenuCount]->Append($id[$totalCount], $_[$totalCount]);    #Add the element to the menu and assign it an ID
        }
    }
    #print $mbar;
    return (@id, $mbar);
}

#This package puts crap in the main window
package mehFrame;
use base qw(Wx::Frame);

sub new {
    #Preparation
    $class = shift;
    $self  = $class->SUPER::new(@_);

    #Place the panel
    $pan = Wx::Panel->new($self, -1);

    #Set up menus
    (@mehId, $mehBar) = Routines::SetupMenu("File", "Open ROM", "Save ROM", "Save ROM As", "---", "Close ROM", "Exit");

    #Return
    return $self;
}
[...]

不幸的是,它不起作用。在SetupMenu() 函数中放入print 后,它没有打印。另一方面,当我将其设为warn 时,它会发出警告。

更糟糕的是,即使我在new() 函数中输入了print,它仍然无法打印。怎么回事?

【问题讨论】:

    标签: perl wxwidgets wxperl


    【解决方案1】:

    Yakov,在没有其他答案的情况下,我会对此进行尝试,但由于我不是 wxPerl 专家,因此请谨慎对待。

    您的描述听起来像是打印到 STDERR 有效,因为那是 warn 的去处,而打印到 STDOUT 则不行。

    尝试改用print STDERR $mbar - 我很确定它会起作用。

    更新:根据 daotoad 的优秀建议,这也可能是由于缺少刷新 - 如果是这样,那么在 STDOUT 上设置 autoflush 将解决它。无论是一种还是另一种,都取决于 OP 的尝试。我将其添加到我的答案中,因为 daotoad 仅发表了评论并且尚未添加他自己的单独答案 - 一旦他这样做,我将删除。

    【讨论】:

    • 可能是他正在遭受缓冲。 IIRC STDERR 默认设置为 autoflush,但 STDOUT 设置为缓冲输出。
    • @daotoad - 非常有道理......那是我的下一个猜测......你可能想把它变成一个答案,以便 OP 看到它。
    • 感谢自动刷新建议!在文件顶部添加$|=1 会在需要时完成print。但是,该栏仍然没有显示,尽管这可能是我的程序。
    【解决方案2】:

    在 Wx 下打印有时是异步的,除非您添加“\n”

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 2016-04-20
      • 2015-05-21
      • 2011-02-27
      • 2022-01-17
      • 1970-01-01
      • 2019-12-31
      • 2013-08-09
      • 2015-09-22
      相关资源
      最近更新 更多