簡體   English   中英

源代碼不會在 python 2.7.10 中運行

[英]source code won't run in python 2.7.10

我是 Python 新手,在第 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 中的這個問題? 我不知所措。

原始代碼,除了“from tkinter”被更改為“from 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()

我沒有在任何項目中使用過 Tk,但我懷疑 Frame 不是使用新式類創建的,並且super()僅適用於新式 ( https://docs.python.org/2/library/函數.html#super )。 嘗試將您的__init__方法更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM