简体   繁体   English

在python gtk中的文本条目旁边放置标签

[英]Put a label beside a text entry in python gtk

So I'm trying to build a pretty basic GUI using python GTK, but I'm getting frustrated very quickly. 因此,我尝试使用python GTK构建一个非常基本的GUI,但是我很快就感到沮丧。 How do I put my label beside my text entry? 如何将标签放在文字输入旁边? How can I make the entry smaller? 我怎样才能缩小条目? The button smaller? 按钮较小? I'm going to have to add more entries and take them all in at once to process the information, and I don't necessarily need them to fill the window... Thanks. 我将不得不添加更多条目并将它们一次全部接收以处理信息,并且我不一定需要它们来填充窗口...谢谢。

#!/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()

Try using a Table and add the table to the window using: 尝试使用并将添加到窗口中,方法是:

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

This displays the label beside the text box, with the button beside it 这会在文本框旁边显示标签,旁边是按钮

您可以将hexpandvexpand属性设置为False ,并将halignvalign属性设置为hexpand vexpand其他Gtk.Align.FILL ,以便小部件不占用最大空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM