2012-06-22 4 views
0

파이썬과 Pygtk에서 새로운 기능이며 이전 드롭 다운 메뉴 항목에서 드롭 다운 메뉴 항목을 업데이트하는 방법을 모르겠습니다. 이전 콤보 상자 (cb1)의 선택에 따라 달라지는 콤보 상자 (cb2)의 목록 저장소를 설정하려고합니다. 그래서 이것은 cb1 사용자 선택에 달려 있기 때문에 이것이 동적 cb2라고 생각합니다.PyGTK : 이전 콤보 상자 드롭 다운 메뉴에서 콤보 상자 드롭 다운 메뉴 변경

cb1에는 항목 C1과 C2가 있습니다. C1을 선택하면 cb2 항목이 2,3 및 4가되도록합니다. C2를 선택하면 2,3,4 및 6 항목이 필요합니다.

나를 도와 줄 사람이 있습니까?

여기에 제가 지금까지 가지고있는 테스트 케이스 코드가 있습니다. 그러나 변화는 효과가 없습니다!

import pygtk 
pygtk.require("2.0") 
import gtk 

class test: 
    def __init__(self): 
     self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL) 
     self.window.connect("destroy", lambda w: gtk.main_quit()) 

     self.vbox_setup = gtk.VBox(False, 0) 
     self.vbox_setup.set_border_width(10) 
     self.window.add(self.vbox_setup) 
     self.conf_1() 

     self.window.show_all() 
     gtk.main() 

    def conf_1(self): 
     self.obs_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.obs_box, False, True, 0) 
     self.label = gtk.Label('Conf1') 
     self.obs_box.pack_start(self.label, False, True, 3) 
     self.label_lat = gtk.Label('Latitude: ?') 
     self.combobox_1 = gtk.ComboBox() 
     self.liststore = gtk.ListStore(str) 
     self.cell = gtk.CellRendererText() 
     self.combobox_1.pack_start(self.cell) 
     self.combobox_1.add_attribute(self.cell, 'text', 0) 
     self.obs_box.pack_start(self.combobox_1, False, True, 3) 
     self.liststore.append(['Select:']) 
     self.liststore.append(['C1']) 
     self.liststore.append(['C2']) 
     self.combobox_1.set_model(self.liststore) 
     self.combobox_1.connect('changed', self.conf1_choice) 
     self.combobox_1.connect('changed', self.conf2_choice) 
     self.combobox_1.set_active(0) 
     self.obs_box.pack_start(self.label_lat, False, True, 3) 

    def conf1_choice(self, combobox): 
     model = combobox.get_model() 
     index = combobox.get_active() 
     if model[index][0] == 'C1': 
      self.latitude = -4.23 
     elif model[index][0] == 'C2': 
      self.latitude = 45.22 
     else : 
      self.latitude = 0 
     lat_dgr = int(self.latitude) 
     lat_mn = int((self.latitude %1)*60.) 
     lat_s = ((self.latitude %1)*60. %1)*60. 
     self.label_lat.set_label('Latitude: '+str(lat_dgr)+u'\u00B0 '+ 
          str(lat_mn)+u'\u0027 '+str(round(lat_s,2))+u'\u0027\u0027') 

    def conf2_choice(self, widget): 
     self.configuration_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.configuration_box, False, True, 0) 
     self.label = gtk.Label('Configuration') 
     self.configuration_box.pack_start(self.label, False, True, 3) 
     self.combobox_2 = gtk.ComboBox() 
     self.liststore = gtk.ListStore(str) 
     self.cell = gtk.CellRendererText() 
     self.combobox_2.pack_start(self.cell) 
     self.combobox_2.add_attribute(self.cell, 'text', 0) 
     self.configuration_box.pack_start(self.combobox_2, False, True, 3) 
     self.liststore.append(['Select:']) 
     model = self.combobox_1.get_model() 
     index = self.combobox_1.get_active() 
     if model[index][0] == 'C1': 
      self.liststore.append(['2']) 
      self.liststore.append(['3']) 
      self.liststore.append(['4']) 
     if model[index][0] == 'C2': 
      self.liststore.append(['2']) 
      self.liststore.append(['3']) 
      self.liststore.append(['4']) 
      self.liststore.append(['6']) 
     self.combobox_2.set_model(self.liststore) 
     self.combobox_2.set_active(0) 
     self.combobox_2.connect('changed', self.conf3_choice) 

    def conf3_choice(self, widget): 
     pass 

if __name__ == "__main__": 
    uv = test() 

또한, 나는 또한 선택 CB1은 = C1과 CB2 = 2 항목 T1, T2와 T3 2 다른 콤보를 만들거나 내가 CB1을 선택하면 = C1과 CB2 = 4,은 4를 만들 것이라고합니다 항목 T1, T2 및 T3이있는 다른 콤보 상자. cb1 = C2이지만 항목 U1, U2, U3 및 U4와 동일합니다. 이러한 경우, *의 conf3_choice의 * 내 구현은 : 모든 콤보와 같은 모델 (self.liststore)을 공유하는

def conf3_choice(self, widget): 
    model_1 = self.combobox_1.get_model() 
    index_1 = self.combobox_1.get_active() 
    model_2 = self.combobox_2.get_model() 
    index_2 = self.combobox_2.get_active() 
    self.new_box_1 = gtk.VBox(False, 0) 
    self.vbox_setup.pack_start(self.new_box_1, False, True, 0) 
    self.new_box_2 = gtk.VBox(False, 0) 
    self.vbox_setup.pack_start(self.new_box_2, False, True, 0) 
    self.combobox_3 = gtk.ComboBox() 
    self.liststore = gtk.ListStore(str) 
    self.cell = gtk.CellRendererText() 
    self.combobox_3.pack_start(self.cell) 
    self.combobox_3.add_attribute(self.cell, 'text', 0) 
    self.liststore.append(['Select:']) 
    if model_1[index][0]=='C1': 
     self.liststore.append(['T1']) 
     self.liststore.append(['T2']) 
     self.liststore.append(['T3']) 
     self.combobox_3.set_model(self.liststore) 
     self.combobox_3.set_active(0) 
     if model_2[index][0]=='2': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='3': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='4': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
    if model_1[index][0]=='C2': 
     self.liststore.append(['U1']) 
     self.liststore.append(['U2']) 
     self.liststore.append(['U3']) 
     self.liststore.append(['U4']) 
     self.combobox_3.set_model(self.liststore) 
     self.combobox_3.set_active(0) 
     if model_2[index][0]=='2': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='3': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='4': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='6': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
+0

최소한의 테스트 케이스를 제공하는 것이 좋습니다. 읽고 이해하기 쉽고 문제를 쉽게 잡을 수 있으며 더 일반적입니다 (다른 사람들에게도 도움이됩니다). – gpoo

답변

0

. 마찬가지로 self.cell. 나중에 필요하다면 로컬 변수를 사용해야합니다. 나중에 get_model() 등으로 얻을 수 있습니다. 둘째, 콤보 상자가 변경 될 때마다 위젯을 만드는 것이 좋습니다. 더 나은 접근 방법은 처음에는 위젯을 만들고 필요에 따라 위젯을 활성화/비활성화하는 것입니다. 모든 경우에 모델을 업데이트하기 만하면됩니다 (liststore).

관련 기능을 함께 사용하기 위해 코드를 약간 재정렬하십시오. 지역 변수와 올바른 순서를 사용하면 코드가 작동하는 것을 알 수 있습니다. 3 번째와 4 번째 콤보 박스를 만들기 위해 여기에서 코드를 외삽 할 수 있습니다.

마지막으로 중요한 것은 두 번째 콤보 박스의 모델을 언제든지 추가하고 싶을 때 clear()입니다.

import pygtk 
pygtk.require("2.0") 
import gtk 

class test: 
    def __init__(self): 
     self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL) 
     self.window.connect("destroy", lambda w: gtk.main_quit()) 

     self.vbox_setup = gtk.VBox(False, 0) 
     self.vbox_setup.set_border_width(10) 
     self.window.add(self.vbox_setup) 
     self.create_comboxbox() 

     self.window.show_all() 
     gtk.main() 

    def create_comboxbox(self): 
     # Main Combo box 
     liststore = gtk.ListStore(str) 
     liststore.append(['Select:']) 
     liststore.append(['C1']) 
     liststore.append(['C2']) 

     self.obs_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.obs_box, False, True, 0) 
     label = gtk.Label('Conf1') 
     self.label_lat = gtk.Label('Latitude: ?') 

     self.combobox_1 = gtk.ComboBox() 
     cell = gtk.CellRendererText() 
     self.combobox_1.pack_start(cell) 
     self.combobox_1.add_attribute(cell, 'text', 0) 
     self.combobox_1.set_model(liststore) 
     self.combobox_1.set_active(0) 
     self.combobox_1.connect('changed', self.conf1_choice) 
     self.combobox_1.connect('changed', self.conf2_choice) 

     self.obs_box.pack_start(label, False, True, 3) 
     self.obs_box.pack_start(self.combobox_1, False, True, 3) 
     self.obs_box.pack_start(self.label_lat, False, True, 3) 

     # Secondary Combo box 
     liststore = gtk.ListStore(str) 
     liststore.append(['Select:']) 

     label = gtk.Label('Configuration') 
     self.combobox_2 = gtk.ComboBox() 

     cell = gtk.CellRendererText() 
     self.combobox_2.pack_start(cell) 
     self.combobox_2.add_attribute(cell, 'text', 0) 
     self.combobox_2.set_model(liststore) 
     self.combobox_2.set_active(0) 
     #self.combobox_2.connect('changed', self.conf3_choice) 

     self.obs_box.pack_start(label, False, True, 3) 
     self.obs_box.pack_start(self.combobox_2, False, True, 3) 

    def conf1_choice(self, combobox): 
     model = combobox.get_model() 
     index = combobox.get_active() 
     if model[index][0] == 'C1': 
      self.latitude = -4.23 
     elif model[index][0] == 'C2': 
      self.latitude = 45.22 
     else: 
      self.latitude = 0 
     lat_dgr = int(self.latitude) 
     lat_mn = int((self.latitude %1)*60.) 
     lat_s = ((self.latitude %1)*60. %1)*60. 
     self.label_lat.set_label('Latitude: '+str(lat_dgr)+u'\u00B0 '+ 
          str(lat_mn)+u'\u0027 '+str(round(lat_s,2))+u'\u0027\u0027') 

    def conf2_choice(self, widget): 
     model = self.combobox_1.get_model() 
     index = self.combobox_1.get_active() 
     liststore = self.combobox_2.get_model() 
     liststore.clear() 
     liststore.append(['Select:']) 
     if model[index][0] == 'C1': 
      liststore.append(['2']) 
      liststore.append(['3']) 
      liststore.append(['4']) 
     if model[index][0] == 'C2': 
      liststore.append(['2']) 
      liststore.append(['3']) 
      liststore.append(['4']) 
      liststore.append(['6']) 
     self.combobox_2.set_model(liststore) 
     self.combobox_2.set_active(0) 

if __name__ == "__main__": 
    uv = test() 
관련 문제