Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python gtk中,在文本条目旁边放置一个标签_Python_User Interface_Gtk - Fatal编程技术网

在python gtk中,在文本条目旁边放置一个标签

在python gtk中,在文本条目旁边放置一个标签,python,user-interface,gtk,Python,User Interface,Gtk,所以我试图用PythonGTK构建一个非常基本的GUI,但是我很快就被挫败了。如何将标签放在文本条目旁边?如何使条目变小?按钮要小一点吗?我将不得不添加更多的条目,并一次将它们全部输入以处理信息,我不一定需要它们来填充窗口。。。谢谢 #!/usr/bin/python #-*- coding: iso-8859-1 -* import pygtk pygtk.require('2.0') import gtk class text_box: #Callback function, d

所以我试图用PythonGTK构建一个非常基本的GUI,但是我很快就被挫败了。如何将标签放在文本条目旁边?如何使条目变小?按钮要小一点吗?我将不得不添加更多的条目,并一次将它们全部输入以处理信息,我不一定需要它们来填充窗口。。。谢谢

#!/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()
尝试使用,并使用以下命令将表添加到窗口:

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

这将在文本框旁边显示标签,通过其旁边的按钮,您可以将
hexpand
vexpand
属性设置为
False
,将
halign
valign
属性设置为
Gtk.Align.FILL
以外的其他属性,以便小部件不会占用最大空间

“这会在文本bo[x]旁边显示标签。”确实如此……但它会垂直和水平居中(不需要)我的按钮不在哪里可以找到…数字对应什么?我可以把我的按钮放进去,但它占据了整个窗口的一侧,同样,也不需要。这会在文本框旁边显示标签,按钮在旁边,按钮与整个窗口一样高。数字对应什么?所有三个对象都有Well的大小与窗口的大小相同。如果您想减少它,请增加行和列的数量。至于数量,请参阅我发布的链接。谢谢您的帮助。