如何使用Glade / GtkD和D编程语言显示Hello World

时间:2022-04-16 15:02:01

On Ubuntu Linux, I can use the Glade application to create a Hello World dialog. Now how do I get the D programming language to display it?

在Ubuntu Linux上,我可以使用Glade应用程序创建一个Hello World对话框。现在我如何让D编程语言显示它?

1 个解决方案

#1


11  

  1. Install the DMD compiler that compiles the D language on your Mac, Linux, or Windows computer. You can get more info about this here.
  2. 安装在Mac,Linux或Windows计算机上编译D语言的DMD编译器。你可以在这里获得更多相关信息。

  3. Install the Glade interface designer on your Mac, Linux, or Windows computer. You can get Glade on Ubuntu Linux quite easy with sudo apt-get install glade, but more information about installing on the various other platforms is here.
  4. 在Mac,Linux或Windows计算机上安装Glade界面设计器。使用sudo apt-get install glade可以很容易地在Ubuntu Linux上获得Glade,但是有关在其他各种平台上安装的更多信息都在这里。

  5. Install GTKd on your Mac, Linux, or Windows computer. This is not easy. You will need to start with the documentation at gtkd.org and then interact in the DLang.org Learn forum for more assistance if necessary.
  6. 在Mac,Linux或Windows计算机上安装GTKd。这并不容易。您需要从gtkd.org上的文档开始,然后在DLang.org学习论坛中进行交互,以获得更多帮助。

  7. Open Glade and create a new window that has a label on it saying Hello World.
  8. 打开Glade并创建一个新窗口,上面有一个标签,上面写着Hello World。

Note that the tools palette in Glade shows a Window widget and an ApplicationWindow widget. Since we're not drawing any menus, ensure you're using the Window widget instead of the ApplicationWidget. If you fail to do that, you'll get warnings when running the application, talking about some missing menu calls.

请注意,Glade中的工具选项板显示了一个Window小部件和一个ApplicationWindow小部件。由于我们没有绘制任何菜单,因此请确保使用Window小部件而不是ApplicationWidget。如果你没有这样做,你会在运行应用程序时收到警告,谈论一些丢失的菜单调用。

       Save it as hello.glade.

将其保存为hello.glade。

  1. Open your hello.glade file in a text editor and look for a line that looks similar to this:
  2. 在文本编辑器中打开hello.glade文件,然后查找与此类似的行:

      <object class="GtkWindow" id="window1">

       Write down on a piece of paper that id attribute.

写下一张id属性的纸。

  1. Create a hello.d script in the same directory as this hello.glade file and alter the following contents, changing the window1 to whatever was your id you wrote down earlier.
  2. 在与此hello.glade文件相同的目录中创建一个hello.d脚本并更改以下内容,将window1更改为您之前记下的id。

import gtk.Builder;
import gtk.Main;
import gtk.Widget;
import gtk.Window;
import std.stdio;

int main (string[] args)
{
    Main.init(args);
    Builder b = new Builder();
    b.addFromFile("hello.glade");
    Window w = cast(Window)b.getObject("window1");
    w.addOnHide( delegate void(Widget aux){ Main.quit(); } );
    w.showAll();
    Main.run();
    return 0;
}
  1. Compiling is tricky. On Ubuntu Linux, I had to create a statement like the following. You may have to interact in the dlang.org Learn forums for your particular platform.
  2. 编译很棘手。在Ubuntu Linux上,我必须创建如下语句。您可能需要在dlang.org Learn论坛中与您的特定平台进行互动。

# dmd hello.d `pkg-config --cflags --libs gtkd3`
  1. Once compiled, you can run your D executable to show the Hello World dialog. On Ubuntu Linux, I simply did:
  2. 编译完成后,您可以运行D可执行文件以显示Hello World对话框。在Ubuntu Linux上,我只是做了:

       # ./hello

  1. Note that when you see the window and close it, you may receive some warnings on Linux. (On other platforms, this may vary.) I was receiving a warning about "Couldn't connect to accessibility bus - connection refused". The fix at least on Linux is to add this to your ~/.bashrc script at the bottom:
  2. 请注意,当您看到窗口并关闭它时,您可能会在Linux上收到一些警告。 (在其他平台上,这可能会有所不同。)我收到一条警告“无法连接到辅助功能总线 - 连接被拒绝”。至少在Linux上的修复是将它添加到底部的〜/ .bashrc脚本中:

       export NO_AT_BRIDGE=1

       Now when you open the command prompt and run your compiled D command "hello" again,        it will not show that error.

现在,当您打开命令提示符并再次运行已编译的D命令“hello”时,它将不会显示该错误。

       If you get errors regarding menus, then you used an ApplicationWindow widget instead of a        Window widget, and will need to switch that in Glade.

如果您收到有关菜单的错误,那么您使用了ApplicationWindow小部件而不是Window小部件,并且需要在Glade中切换它。

Adding Buttons & Signals

  1. The way I do it is to click on a widget in Glade, click Signals, find the event I want to add, such as clicked, and then in the Handler column, type in a function. For example, on a button1, I would type onButton1Clicked. Save the file.

    我这样做的方法是单击Glade中的小部件,单击Signals,找到我要添加的事件,例如单击,然后在Handler列中输入一个函数。例如,在button1上,我会输入onButton1Clicked。保存文件。

  2. Now, in your D source code, right after you create your Window object, add this code:

    现在,在D源代码中,在创建Window对象之后,添加以下代码:

       b.connectSignals(null);

       ...where b is your Builder variable.

...其中b是您的Builder变量。

  1. In your D source code, add a function for this signal. For instance, I did:
  2. 在D源代码中,为此信号添加一个函数。例如,我做了:

extern(C) void onButton1Clicked()
{
    writeln("got here");
    Main.quit();
}

       Note that in this case, the extern(C) is required.

注意,在这种情况下,需要extern(C)。

  1. Recompile and run your application. You'll see that it automatically runs your new function for that button click.
  2. 重新编译并运行您的应用程序。你会看到它会自动运行你点击该按钮的新功能。

#1


11  

  1. Install the DMD compiler that compiles the D language on your Mac, Linux, or Windows computer. You can get more info about this here.
  2. 安装在Mac,Linux或Windows计算机上编译D语言的DMD编译器。你可以在这里获得更多相关信息。

  3. Install the Glade interface designer on your Mac, Linux, or Windows computer. You can get Glade on Ubuntu Linux quite easy with sudo apt-get install glade, but more information about installing on the various other platforms is here.
  4. 在Mac,Linux或Windows计算机上安装Glade界面设计器。使用sudo apt-get install glade可以很容易地在Ubuntu Linux上获得Glade,但是有关在其他各种平台上安装的更多信息都在这里。

  5. Install GTKd on your Mac, Linux, or Windows computer. This is not easy. You will need to start with the documentation at gtkd.org and then interact in the DLang.org Learn forum for more assistance if necessary.
  6. 在Mac,Linux或Windows计算机上安装GTKd。这并不容易。您需要从gtkd.org上的文档开始,然后在DLang.org学习论坛中进行交互,以获得更多帮助。

  7. Open Glade and create a new window that has a label on it saying Hello World.
  8. 打开Glade并创建一个新窗口,上面有一个标签,上面写着Hello World。

Note that the tools palette in Glade shows a Window widget and an ApplicationWindow widget. Since we're not drawing any menus, ensure you're using the Window widget instead of the ApplicationWidget. If you fail to do that, you'll get warnings when running the application, talking about some missing menu calls.

请注意,Glade中的工具选项板显示了一个Window小部件和一个ApplicationWindow小部件。由于我们没有绘制任何菜单,因此请确保使用Window小部件而不是ApplicationWidget。如果你没有这样做,你会在运行应用程序时收到警告,谈论一些丢失的菜单调用。

       Save it as hello.glade.

将其保存为hello.glade。

  1. Open your hello.glade file in a text editor and look for a line that looks similar to this:
  2. 在文本编辑器中打开hello.glade文件,然后查找与此类似的行:

      <object class="GtkWindow" id="window1">

       Write down on a piece of paper that id attribute.

写下一张id属性的纸。

  1. Create a hello.d script in the same directory as this hello.glade file and alter the following contents, changing the window1 to whatever was your id you wrote down earlier.
  2. 在与此hello.glade文件相同的目录中创建一个hello.d脚本并更改以下内容,将window1更改为您之前记下的id。

import gtk.Builder;
import gtk.Main;
import gtk.Widget;
import gtk.Window;
import std.stdio;

int main (string[] args)
{
    Main.init(args);
    Builder b = new Builder();
    b.addFromFile("hello.glade");
    Window w = cast(Window)b.getObject("window1");
    w.addOnHide( delegate void(Widget aux){ Main.quit(); } );
    w.showAll();
    Main.run();
    return 0;
}
  1. Compiling is tricky. On Ubuntu Linux, I had to create a statement like the following. You may have to interact in the dlang.org Learn forums for your particular platform.
  2. 编译很棘手。在Ubuntu Linux上,我必须创建如下语句。您可能需要在dlang.org Learn论坛中与您的特定平台进行互动。

# dmd hello.d `pkg-config --cflags --libs gtkd3`
  1. Once compiled, you can run your D executable to show the Hello World dialog. On Ubuntu Linux, I simply did:
  2. 编译完成后,您可以运行D可执行文件以显示Hello World对话框。在Ubuntu Linux上,我只是做了:

       # ./hello

  1. Note that when you see the window and close it, you may receive some warnings on Linux. (On other platforms, this may vary.) I was receiving a warning about "Couldn't connect to accessibility bus - connection refused". The fix at least on Linux is to add this to your ~/.bashrc script at the bottom:
  2. 请注意,当您看到窗口并关闭它时,您可能会在Linux上收到一些警告。 (在其他平台上,这可能会有所不同。)我收到一条警告“无法连接到辅助功能总线 - 连接被拒绝”。至少在Linux上的修复是将它添加到底部的〜/ .bashrc脚本中:

       export NO_AT_BRIDGE=1

       Now when you open the command prompt and run your compiled D command "hello" again,        it will not show that error.

现在,当您打开命令提示符并再次运行已编译的D命令“hello”时,它将不会显示该错误。

       If you get errors regarding menus, then you used an ApplicationWindow widget instead of a        Window widget, and will need to switch that in Glade.

如果您收到有关菜单的错误,那么您使用了ApplicationWindow小部件而不是Window小部件,并且需要在Glade中切换它。

Adding Buttons & Signals

  1. The way I do it is to click on a widget in Glade, click Signals, find the event I want to add, such as clicked, and then in the Handler column, type in a function. For example, on a button1, I would type onButton1Clicked. Save the file.

    我这样做的方法是单击Glade中的小部件,单击Signals,找到我要添加的事件,例如单击,然后在Handler列中输入一个函数。例如,在button1上,我会输入onButton1Clicked。保存文件。

  2. Now, in your D source code, right after you create your Window object, add this code:

    现在,在D源代码中,在创建Window对象之后,添加以下代码:

       b.connectSignals(null);

       ...where b is your Builder variable.

...其中b是您的Builder变量。

  1. In your D source code, add a function for this signal. For instance, I did:
  2. 在D源代码中,为此信号添加一个函数。例如,我做了:

extern(C) void onButton1Clicked()
{
    writeln("got here");
    Main.quit();
}

       Note that in this case, the extern(C) is required.

注意,在这种情况下,需要extern(C)。

  1. Recompile and run your application. You'll see that it automatically runs your new function for that button click.
  2. 重新编译并运行您的应用程序。你会看到它会自动运行你点击该按钮的新功能。