我在林间空地做了这段代码:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.0 on Wed Apr 30 19:25:13 2014 -->
<interface>
<!-- interface-requires gtk+ 3.10 -->
<object class="GtkListStore" id="dinero">
<columns>
<!-- column-name extraer -->
<column type="gint"/>
</columns>
<data>
<row>
<col id="0">20</col>
</row>
<row>
<col id="0">50</col>
</row>
<row>
<col id="0">100</col>
</row>
<row>
<col id="0">200</col>
</row>
<row>
<col id="0">300</col>
</row>
<row>
<col id="0">500</col>
</row>
</data>
</object>
<object class="GtkListStore" id="tipo_cuenta">
<columns>
<!-- column-name cuenta -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Cuenta Corriente</col>
</row>
<row>
<col id="0" translatable="yes">Cuenta de Ahorro</col>
</row>
</data>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="orientation">vertical</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">80</property>
<property name="label" translatable="yes">Extraer</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="comboextraer">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="model">dinero</property>
<property name="wrap_width">3</property>
<property name="row_span_column">3</property>
<property name="column_span_column">3</property>
<property name="active">0</property>
<property name="button_sensitivity">on</property>
<property name="id_column">0</property>
<property name="active_id">extraer</property>
<child>
<object class="GtkCellRendererText" id="lista_extraer"/>
<attributes>
<attribute name="visible">0</attribute>
<attribute name="text">0</attribute>
<attribute name="wrap-width">2</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">True</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="box4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">76</property>
<property name="label" translatable="yes">Tipo de cuenta</property>
<attributes>
<attribute name="font-desc" value="<Introducir valor> 10"/>
<attribute name="scale" value="1"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="comboextraer1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="model">tipo_cuenta</property>
<property name="wrap_width">3</property>
<property name="row_span_column">3</property>
<property name="column_span_column">3</property>
<property name="active">0</property>
<property name="button_sensitivity">on</property>
<property name="id_column">0</property>
<property name="active_id">extraer</property>
<child>
<object class="GtkCellRendererText" id="lista_extraer1"/>
<attributes>
<attribute name="visible">0</attribute>
<attribute name="text">0</attribute>
<attribute name="wrap-width">2</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">True</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>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="boton">
<property name="label" translatable="yes">Sacar dinero</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="margin_left">3</property>
<property name="margin_right">6</property>
<property name="margin_top">9</property>
<property name="margin_bottom">6</property>
<property name="relief">half</property>
<property name="image_position">bottom</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="verificar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">33</property>
<property name="margin_right">40</property>
<property name="label" translatable="yes">Verificar</property>
<attributes>
<attribute name="scale" value="1"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="Deposito">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">74</property>
<property name="margin_right">64</property>
<property name="label" translatable="yes">Deposito</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
我用Vala语言编写了这段代码:
using Gtk;
class Cajero : GLib.Object {
public Cajero(){
int deposito = 3000;
var builder = new Builder();
builder.add_from_file("cajero.ui");
builder.connect_signals(null);
var ventana = builder.get_object("window1") as Window;
ventana.title = "Cajero Automático";
ventana.destroy.connect(Gtk.main_quit);
var contenedor = builder.get_object("box1") as Box;
var caja1 = builder.get_object("box2") as Box;
var caja2 = builder.get_object("box3") as Box;
var label1 = builder.get_object("label1") as Label;
var combo_extraer = builder.get_object("comboextraer") as ComboBox;
var lista_extraer = builder.get_object("lista_extraer") as CellRendererText;
var caja3 = builder.get_object("box4") as Box;
var label2 = builder.get_object("label2") as Label;
var combo_cuenta = builder.get_object("comboextraer1") as ComboBox;
var lista_cuenta = builder.get_object("lista_extraer1") as CellRendererText;
var caja4 = builder.get_object("box5") as Box;
var boton = builder.get_object("boton") as Button;
var label3 = builder.get_object("verificar") as Label;
var label4 = builder.get_object("Deposito") as Label;
boton.clicked.connect(() => {
int extraer = combo_extraer.get_active();
int cuenta = combo_cuenta.get_active();
if(cuenta == 1){
if(extraer > 200){
label3.set_label("Incorrecto");
}else{
label3.set_label("Correcto");
deposito = deposito - extraer;
if(deposito < 0){
ventana.title = "Fuera de servicio";
deposito = deposito + extraer;
label4.set_label(deposito.to_string());
}else{
label4.set_label(deposito.to_string());
}
}
}else{
if(extraer > 400){
label3.set_label("Incorrecto");
}else{
label3.set_label("Correcto");
deposito = deposito - extraer;
if(deposito < 0){
ventana.title = "Fuera de servicio";
deposito = deposito + extraer;
label4.set_label(deposito.to_string());
}else{
label4.set_label(deposito.to_string());
}
}
}
});
ventana.show_all();
}
public static int main(string[] args) {
Gtk.init(ref args);
Cajero cajero = new Cajero();
Gtk.main();
return 0;
}
}
我不知道如何获取combo_extraer的元素值。我只使用此Vala代码获得活动元素位置,但我无法获得其值。
答案 0 :(得分:1)
您必须获取ListStore
使用的CombobBox
的值。
基本上,您在get_active_iter
上调用ComboBox
来获取活动条目的迭代器,然后使用该迭代器和要从列表存储中检索的列调用get_value
。
您的列表商店名为dinero
,您必须从构建器获取它,就像您已经为其他组件所做的那样。
example in valadoc显示了一般情况如何:
public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.ComboBox";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
// Create & fill a ListStore:
Gtk.ListStore list_store = new Gtk.ListStore (2, typeof (string), typeof (int));
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter, 0, "Burgenland", 1, 13);
list_store.append (out iter);
list_store.set (iter, 0, "Carinthia", 1, 17);
// The Box:
Gtk.ComboBox box = new Gtk.ComboBox.with_model (list_store);
this.add (box);
Gtk.CellRendererText renderer = new Gtk.CellRendererText ();
box.pack_start (renderer, true);
box.add_attribute (renderer, "text", 0);
box.active = 0;
renderer = new Gtk.CellRendererText ();
box.pack_start (renderer, true);
box.add_attribute (renderer, "text", 1);
box.active = 0;
box.changed.connect (() => {
Value val1;
Value val2;
box.get_active_iter (out iter);
list_store.get_value (iter, 0, out val1);
list_store.get_value (iter, 1, out val2);
stdout.printf ("Selection: %s, %d\n", (string) val1, (int) val2);
});
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}