2013-05-14 89 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以外的其他屬性,以便這些小部件不佔用最大值空間。

相關問題