【发布时间】:2013-12-24 22:50:13
【问题描述】:
我最近一直在尝试 Vala 编程。我有相当丰富的其他语言编程经验,但近年来主要使用 Python、Tcl 和 Perl 等脚本语言。 Elementary OS 的简洁外观给我留下了深刻的印象,这也是我开始研究 Vala 的原因,我必须说我的第一印象是非常积极的。但是,我在 Dialog 编程方面遇到了障碍,我认为更有经验的 Vala 程序员可能会提供帮助。在我最新的测试程序中,我使用一个基于对话框的例程来获取一个值 (getYesNo),然后使用另一个基于对话框的例程 (showDialog) 向屏幕显示一个描述该值的字符串。两个例程在独立使用时都可以正常工作,但是当按照描述一起使用时,显示事件会被阻止,直到对值获取例程进行第二次调用。这听起来像是在 Perl 或 Tcl 中调用“flush_events”可以解决的那种情况。但是我该如何在 Vala 处理它?或者有没有办法一开始就避免它发生?
代码:
using Gtk;
using Posix;
public class DialogTestWindow: ApplicationWindow {
private int RESPONSE;
private Toolbar tbMain = new Toolbar();
private ToolButton bAbout = new ToolButton(new Image.from_icon_name
("help-about",IconSize.SMALL_TOOLBAR),null);
private ToolButton bDoIt = new ToolButton(new Image.from_icon_name
("media-record",IconSize.SMALL_TOOLBAR),null);
private ToolButton bQuit = new ToolButton(new Image.from_icon_name
("application-exit",IconSize.SMALL_TOOLBAR),null);
internal DialogTestWindow(DialogTest app) {
Object(application: app, title: "DialogTest");
this.window_position = WindowPosition.CENTER;
this.set_default_size(720,480);
// ---- Set up Toolbar ----------------------------------------------------
tbMain.get_style_context().add_class(STYLE_CLASS_PRIMARY_TOOLBAR);
bQuit.is_important = true;
bQuit.clicked.connect(onQuit);
tbMain.add(bQuit);
bAbout.is_important = true;
bAbout.clicked.connect(onAbout);
tbMain.add(bAbout);
bDoIt.is_important = true;
bDoIt.clicked.connect(onDoIt);
tbMain.add(bDoIt);
// ---- Pack Toolbar etc into vBox on main window -------------------------
Box vbMain = new Box(Orientation.VERTICAL,0);
vbMain.pack_start(tbMain,false,true,0);
this.add(vbMain);
this.show_all();
printf("Started\n");
}
// ==== getYesNo ==========================================================
private void getYesNo(string message) {
Dialog dialog = new Dialog.with_buttons
("Get",this,DialogFlags.MODAL,
Stock.YES,ResponseType.YES,Stock.NO,ResponseType.NO,null);
var content = dialog.get_content_area();
// warning: assignment from incompatible pointer type
// [enabled by default]
Label label = new Label(message);
label.set_line_wrap(true);
content.add(label);
dialog.response.connect((id)=>{
printf("response id=%i\n",id);
RESPONSE = id;
dialog.destroy();
});
dialog.show_all();
}
// ==== onAbout ===========================================================
private void onAbout() {
Dialog dialog = new Dialog.with_buttons
("About",this,DialogFlags.MODAL,
Stock.OK,ResponseType.OK,null);
var content = dialog.get_content_area();
// warning: assignment from incompatible pointer type
// [enabled by default]
Label label = new Label("This program tests pop-up dialogs");
content.add(label);
dialog.response.connect(()=>{dialog.destroy();});
dialog.show_all();
}
// ==== onDoIt ============================================================
private void onDoIt() {
getYesNo("Well?");
if (RESPONSE==ResponseType.YES) showDialog("YES!");
}
// ==== onQuit ============================================================
private void onQuit() {
printf("Ending\n");
exit(-1);
}
// ==== showDialog ========================================================
private void showDialog(string message) {
Dialog dialog = new Dialog.with_buttons
("Show",this,DialogFlags.MODAL,
Stock.OK,ResponseType.OK,null);
var content = dialog.get_content_area();
// warning: assignment from incompatible pointer type
// [enabled by default]
Label label = new Label(message);
label.set_line_wrap(true);
content.add(label);
dialog.response.connect((id)=>{dialog.destroy();});
dialog.show_all();
}
}
public class DialogTest: Gtk.Application {
internal DialogTest() {
Object(application_id: "org.test.DialogTest");
}
protected override void activate() {
new DialogTestWindow(this).show();
}
}
extern void exit(int exit_code);
public int main(string[] args) {
return new DialogTest().run(args);
}
【问题讨论】:
-
你能包含一些代码吗?请记住,Vala 本质上使用 GTK+,因此您可能会在该文档(对话框、模式对话框等)中找到答案。
-
谢谢西蒙(祝你圣诞快乐!)。我在原始问题的底部添加了完整的代码。
标签: vala