python 에러

세벌의 이미지

ee.py

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
 
	def __init__(self):
		Gtk.window.__init__(self, title="Hello PyGtk")
 
		self.button=Gtk.Button(label="Label")
		self.button.connect("clicked", self.on_button_clicked)
		self.add(self.button)
 
	def on_button_clicked(self, widget):
		print("Hello PyGtk 3")
 
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

python3 ee.py

하면 아래 에러가 납니다.

Traceback (most recent call last):
  File "ee.py", line 16, in <module>
    win = MyWindow()
  File "ee.py", line 7, in __init__
    Gtk.window.__init__(self, title="Hello PyGtk")
  File "/usr/lib/python3/dist-packages/gi/overrides/__init__.py", line 39, in __getattr__
    return getattr(self._introspection_module, name)
  File "/usr/lib/python3/dist-packages/gi/module.py", line 139, in __getattr__
    self.__name__, name))
AttributeError: 'gi.repository.Gtk' object has no attribute 'window'
redneval의 이미지

     Gtk.window.__init__(self, title="Hello PyGtk")

여기서 window를 Window로 고치면 될 겁니다.

세벌의 이미지

고맙습니다. 대문자 W 였네요. 소문자 w와 비슷해서 눈에 띄지 않았었군요.

세벌의 이미지

위 코드에서 몇 글자를 한글로 바꾸어 보았습니다.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
 
	def __init__(self):
		Gtk.Window.__init__(self, title="안녕 PyGtk")
 
		self.button=Gtk.Button(label="Label")
		self.button.connect("clicked", self.on_button_clicked)
		self.add(self.button)
 
	def on_button_clicked(self, widget):
		print("안녕하쇼 PyGtk 3")
 
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

이 코드를
python ee.py
하면 에러가 나고

python3 ee.py
하면 에러가 안 나네요.
python3 에서 utf-8 코드를 더 쉽게 다룰 수 있게 되었네요.

2minchul의 이미지

첫 줄에

# -*- coding: utf-8 -*-

를 삽입하시면 python2에서도 작동할것 같습니다.


이민철 올림