2015-11-21 75 views
0

我對Pygtk和Stackoverflow一般都很陌生。我正在嘗試構建一個小字典應用程序:我有一個主VBox和一個Hbox,其中包含兩個由Vseparator分隔的Vbox。我試圖通過右側Vbox中的標籤顯示文本,但不會顯示。這是我可憐的代碼:無法讓Pygtk在Hbox中顯示標籤

import gtk 
    import pygtk 
    from Wordlist import * 
    pygtk.require('2.0') 
     def click_handler(button): 
     for i in nouns: 
      print i 
      k = gtk.Label(str=i) 
      k.show() 
      meaningvbox.pack_start(k,True,True,0) 

    window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
    window.set_size_request(300,400) 
    window.set_title("English-Japanese Reference") 
    window.show() 
    window.connect("delete-event", gtk.main_quit) 
    vbox = gtk.VBox(False,0) 
    window.add(vbox) 
    vbox.show() 
    hbox = gtk.HBox(True,0) 
    vbox.pack_end(hbox,False) 
    hbox.show() 
    hbox2 = gtk.HBox(True,0) 
    vbox.pack_end(hbox2,False) 
    hbox2.show() 
    vsep = gtk.VSeparator() 
    vbox.pack_start(vsep) 
    vsep.show() 
    dichbox = gtk.HBox() #### These are boxes created to store the words 
    vbox.pack_start(dichbox) 
    wordvbox = gtk.VBox() 
    dichbox.pack_start(wordvbox) 
    wordvbox.show() 
    meaningvbox = gtk.VBox() 
    dichbox.pack_start(meaningvbox) 
    meaningvbox.show() 
    label = gtk.Label(str="hi") 
    meaningvbox.pack_start(label) 
    label.show() 
    verbButton = gtk.Button(label="Verbs") 
    hbox.pack_end(verbButton,True,False) 
    verbButton.set_size_request(100,30) 
    verbButton.show() 
    nounButton = gtk.Button(label="Nouns") 
    nounButton.set_size_request(100,30) 
    hbox.pack_end(nounButton,True,False) 
    nounButton.show() 
    nounButton.connect("clicked", click_handler) 
    familyButton = gtk.Button(label="Family") 
    familyButton.set_size_request(100,30) 
    hbox.pack_end(familyButton,True,False) 
    familyButton.show() 
    particleButton = gtk.Button(label="Particles") 
    hbox2.pack_end(particleButton,True,False) 
    particleButton.set_size_request(100,30) 
    particleButton.show() 
    adjectiveButton = gtk.Button(label="Adjectives") 
    adjectiveButton.set_size_request(100,30) 
    hbox2.pack_end(adjectiveButton,True,False) 
    adjectiveButton.show() 
    pronounButton = gtk.Button(label="Pronouns") 
    pronounButton.set_size_request(100,30) 
    hbox2.pack_end(pronounButton,True,False) 
    pronounButton.show() 
    def main(): 
     gtk.mainloop() 

    main() 

回答

0

你忘了撥打dichbox.show()

我也建議重組你的代碼和組類似的函數調用,使用show_all()而不是許多show() s和使用一個類的整個窗口。

這就是它會是什麼樣子:

import pygtk 
pygtk.require('2.0') 
import gtk 
from Wordlist import * 

class Window(gtk.Window): 
    def __init__(self): 
     gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL) 
     self.set_size_request(300,400) 
     self.set_title("English-Japanese Reference") 
     self.connect("delete-event", gtk.main_quit) 

     verbButton = gtk.Button(label="Verbs") 
     nounButton = gtk.Button(label="Nouns") 
     nounButton.connect("clicked", self.click_handler) 
     familyButton = gtk.Button(label="Family") 
     particleButton = gtk.Button(label="Particles") 
     adjectiveButton = gtk.Button(label="Adjectives") 
     pronounButton = gtk.Button(label="Pronouns") 

     verbButton.set_size_request(100,30) 
     nounButton.set_size_request(100,30) 
     familyButton.set_size_request(100,30) 
     particleButton.set_size_request(100,30) 
     adjectiveButton.set_size_request(100,30) 
     pronounButton.set_size_request(100,30) 

     hbox = gtk.HBox(True,0) 
     hbox.pack_end(verbButton, True, False) 
     hbox.pack_end(nounButton, True, False) 
     hbox.pack_end(familyButton, True, False) 

     hbox2 = gtk.HBox(True,0) 
     hbox2.pack_end(particleButton, True, False) 
     hbox2.pack_end(adjectiveButton, True, False) 
     hbox2.pack_end(pronounButton, True, False) 

     label = gtk.Label("hi") 
     self.meaningvbox = gtk.VBox() 
     self.meaningvbox.pack_start(label) 
     wordvbox = gtk.VBox() 
     vsep = gtk.VSeparator() 
     dichbox = gtk.HBox() 
     dichbox.pack_start(wordvbox) 
     dichbox.pack_start(vsep) 
     dichbox.pack_start(self.meaningvbox) 


     vbox = gtk.VBox(False, 0) 
     vbox.pack_end(hbox, False) 
     vbox.pack_end(hbox2, False) 
     vbox.pack_start(dichbox) 

     self.add(vbox) 
     self.show_all() 

    def click_handler(self,button): 
     for i in nouns: 
      k = gtk.Label(i) 
      k.show() 
      self.meaningvbox.pack_start(k, True, True, 0) 

def main(): 
    win = Window() 
    gtk.main() 

main()