3

I'm a little bit lost building a UI with Glade and Python 3. I've made a GtkWindows, which has a GtkBox. The GtkBox has a GtkButton and two GtkPaned objects. Each of the panes have a GtkEntry object. Eventually, I want this to become a login form: the user hits a "Connect" button, and the text values of the two GtkEntry objects get picked up by a handler and sent off to a server. The relevant portion of my code looks like this:

class Handler:
    def on_MainWindow_destroy(self, *args):
        Gtk.main_quit(*args)

    def on_LoginButton_clicked(self, *args):
        print(*args)
        #do other stuff


if __name__ == '__main__':
    builder = Gtk.Builder()
    builder.add_from_file('myui.glade')
    builder.connect_signals(Handler())

    window = builder.get_object("MainWindow")
    window.show_all()

    Gtk.main()

(Slightly off the original topic of my question: what's the right way to name GObjects in Glade? CamelCase? lowercase_underscores?)

I want LoginButton to do something with the text of both fields when it gets clicked. However, Glade only gives you the option of passing a single object to the handler. I can attach the on_LoginButton_clicked method to LoginButton twice and pass the username field to it on the first call and the password field to it on the second call, but that seems very messy. What's the right way to do this?

4

1 回答 1

2

您需要使用 GtkBuilder 引用一个类中的两个对象get_object()。然后,在按钮的回调中,只需获取两个文本self.myfield1.get_text()。查看此模板以了解如何构建 PyGObject 应用程序:

如何构建使用 GUI 的程序?

希望能帮助到你。

编辑:关于命名方案,我会使用在使用 Glade 文件的语言上使用的任何命名方案。在 PyGObject 中编程时,我将 Glade 文件中的对象命名为与 Python 变量相同的名称,因此我可以这样做:

[...]
go = self.builder.get_object
self.window = go('window')
self.my_foobar = go('my_foobar')
[...]
于 2013-09-14T04:13:02.320 回答