“ TypeError:Start()缺少1个必需的位置参数:'self'”

时间:2018-09-27 23:06:09

标签: python python-3.x tkinter

我不明白“ TypeError:Start()缺少1个必需的位置参数:'self'”的含义。我需要澄清此错误消息指的是什么以及修复它所需要的内容。

尽管this other Stack Overflow的问题答案是“需要实例化Application对象并在该对象上而不是在类上调用它”,但我完全不知道这种解释意味着要使用尽可能简单的语言。专门需要哪种代码修复来解决这种错误?

我正在制作的程序也有这种错误,但是我不知道我的程序到底在哪里出现此错误。当我按下自定义记录按钮时,它显示以下错误:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) TypeError: Start() missing 1 required positional argument: 'self' Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) TypeError: Start() missing 1 required positional argument: 'self'

我的程序没有1699行;它只有大约128行。这是我的整个程序,大约有128行向您显示它确实没有1699行,以及向您显示包含此令人困惑的错误的程序:

import tkinter as tk
from tkinter import PhotoImage
import time

class TimeCheck():
    def __init__(self, parent=None, **kw):        
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0              

    def trianglemove(move_x, move_y):
        canvas.move (triangle3, move_x, move_y)

    def _update(self):
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(50, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                  

    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            print ("RECORD")
            #for recordcounter in range(768):
            trianglemove(1, 0)

    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0

    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)    

root = tk.Tk()
root.geometry("960x600")

timechecker = TimeCheck(root)

recordbutton = PhotoImage(file="images/recordbutton.gif")
beatbutton = PhotoImage(file="images/beatbutton.gif")
stopbutton = PhotoImage(file="images/stopbutton.gif")

label_toptitle = tk.Label(root, text="Program Name", font=(None, 40),)
label_toptitle.grid(row=0, columnspan=3)

description = "To create rhythm, press the red record button. While recording, use the clicked note button to\n create a series of rectangle notes on screen. They can be held to extend the rectangles. \n\n Once you are done, press the red stop button to stop recording"

pixel = PhotoImage(width=1, height=1)
label_desc = tk.Label(root, image=pixel, compound="center", width=900, font=(None, 14),
                                          padx=20, pady=10, text=description)

label_desc.grid(row=1, columnspan=3)

canvas = tk.Canvas(width=960, height=300, bg='white')
canvas.grid(row=2, column=0, columnspan=3)

for linecounter in range(49):
        newtextbit = linecounter + 1
        if (newtextbit + 3) % 4 == 0 and newtextbit != 49:
                #print ('x is divisible by 3')
                canvas.create_text((linecounter * 16 + 80), 90,
                                           fill="darkblue",
                                           font="Times 10 bold",
                                           text=newtextbit)
        if (newtextbit + 3) % 4 == 0:
                canvas.create_line(((linecounter * 16 + 80)), 40, ((linecounter * 16 + 80)), 70,
                                           width=1,
                                           fill="black"
                                           )
        else:
                canvas.create_line(((linecounter * 16 + 80)), 50, ((linecounter * 16 + 80)), 70,
                                           width=1,
                                           fill="black"
                                           )
canvas.create_line(73, 70, 860, 70,
                                   width=2,
                                   fill="black"
                                   )
triangle3 = canvas.create_polygon(75, 25, 86, 25, 80, 40, fill ='red')

f1 = tk.Frame(root, width=70, height=30)
f1.grid(row=3, column=0, sticky='W')

button_record = tk.Button(f1,
                                                text="Record",
                                                image=recordbutton,
                                                command=TimeCheck.Start,
                                                compound="top"
                                                )
button_beat = tk.Button(f1,
                                                text="Beat",
                                                image=beatbutton,
                                                #command=tomato,
                                                compound="top"
                                                )
button_playstop = tk.Button(f1,
                                                text="Stop",
                                                image=stopbutton,
                                                #command=potato,
                                                compound="top"
                                                )

button_record.pack(side='left', padx=140)
button_beat.pack(side='left', padx=55)
button_playstop.pack(side='left', padx=140)

root.mainloop()

我的程序的目的是在按“记录”按钮时使红针以特定的速率向右移动。但是,该程序的特定区域一直使我在此页面顶部附近报告该错误。

1 个答案:

答案 0 :(得分:1)

它告诉您1699行发生错误的原因是因为Tkinter尝试调用command=TimeCheck.Start参数中指定的函数时发生了错误。因此,该错误发生在Tkinter的模块代码内部,但是由您传入参数的方式引起的。

您需要先创建TimeCheck类的实例,然后再调用其中的非静态方法。该消息告诉您未传递self函数所需的Start参数,这是在Python中在实例化对象上调用函数时在后台发生的。

您需要做的就是使您的按钮命令参数像这样:

button_record = tk.Button(f1,
                          text="Record",
                          image=recordbutton,
                          command=timechecker.Start,
                          compound="top")

timechecker是已经实例化的TimeCheck对象,用于从中调用Start函数。