D 类、OOP 和 GTKd 笔记本

标签 d gtkd

我正在尝试使用下面第二类 (MyNewClass) 中的 gtkd 将页面附加到 gtk 笔记本。 notebook 在第一个类 main_window 中创建,并由第二个类调用。程序编译正常,但是当我打开程序时,gtk 主窗口除了测试框外是空白的。

import gtk.Box;
import gtk.Button;
import gtk.Grid;
import gtk.Label;
import gtk.MainWindow;
import gtk.Main;
import gtk.Notebook;

class main_window : MainWindow
{
  Notebook notebook;

  this()
  {
super("MyProg");
    setDefaultSize(600,100);

       //Here is the creation of the notebook
 this.notebook=new Notebook;

Box tester=new Box(Orientation.VERTICAL, 1);  
notebook.appendPage(tester, new Label("test")); //This works fine from this class

 Grid grid=new Grid();
 grid.setColumnSpacing(12);  //establish the main grid
 grid.setRowSpacing(3);
 grid.attach(notebook, 0,0,1,9);
add(grid);
showAll();
  }
}

class MyNewClass : main_window
{
    this()
    {
File MFile = File("file.txt", "r");

    Grid MGrid;
  int row=0;
 int col=0;  //Set the column and row number for the gtk grid.
  string[] list;
 string i;
float p;
Label MLabel;


          while(!MFile.eof)
          {
             if (match(line, `\[\[`)){
             MGrid=new Grid();
        MGrid.setColumnSpacing(12);
         MGrid.setRowSpacing(3);
             row=0;

             line=replace(line, regex(r"(\[)", "g"), "");
            line=replace(line, regex(r"(\])", "g"), "");

                //I HAVE USED A TEST WRITELN HERE TO MAKE SURE THE FUNCTION IS CALLED.
                //Below is the notebook append that fails. When I test it from the first class above, I can append. When I call it here, it compiles but nothing is done.

                Box MBox=new Box(Orientation.VERTICAL, 1);
                MBox.add(MGrid);
         super.notebook.appendPage(MBox, new Label(line));//
          }
      }
}

void main(string[] args)
{
Main.init(args);
new main_window;

new M;
Main.run();
}

最佳答案

虽然代码无法编译,但您的页面未显示的原因是您需要调用 MBox.show() 来显示框。

Box MBox = new Box(Orientation.VERTICAL, 1);
MBox.add(MGrid);
super.notebook.appendPage(MBox, new Label(line));
MBox.show();

您还可以在容器小部件上调用 showAll 以显示所有子小部件。在您的情况下,这是笔记本、包含笔记本或主窗口的网格。所以你可以在循环之后添加 notebook.showAll() 来实现同样的事情。

附带说明一下,您可能希望遵循 D 风格来编写 D 代码:http://dlang.org/dstyle.html .

关于D 类、OOP 和 GTKd 笔记本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24073341/

相关文章:

c++ - 如何将对象从 D 传递到 C++?

templates - 关于 postblit 和 move 语义的问题

d - 开始使用 gtkd

multithreading - 带有 gtkD 的多线程应用程序

caching - 自动过期字典

d - 拆分/拆分的编译问题

d - GtkD(D语言的Gtk+绑定(bind))为什么要编译它?

gtk - 在 D 中,如何传递一个空字符串? (对 gtkD)

templates - dlang 模板和模板化类、结构和函数之间的区别

d - 在 Ubuntu 10.04 上设置有效的 D2.x 工具链(使用 gtkd)