用于创建简单的 Microsoft Windows GUI 的 Perl 模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1015699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Perl module(s) for creating a simple Microsoft Windows GUI?
提问by Chas. Owens
I'd like to create a simple Windows GUI for my Perl program. It basically needs to spawn a window, write log information to a text box, and have an input box and a couple of start/stop buttons.
我想为我的 Perl 程序创建一个简单的 Windows GUI。它基本上需要生成一个窗口,将日志信息写入文本框,并有一个输入框和几个开始/停止按钮。
Does anyone have any tips as to which Perl modules I use? The people I work with like Qt, so that may be a preference, but I'm not bothered.
有没有人对我使用哪些 Perl 模块有任何提示?和我一起工作的人喜欢 Qt,所以这可能是一种偏好,但我并不介意。
回答by Sinan ünür
I did use Win32::GUIonce for such a simple project. The main window had a menu, a text-box and a few buttons and checkboxes. It worked.
对于这样一个简单的项目,我确实使用过一次Win32::GUI。主窗口有一个菜单、一个文本框和一些按钮和复选框。有效。
Excerpt from the method that sets up the GUI (just to give you an idea):
摘自设置 GUI 的方法(只是为了给您一个想法):
my @menu_items = (
'&File' => 'File',
' > &Open' => {
-name => 'FileOpen',
-onClick => sub { $self->onFileOpen(@_) },
},
' > &Close' => {
-name => 'FileClose',
-onClick => sub { $self->onFileClose(@_) },
},
' > E&xit' => {
-name => 'FileExit',
-onClick => sub { $self->onFileExit(@_) },
},
'&Help' => 'Help',
' > &About' => {
-name => 'About',
-onClick => sub { $self->onHelpAbout(@_) },
},
);
$self->set_main_menu( Win32::GUI::MakeMenu(@menu_items) );
my $window = $self->set_main_window(
Win32::GUI::Window->new(
-menu => $self->get_main_menu,
-name => 'Main',
-sizable => 0,
-resizable => 0,
-hasmaximize => 0,
-maximizebox => 0,
-title => $self->get_program_name,
-onTerminate => sub { -1 },
-onTimer => sub { $self->onTimer(@_) },
),
);
$self->set_log_field(
$window->AddTextfield(
-name => 'Log',
-font => Win32::GUI::Font->new(
-name => 'LogFont',
-face => 'Courier New',
-size => 9,
),
-multiline => 1,
-wantreturn => 1,
-autovscroll => 1,
-vscroll => 1,
-readonly => 1,
),
);
$self->get_log_field->MaxLength(40000);
$self->set_status_bar(
$window->AddStatusBar(
-name => 'Status',
-text => $self->get_program_name,
),
);
回答by Chas. Owens
You have several choices:
您有多种选择:
I am partial to Gtk2. It is easily installed in MS Windows via the CamelBoxinstaller.
我偏爱 Gtk2。它可以通过CamelBox安装程序轻松安装在 MS Windows 中。
A simple "hello world" style application looks like
一个简单的“hello world”风格的应用程序看起来像
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2;
Gtk2->init;
my $window = Gtk2::Window->new;
my $vbox = Gtk2::VBox->new;
my $label = Gtk2::Label->new("Hello World");
my $button = Gtk2::Button->new("Press me");
$window->add($vbox);
$vbox->add($label);
$vbox->add($button);
my $i;
$button->signal_connect(clicked => sub {
$label->set_text("button pressed " . ++$i . " times");
});
$window->signal_connect(destroy => sub { Gtk2->main_quit });
$window->show_all;
Gtk2->main;
回答by Akers
Perl 5.10 from Activestate come pre-compiled with Tkx which is a Gui Platform. You can download Perl Tk if you'd like a module with more web examples. Which ever module you use, you can download GUIbuilderfrom sourceforge and it writes pretty good Tk or Tkx code for perl, and Tk code for python, ruby.
Activestate 的 Perl 5.10 预编译了 Tkx,这是一个 Gui 平台。如果您想要包含更多 Web 示例的模块,可以下载 Perl Tk。无论您使用哪个模块,您都可以从 sourceforge下载GUIbuilder,它为 perl 编写了非常好的 Tk 或 Tkx 代码,为 python、ruby 编写了 Tk 代码。
This code was largely generated by GuiBuilder as an example of output code:
此代码主要由 GuiBuilder 生成,作为输出代码的示例:
use Tkx;
Tkx::package_require('BWidget');
sub example::ui {
my($root) = @_;
my($_entry_box) = $root->new_entry(
-width => 0,
);
my($_text_box) = $root->new_text(
-height => 0,
-width => 0,
);
my($_label) = $root->new_label(
-text => "Hello World",
);
my($_textbox_horiz_scrollbar) = $root->new_scrollbar(
-orient => "horizontal",
);
my($_textbox_vert_scrollbar) = $root->new_scrollbar(
);
my($_Start_Button) = $root->new_Button(
-text => "Start",
-width => 10,
);
my($_Stop_Button) = $root->new_Button(
-text => "Stop",
-width => 10,
);
$_entry_box->configure(
-invalidcommand => \&_entry_box_invalidcommand
);
$_entry_box->configure(
-validatecommand => \&_entry_box_validatecommand
);
$_entry_box->configure(
-xscrollcommand => \&_entry_box_xscrollcommand
);
$_text_box->configure(
-xscrollcommand => [ $_textbox_horiz_scrollbar => set ]
);
$_text_box->configure(
-yscrollcommand => [ $_textbox_vert_scrollbar => set ]
);
$_textbox_horiz_scrollbar->configure(
-command => [ $_text_box => xview ]
);
$_textbox_vert_scrollbar->configure(
-command => [ $_text_box => yview ]
);
$_Start_Button->configure(
-armcommand => \&_Start_Button_armcommand
);
$_Start_Button->configure(
-command => \&_Start_Button_command
);
$_Start_Button->configure(
-disarmcommand => \&_Start_Button_disarmcommand
);
$_Stop_Button->configure(
-armcommand => \&_Stop_Button_armcommand
);
$_Stop_Button->configure(
-command => \&_Stop_Button_command
);
$_Stop_Button->configure(
-disarmcommand => \&_Stop_Button_disarmcommand
);
sub _entry_box_xscrollcommand {}
# Geometry Management
$_entry_box->g_grid(
-in => $root,
-column => 1,
-row => 2,
-columnspan => 3,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 5,
-rowspan => 1,
-sticky => "ew"
);
$_text_box->g_grid(
-in => $root,
-column => 1,
-row => 3,
-columnspan => 2,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => "news"
);
$_label->g_grid(
-in => $root,
-column => 1,
-row => 1,
-columnspan => 3,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => "ew"
);
$_textbox_horiz_scrollbar->g_grid(
-in => $root,
-column => 1,
-row => 4,
-columnspan => 2,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => "ew"
);
$_textbox_vert_scrollbar->g_grid(
-in => $root,
-column => 3,
-row => 3,
-columnspan => 1,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => "ns"
);
$_Start_Button->g_grid(
-in => $root,
-column => 1,
-row => 5,
-columnspan => 1,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => ""
);
$_Stop_Button->g_grid(
-in => $root,
-column => 2,
-row => 5,
-columnspan => 2,
-ipadx => 0,
-ipady => 0,
-padx => 0,
-pady => 0,
-rowspan => 1,
-sticky => ""
);
# Resize Behavior
$root->g_grid_rowconfigure(1, -weight => 0, -minsize => 2, -pad => 0);
$root->g_grid_rowconfigure(2, -weight => 0, -minsize => 12, -pad => 0);
$root->g_grid_rowconfigure(3, -weight => 1, -minsize => 85, -pad => 0);
$root->g_grid_rowconfigure(4, -weight => 0, -minsize => 4, -pad => 0);
$root->g_grid_rowconfigure(5, -weight => 0, -minsize => 40, -pad => 0);
$root->g_grid_columnconfigure(1, -weight => 1, -minsize => 67, -pad => 0);
$root->g_grid_columnconfigure(2, -weight => 1, -minsize => 186, -pad => 0);
$root->g_grid_columnconfigure(3, -weight => 0, -minsize => 2, -pad => 0);
}
my($root) = Tkx::widget->new('.');
$root->g_wm_title('stackoverflow');
example::ui($root);
Tkx::MainLoop();
1;
回答by Demi
use Perl/Tk
使用 Perl/Tk
回答by Paul Chernoch
The only one I have used is Perl Tk. It was quick to learn, there are many examples on the web, and I was able to port from Mac OS X to Windows quickly.
我唯一使用过的就是 Perl Tk。学起来很快,网上有很多例子,我能够快速从 Mac OS X 移植到 Windows。
The downside is that the GUI looks dated. It is great for in-house tools, but not for selling to third parties.
缺点是 GUI 看起来过时了。它非常适合内部工具,但不适用于销售给第三方。