2015-11-08 77 views
2

我是蟒蛇新手,所遇到的第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() 
+0

我可以看到源代碼嗎? –

+0

在原帖子底部添加Kevin。 –

+0

謝謝,現在我們可以檢查出什麼問題了:) –

回答

2

我沒有在任何項目中使用TK,但我懷疑是不是創建框架使用新型類,super()僅適用於新款(https://docs.python.org/2/library/functions.html#super)。嘗試將您的__init__方法更改爲:

def __init__(self, master): 
    """ Initialize the frame. """ 
    Frame.__init__(self, master) # <-- CHANGED 
    self.grid() 
    self.bttn_clicks = 0 
    self.create_widget() 
+0

太棒了。非常感謝。現在我可以回到學習研究:)感謝大家的幫助。戴夫 –

+0

你是對的:tkinter類是舊式類。 –