gpt4 book ai didi

python - 属性错误 : HelloWorld Instance has no attribute main

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:25 26 4
gpt4 key购买 nike

我收到这个错误

Traceback (most recent call last):
File "helloworld.py", line 66, in module
hello.main()
AttributeError: Helloworld instance has no attribute 'main'

我在 Linux 机器上运行下面给出的代码

#!/usr/bin/env python

# example helloworld.py
import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

# This is a callback function. The data arguments are ignored
# in this example. More on callbacks below.
def hello(self, widget, data=None):
print "Hello World"

def delete_event(self, widget, event, data=None):
# If you return FALSE in the "delete_event" signal handler,
# GTK will emit the "destroy" signal. Returning TRUE means
# you don't want the window to be destroyed.
# This is useful for popping up 'are you sure you want to quit?'
# type dialogs.
print "delete event occurred"

# Change FALSE to TRUE and the main window will not be destroyed
# with a "delete_event".
return False

def destroy(self, widget, data=None):
print "destroy signal occurred"
gtk.main_quit()

def __init__(self):
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

# When the window is given the "delete_event" signal (this is given
# by the window manager, usually by the "close" option, or on the
# titlebar), we ask it to call the delete_event () function
# as defined above. The data passed to the callback
# function is NULL and is ignored in the callback function.
self.window.connect("delete_event", self.delete_event)

# Here we connect the "destroy" event to a signal handler.
# This event occurs when we call gtk_widget_destroy() on the window,
# or if we return FALSE in the "delete_event" callback.
self.window.connect("destroy", self.destroy)

# Sets the border width of the window.
self.window.set_border_width(10)

# Creates a new button with the label "Hello World".
self.button = gtk.Button("Hello World")

# When the button receives the "clicked" signal, it will call the
# function hello() passing it None as its argument. The hello()
# function is defined above.
self.button.connect("clicked", self.hello, None)

# This will cause the window to be destroyed by calling
# gtk_widget_destroy(window) when "clicked". Again, the destroy
# signal could come from here, or the window manager.
self.button.connect_object("clicked", gtk.Widget.destroy, self.window)

# This packs the button into the window (a GTK container).
self.window.add(self.button)

# The final step is to display this newly created widget.
self.button.show()

# and the window
self.window.show()

def main(self):
# All PyGTK applications must have a gtk.main(). Control ends here
# and waits for an event to occur (like a key press or mouse event).
gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
hello = HelloWorld()
hello.main()

这是一个缩进问题吗?如何解决?我尝试在互联网上搜索,但没有帮助。这与 pyGTK 教程中给出的代码相同。请帮忙。

最佳答案

我只是将这段代码复制到我的 Linux 机器上并运行它,它运行良好,没有任何错误。你是从命令行运行这个吗?喜欢 $python helloworld.py 吗?或者您是否尝试通过 Python 控制台 session 运行它?

缩进似乎不是问题。您运行的是哪个版本的 Python?

关于python - 属性错误 : HelloWorld Instance has no attribute main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com