我有一个应用程序需要PYGObject与Gtk,Glib和Pango。
当我尝试在Windows上运行它时,我收到以下错误:
Traceback (most recent call last):
File "C:\Python27\lib\site.py", line 62, in <module>
import os
File "C:\Python27\lib\os.py", line 63, in <module>
import ntpath as path
File "C:\Python27\lib\ntpath.py", line 12, in <module>
import warnings
File "C:\Python27\lib\warnings.py", line 8, in <module>
import types
File "C:\Python27\Lib\site-packages\gi\types.py", line 28, in <module>
from ._constants import TYPE_INVALID
ValueError: Attempted relative import in non-package
我已经安装并重新安装了Python27,PySerial(需要其他地方)和PygObject。 PYTHONPATH包含所有相关目录和子目录。这些包括c:/ python27 / Lib / site-packages / Python27以及对gi,gi / override和gi / respository子目录的显式引用。
获得相同错误的python代码的最小版本(借用网络 - 感谢Liam)如下:
#!/usr/bin/env python
# A GTK Hello World example using GObject.
# based on an example by Liam Fraser for Raspberry Pi Tutorials
# Import the GTK libaries
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class helloGlade:
# Our helloGlade class
def __init__(self):
# create a builder
self.builder = Gtk.Builder()
self.builder.add_from_file ("helloGlade.glade")
# Create a new window
self.windowMain = self.builder.get_object("windowMain")
# Connect the window's close event ("destroy") to the default quit function
self.windowMain.connect("destroy", Gtk.main_quit)
self.windowMain.set_default_size(640,480)
self.windowMain.show()
def main(self):
# All PyGTK applications must have a gtk.main()
# gtk.main waits for an event to occur (like a key press or mouse event)
# and then runs the appropriate event handler
Gtk.main()
if __name__ == "__main__":
# Create an instance of the HelloWorld class and run the main function
application = helloGlade()
application.main()
任何人都可以提供帮助。非常感谢。