我確信icon.png與python文件在同一個工作目錄下。
這可能是你的問題 - 路徑相對於Python的解釋的工作目錄中查找,不包含代碼的文件。我經常發現自己定義喜歡的功能:
def get_resource_path(rel_path):
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
礦實際上並不是想象中的那麼冗長,但希望變量名說清楚是怎麼回事。另外,獲取絕對路徑並不是絕對必要的,但如果需要調試,可能會有所幫助。
然後,你可以這樣做:
self.set_icon_from_file(get_resource_path("icon.png"))
更新:這裏是一個演示程序。 「icon.png」和這個腳本位於同一個目錄,我使用./gtktest.py
來運行它。我看到左上角的圖標(我主題的標準位置)。 icon.png
只是在Inkscape中繪製的一個形狀,並以位圖的形式導出(無論如何,它也可以與原始SVG一起使用)。
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class HelloWorld:
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_icon_from_file('icon.png')
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
# Creates a new button with the label "Hello World".
self.button = gtk.Button("Hello World")
self.window.add(self.button)
self.button.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
hello = HelloWorld()
hello.main()
什麼是'self'? – detly 2010-12-11 10:54:49
我要求它使用「self」來設置當前窗口的程序圖標 – Nik 2010-12-11 11:06:45