这两个window结构的区别?

Difference between these two window structure?

下面的两个代码片段都将使用 python 创建一个空的 Gtk window。然而,它们似乎完全不同。

两者的主要优势是什么? 选择一个与另一个相比是否对性能、安全或兼容性有影响?

第一个代码片段:

#!/usr/bin/python
from gi.repository import Gtk

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

第二个代码片段:

from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys

class GUI:
    def __init__(self):
        window = Gtk.Window()
        window.set_title ("Hello World")
        window.connect_after('destroy', self.destroy)

        window.show_all()

    def destroy(self, window):
        Gtk.main_quit()

def main():
    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

参考 1:1st snip-code reference

参考文献 2:2nd snip-code reference

第二个代码片段更面向对象,它定义了一个 class GUI,您可以为您的应用程序进行扩展,在我看来这是一个更优雅的解决方案。此外,它正确定义了一个 main() 函数并调用它,这为您提供了更大的灵活性,并允许您从其他地方导入此模块,而无需在导入时实例化 GUI。正如您所问,没有真正的性能、兼容性或安全隐患。

但是,在第二个片段中,我会将 window 绑定到 self (self.window = Gtk.Window()),从而允许您从 class 中的任何方法进行访问。