python gtk poo 编程有多少文件?

python gtk poo programming many files?

我的程序代码越来越重,我想把它分成很多文件。

我找到了一个教程,其代码为:

#!/usr/bin/env python3
# coding: utf-8

#Box.py
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk,  GdkPixbuf

from BoxBoutton import BoxBoutton

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        box = Gtk.Box()
        sublayout = BoxBoutton()

        box.pack_start(sublayout, True, True, 0)
        self.add(box)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

第二个:

#!/usr/bin/env python3
# coding: utf-8

#BoxBoutton.py

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class BoxBoutton(Gtk.Grid):
    def __init__(self):
        Gtk.Grid.__init__(self)

        btn = Gtk.Button(label="Mon super bouton")
        self.attach(0, 0, 1, 1)

但是我有这个错误:

TypeError: Gtk.Grid.attach() takes exactly 6 arguments (5 given)

非常感谢您的帮助

您忘记了 attach method of Gtk.Grid 中的 child。

attach(child, left, top, width, height)

尝试以下操作:

self.attach(btn, 0, 0, 1, 1)