我正在尝试编写一些配置GUI,其中许多GUI都有两个或更多ComboBox,以及其他东西。我不喜欢复制东西,并且在任何地方制作它都是相同的,长的,几乎不可读的。 我想在这个问题上问你自己的话,创建sotcha类是否合适,如果没有,如何更好地处理它?</ p>
./ listaRozwijana-experymentalne.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Camelek.AmigaRulez
# License: Public Domain
# Date: poniedziałek, 14 listopad 2011 15:41:36
#
import gtk, gobject
class KlasaComboBox():
def __init__(self, comboboxprojekt, comboboxnazwa, comboboxlista):
self.cmb = comboboxprojekt.get_object(comboboxnazwa);
self.liststore = gtk.ListStore(gobject.TYPE_STRING);
for self.wpis in comboboxlista:
self.liststore.append([self.wpis]);
self.cmb.set_model(self.liststore);
self.komorka = gtk.CellRendererText();
self.cmb.pack_start(self.komorka, True);
self.cmb.add_attribute(self.komorka, 'text',0);
def get_active(self):
return(self.cmb.get_active());
def get_active_text(self):
return(self.cmb.get_active_text());
def wyswietl(self):
print 'Wybrałeś', self.cmb.get_active_text(), 'jego indeks na tej liście to:', self.cmb.get_active();
class KlasaGlowna():
def __init__(self):
self.projekt = gtk.Builder();
self.projekt.add_from_file("./gui/okienkoDwochListRozwijanych.ui");
self.okienko = self.projekt.get_object("window1");
self.projekt.connect_signals(self);
self.lista1 = ["Motorola 68000"];
self.lista1.append ("Intel MCS-51");
self.lista1.append ("Cell");
self.lista1.append ("Zilog Z80");
self.cmb1 = KlasaComboBox(self.projekt, "combobox1", self.lista1);
self.lista2 = ["Pierwszy", "Drugi", "Trzeci", "Czwarty", "Piąty"];
self.cmb2 = KlasaComboBox(self.projekt, "combobox2", self.lista2);
def uruchom(self):
self.okienko.show_all();
gtk.main();
def on_combobox1_changed(self, widget, data=None):
self.cmb1.wyswietl();
def on_combobox2_changed(self, widget, data=None):
self.cmb2.wyswietl();
def on_window1_delete_event(self, widget, event, data=None):
gtk.main_quit();
if __name__ == "__main__":
app = KlasaGlowna();
app.uruchom();
else:
sys.exit(1);
./ GUI / okienkoDwochListRozwijanych.ui
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="title" translatable="yes">Wybierz swój ulubiony procesor</property>
<property name="window_position">center</property>
<signal name="delete_event" handler="on_window1_delete_event"/>
<child>
<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkVButtonBox" id="vbuttonbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="layout_style">spread</property>
<child>
<object class="GtkComboBox" id="combobox1">
<property name="visible">True</property>
<signal name="changed" handler="on_combobox1_changed"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="combobox2">
<property name="visible">True</property>
<signal name="changed" handler="on_combobox2_changed"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
答案 0 :(得分:0)
你不是唯一有这个想法的人;创建gtk.ComboBoxText
是为了解决这个问题。它可以从PyGTK 2.24开始提供。如果你不能使用PyGTK 2.24,那么你的解决方案似乎很合理。