无法让Pygtk在Hbox中显示标签

时间:2015-11-21 23:07:21

标签: label pygtk hbox

我对Pygtk和Stackoverflow一般都是新手。我正在尝试构建一个小字典应用程序:我有一个主VBox和一个包含两个Vbox的Hbox由Vseparator划分。我试图通过右侧Vbox中的标签显示文本,但它不会出现。这是我可怜的代码:

    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()

1 个答案:

答案 0 :(得分:0)

您忘了拨打dichbox.show()

我还建议重新构建代码并对类似的函数调用进行分组,使用show_all()代替许多show(),并在整个窗口中使用类。

它会是什么样子:

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()