2013-05-14 49 views
0

所以我试图用python GTK构建一个非常基本的GUI,但我很快就感到沮丧。如何将我的标签放在我的文字输入旁边?我怎样才能让这个项目更小?按钮更小?我将不得不添加更多条目,并一次性全部处理这些信息,而且我不一定需要它们填满窗口......谢谢。把一个标签放在python gtk文本条目旁边

#!/usr/bin/python 
#-*- coding: iso-8859-1 -* 
import pygtk 
pygtk.require('2.0') 
import gtk 


class text_box: 
    #Callback function, data arguments are ignored 
    def hello(self, widget, entry): 
     entry_text = self.entry.get_text() 
     print("Entry contents: ".format(entry_text)) 


    def delete_event(self, widget, event, data=None): 
     #Return of FALSE deletes event, True keeps it 
     print("Delete even occurred") 
     return False 

    def submit(self, button): 
     try: 
      input = self.entry.get_text() 
      print(float(input)) 
      return input 
     except ValueError: 
      print("This is not a number...") 
      self.md = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "This is not a number") 
      self.md.set_position(gtk.WIN_POS_CENTER) 
      self.md.run() 
      self.md.destroy() 


    def enter(self, button): 
     try: 
      input = self.entry.get_text() 
      input = float(input) 
      print(input) 
      return input 
     except ValueError: 
      self.md = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "This is not a number") 
      self.md.run() 
      self.md.destroy() 
      print("This is not a number...") 



    #Another Callback 
    def destroy(self, widget, data=None): 
     gtk.main_quit() 

    def __init__(self): 

     self.fix = gtk.Fixed() 

     #create a new window 
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     self.window.set_size_request(500, 500) 
     self.window.set_title("Powder Application") 
     self.window.set_position(gtk.WIN_POS_CENTER) 
     vbox = gtk.VBox(False,0) 
     self.window.add(vbox) 
     vbox.show() 

     #When window is given delete_event, close 
     self.window.connect("delete_event", self.delete_event) 

     #Connect the "destroy" event to a signal handler 
     #Occurs with gtk_widget_destroy() or False in delete_event 
     self.window.connect("destroy", self.destroy) 

     #Sets border width of window 
     self.window.set_border_width(10) 

     #Creates button 
     self.button = gtk.Button("Submit") 
     #self.button.connect("clicked", self.hello, None) 

     #Submit data in window on click 
     self.button.connect_object("clicked", self.submit, self.window) 


     #Make entry box 
     self.entry = gtk.Entry() 
     self.label = gtk.Label("Powder Density") 
     vbox.pack_start(self.label, False, False, 0) 
     self.label.show() 
     self.entry.set_max_length(20) 
     self.entry.select_region(0, len(self.entry.get_text())) 
     #self.entry.connect("activate", self.hello, self.entry) 
     self.entry.connect_object("activate", self.enter, self.window) 
     vbox.pack_start(self.entry, False, False, 0) 
     self.entry.show() 

     #This packs the button and entry into the window 
     #self.window.add(self.button) 
     #self.window.add(self.entry) 

     #hbox = gtk.HBox(False, 0) 
     #vbox.add(hbox) 
     #hbox.show() 

     #The final step is to display this newly created widget. 
     vbox.pack_start(self.button, False, False, 00) 
     self.button.show() 

     #And the window 
     self.window.show() 

    def main(self): 
     #All PyGTK apps must have a gtk.main(). Control ends here 
     #and waits for an event to occur 
     gtk.main() 
     return 0 


#If the program is run, create a gui instance and show it 
if __name__ == "__main__": 
    hello = text_box() 
    hello.main() 

回答

0

尝试使用Table,并使用表添加到窗口:

table=Gtk.Table(1,4,True) 
table.attach(label,0,1,0,1) 
table.attach(entry,1,3,0,1) 
table.attach(button,3,4,0,1) 
window.add(table) 
window.show_all() 

这显示文本框旁边的标签,与按钮旁边它

+0

“这显示了文本旁边的标签bo [x]”所以它确实......但它将它垂直和水平居中(不需要),而我的按钮不在哪里可以找到......数字是多少相当于?我可以拿到我的按钮,但它占据了整个窗口的一面,再次,不想要。 –

+0

这会在文本框旁边显示标签,旁边有按钮,并且该按钮与整个窗口一样高。这些数字对应什么? –

+0

所有这三个对象的大小将与窗口大小相同。如果要减少它,请增加行数和列数。至于数字,请参阅我发布的链接 –

0

您可以设置hexpandvexpand属性为Falsehalignvalign属性为Gtk.Align.FILL以外的其他属性,以便这些小部件不占用最大值空间。

相关问题