D类,OOP和GTKd笔记本

时间:2014-06-06 02:42:32

标签: d gtkd

我正在尝试使用下面第二个类(MyNewClass)中的gtkd将页面附加到gtk笔记本。笔记本是在第一个类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();
}

1 个答案:

答案 0 :(得分:2)

虽然代码无法编译,但您的网页未显示的原因是您需要调用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