我是蟒蛇新手,所遇到的第10章的內容如下:源代碼不會在Python運行2.7.10
當我運行在python 2.7.10代碼它給:
Traceback (most recent call last):
File "C:\Users\Dave\learnpython\py3e_source\chapter10\click_counter.py", line 32, in <module>
app = Application(root)
File "C:\Users\Dave\learnpython\py3e_source\chapter10\click_counter.py", line 10, in __init__
super(Application, self).__init__(master)
TypeError: must be type, not classobj
這本書寫的是理解python 3>會被使用。但是在2.7.10中我能做些什麼來解決這個問題嗎?我不知道該怎麼做。
原代碼,除了「從Tkinter的」被改爲「從Tkinter的」:
# Click Counter
# Demonstrates binding an event with an event handler
from Tkinter import *
class Application(Frame):
""" GUI application which counts button clicks. """
def __init__(self, master):
""" Initialize the frame. """
super(Application, self).__init__(master)
self.grid()
self.bttn_clicks = 0 # the number of button clicks
self.create_widget()
def create_widget(self):
""" Create button which displays number of clicks. """
self.bttn = Button(self)
self.bttn["text"]= "Total Clicks: 0"
self.bttn["command"] = self.update_count
self.bttn.grid()
def update_count(self):
""" Increase click count and display new total. """
self.bttn_clicks += 1
self.bttn["text"] = "Total Clicks: " + str(self.bttn_clicks)
# main
root = Tk()
root.title("Click Counter")
root.geometry("200x50")
app = Application(root)
root.mainloop()
我可以看到源代碼嗎? –
在原帖子底部添加Kevin。 –
謝謝,現在我們可以檢查出什麼問題了:) –