Gtk 为什么绘图区域不重新绘制?

Gtk 为什么绘图区域不重新绘制?,gtk,draw,vala,Gtk,Draw,Vala,我在瓦拉有这个代码: using Gtk; class Elecciones : GLib.Object { private Gtk.Window ventana; private Gtk.Entry partido1; private Gtk.Entry partido2; private Gtk.Entry partido3; private Gtk.DrawingArea tabla; public Elecciones(){

我在瓦拉有这个代码:

using Gtk;

class Elecciones : GLib.Object {
     private Gtk.Window ventana;
     private Gtk.Entry partido1;
     private Gtk.Entry partido2;
     private Gtk.Entry partido3;
     private Gtk.DrawingArea tabla;

     public Elecciones(){
            var builder = new Builder();
            builder.add_from_file("elecciones.ui");
            builder.connect_signals(null);
            var ventana = builder.get_object("window1") as Window;
            ventana.title = "Grafico electoral";
            ventana.destroy.connect(Gtk.main_quit);
            var contenedor = builder.get_object("contenedor") as Box;
            var caja1 = builder.get_object("caja1") as Box;
            var label1 = builder.get_object("label1") as Label;
            partido1 = builder.get_object("partido1") as Entry;
            var caja2 = builder.get_object("caja2") as Box;
            var label2 = builder.get_object("label2") as Label;
            partido2 = builder.get_object("partido2") as Entry;
            var caja3 = builder.get_object("caja3") as Box;
            var label3 = builder.get_object("partido3") as Label;
            partido3 = builder.get_object("partido3") as Entry;
            var boton = builder.get_object("boton") as Button;
            var tabla = builder.get_object("tabla") as DrawingArea;

            boton.clicked.connect(()=>{                     
                    int v1 = int.parse(partido1.get_text());
                    int v2 = int.parse(partido2.get_text());
                    int v3 = int.parse(partido3.get_text());
                    int total = v1+v2+v3;

                    int cuadro1 = v1*200/total;
                    int cuadro2 = v2*200/total;
                    int cuadro3 = v3*200/total;

                    string percent1 = (v1*100/total).to_string();
                    string percent2 = (v2*100/total).to_string();
                    string percent3 = (v3*100/total).to_string();

                    ventana.style_updated();
                    tabla.draw.connect((context)=>{
                            weak Gtk.StyleContext style_context = tabla.get_style_context();
                            int height = tabla.get_allocated_height();
                            int width = tabla.get_allocated_width();

                            context.set_source_rgba(0.144360,0.284024,0.839825,1);
                            context.rectangle(30, 0, cuadro1, 100);
                            context.fill();
                            context.set_source_rgba(1,1,1,1);
                             context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
                            context.set_font_size(16);
                            context.move_to(35,30);
                            context.show_text(percent1+"%");

                            context.set_source_rgba(0.980442,0.118050,0.000000,1);
                            context.rectangle(30+cuadro1, 0, cuadro2, 100);
                            context.fill();
                            context.set_source_rgba(1,1,1,1);           

                            context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
                            context.set_font_size(16);
                            context.move_to(35+cuadro1,30);
                            context.show_text(percent2+"%");

                            context.set_source_rgba(0,0.980442,0.092757,1);
                            context.rectangle(30+cuadro1+cuadro2,0,cuadro3, 100);
                            context.fill();
                            context.set_source_rgba(1,1,1,1);
                            context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
                            context.set_font_size(16);
                            context.move_to(35+cuadro1+cuadro2,30);
                            context.show_text(percent3+"%");

                            return true;
                     });
               });

               ventana.show_all();
      }

      public static int main(string[] args) {
            Gtk.init(ref args);
            Elecciones elecciones = new Elecciones();
            Gtk.main(); 
            return 0;
      }
}
这片空地的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.0 on Mon May  5 18:42:12 2014 -->
<interface>
<!-- interface-requires gtk+ 3.10 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">300</property>
<property name="default_height">400</property>
<child>
  <object class="GtkBox" id="contenedor">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="orientation">vertical</property>
    <child>
      <object class="GtkBox" id="caja1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="halign">start</property>
        <property name="margin_top">20</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="margin_left">20</property>
            <property name="hexpand">True</property>
            <property name="label" translatable="yes">Partido 1: </property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="partido1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="halign">start</property>
            <property name="margin_left">10</property>
            <property name="margin_right">120</property>
            <property name="hexpand">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkBox" id="caja2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="halign">start</property>
        <property name="margin_top">20</property>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="margin_left">20</property>
            <property name="hexpand">True</property>
            <property name="label" translatable="yes">Partido 2: </property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="partido2">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="halign">start</property>
            <property name="margin_left">10</property>
            <property name="margin_right">120</property>
            <property name="hexpand">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">1</property>
      </packing>
    </child>
    <child>
      <object class="GtkBox" id="caja3">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="halign">start</property>
        <property name="margin_top">20</property>
        <child>
          <object class="GtkLabel" id="label3">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="margin_left">20</property>
            <property name="hexpand">True</property>
            <property name="label" translatable="yes">Partido 3: </property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="partido3">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="halign">start</property>
            <property name="margin_left">10</property>
            <property name="margin_right">120</property>
            <property name="hexpand">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">2</property>
      </packing>
    </child>
    <child>
      <object class="GtkButton" id="boton">
        <property name="label" translatable="yes">Graficar</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="halign">start</property>
        <property name="margin_left">20</property>
        <property name="margin_top">20</property>
        <property name="hexpand">True</property>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">3</property>
      </packing>
    </child>
    <child>
      <object class="GtkDrawingArea" id="tabla">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_top">20</property>
        <property name="margin_bottom">20</property>
      </object>
      <packing>
        <property name="expand">True</property>
        <property name="fill">True</property>
        <property name="position">4</property>
      </packing>
    </child>
  </object>
</child>
</object>
</interface>

假的
300
400
真的
假的
垂直的
真的
假的
开始
20
真的
假的
开始
20
真的
第一部分:
假的
真的
0
真的
真的
开始
10
120
真的
假的
真的
1.
假的
真的
0
真的
假的
开始
20
真的
假的
开始
20
真的
第二部分:
假的
真的
0
真的
真的
开始
10
120
真的
假的
真的
1.
假的
真的
1.
真的
假的
开始
20
真的
假的
开始
20
真的
第三部分:
假的
真的
0
真的
真的
开始
10
120
真的
假的
真的
1.
假的
真的
2.
格拉菲卡
真的
真的
真的
开始
20
20
真的
假的
真的
3.
真的
假的
20
20
真的
真的
4.
当我点击Graficar按钮时,它会显示一个带有3个文本条目的百分比的条。但问题是,当我在任何条目中写入新百分比并再次单击Graficar按钮时,它不会用新日期更新栏


我终于解决了这个问题。问题是我在按钮信号内部发送信号。现在我把信号图放在外面,并在单击按钮时放置一个绘制的处理程序(activado)。这是正确的代码:

using Gtk;

class Elecciones : GLib.Object {
private Gtk.Window ventana;
private Gtk.Entry partido1;
private Gtk.Entry partido2;
private Gtk.Entry partido3;
private Gtk.DrawingArea tabla;
private bool activado = false;

public Elecciones(){
  var builder = new Builder();
  builder.add_from_file("elecciones.ui");
  builder.connect_signals(null);
  var ventana = builder.get_object("window1") as Window;
  ventana.title = "Grafico electoral";
  ventana.destroy.connect(Gtk.main_quit);
  var contenedor = builder.get_object("contenedor") as Box;
  var caja1 = builder.get_object("caja1") as Box;
  var label1 = builder.get_object("label1") as Label;
  partido1 = builder.get_object("partido1") as Entry;
  var caja2 = builder.get_object("caja2") as Box;
  var label2 = builder.get_object("label2") as Label;
  partido2 = builder.get_object("partido2") as Entry;
  var caja3 = builder.get_object("caja3") as Box;
  var label3 = builder.get_object("partido3") as Label;
  partido3 = builder.get_object("partido3") as Entry;
  var boton = builder.get_object("boton") as Button;
  var tabla = builder.get_object("tabla") as DrawingArea;
  boton.clicked.connect(()=>{   
    activado = true;
    ventana.style_updated();
  });   
  tabla.visible = true;
  tabla.draw.connect((context)=>{
    if(activado==true){ 
      weak Gtk.StyleContext style_context = tabla.get_style_context();
      int height = tabla.get_allocated_height();
      int width = tabla.get_allocated_width();
      int v1 = int.parse(partido1.get_text());
      int v2 = int.parse(partido2.get_text());
      int v3 = int.parse(partido3.get_text());
      int total = v1+v2+v3;
      int cuadro1 = v1*200/total;
      int cuadro2 = v2*200/total;
      int cuadro3 = v3*200/total;
      string percent1 = (v1*100/total).to_string();
      string percent2 = (v2*100/total).to_string();
      string percent3 = (v3*100/total).to_string();
      context.set_source_rgba(0.144360,0.284024,0.839825,1);
      context.rectangle(30, 0, cuadro1, 100);
      context.fill();
      context.set_source_rgba(1,1,1,1);
      context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
      context.set_font_size(16);
      context.move_to(35,30);
      context.show_text(percent1+"%");
      context.set_source_rgba(0.980442,0.118050,0.000000,1);
      context.rectangle(30+cuadro1, 0, cuadro2, 100);
      context.fill();
      context.set_source_rgba(1,1,1,1);
      context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
      context.set_font_size(16);
      context.move_to(35+cuadro1,30);
      context.show_text(percent2+"%");
      context.set_source_rgba(0,0.980442,0.092757,1);
      context.rectangle(30+cuadro1+cuadro2,0,cuadro3, 100);
      context.fill();
      context.set_source_rgba(1,1,1,1);
      context.select_font_face("serif",Cairo.FontSlant.NORMAL,Cairo.FontWeight.NORMAL);
      context.set_font_size(16);
      context.move_to(35+cuadro1+cuadro2,30);
      context.show_text(percent3+"%");  
    }
    return true;
  });
  tabla.queue_draw();
  ventana.show_all();
}
public static int main(string[] args) {
  Gtk.init(ref args);
  Elecciones elecciones = new Elecciones();
  Gtk.main();
  return 0;
}
}

所以问题是,
Gtk.drawingara
没有重新绘制?您应该编辑问题的标题,使其更有意义(如“为什么绘图区域没有重新绘制?”),并说几句为什么您的解决方案可以解决问题。这样,问题+答案将对未来的访问者更有帮助。