0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Gtk
class ourwindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="My Hello World Program")
Gtk.Window.set_default_size(self, 400,325)
Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER)
button1 = Gtk.Button("Hello, World!")
button1.connect("clicked", self.whenbutton1_clicked)
self.add(button1)
def whenbutton1_clicked(self, button):
print "Hello, World!"
window = ourwindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
這段Python + GTK代碼是給我下面的錯誤:PyGobject錯誤
./pygtk.py
./pygtk.py:3: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded.
from gi.repository import Gtk
Traceback (most recent call last):
File "./pygtk.py", line 4, in <module>
class ourwindow(Gtk.Window):
File "./pygtk.py", line 10, in ourwindow
button1.connect("clicked", self.whenbutton1_clicked)
NameError: name 'self' is not defined
這也給了我一個indentaion錯誤。 我是Python和GTK的新手。 在此先感謝。
PyGObject可以與GTK + 2和GTK + 3一起使用,您需要選擇一個。你想使用哪一個? (請注意,PyGTK是完全不同的)。Python有嚴格的縮進規則。我建議您閱讀[Python教程](https://docs.python.org/3/tutorial/)(本教程涵蓋Python 3)和[Python GTK +教程](http:// python-gtk-3- tutorial.readthedocs.io/en/latest/)(這涵蓋了GTK + 3,這是我推薦的版本)。 – andlabs
這是你的代碼如何縮進?如果沒有,那麼你可以格式化它以匹配原始? –
@TeemuRisikko「這也給我一個indentaion錯誤。」意味着他們的代碼是如何縮進的,這進一步暗示他們對於Python來說太新了。 – andlabs