我不明白“TypeError: Start() missing 1 required positional argument: 'self'”是什么意思。我需要澄清此错误消息指的是什么以及修复它需要什么。
同时 this other Stack Overflow关于“需要实例化 Application 对象并在该对象上调用 start,而不是在类上调用”的问题答案,我完全不知道这个解释意味着使用尽可能简单的语言。修复此类错误具体需要什么样的代码修复?
我正在做的程序也有这种错误,但我不知道我的程序到底在哪里出现这个错误;当我按下我定制的录制按钮时,它显示以下错误:
Tkinter 回调异常
追溯(最近一次通话):
文件“C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py”,第 1699 行,在 __call__ 中
返回 self.func(*args)
TypeError: Start() 缺少 1 个必需的位置参数:'self'
Tkinter 回调异常
追溯(最近一次通话):
文件“C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py”,第 1699 行,在 __call__ 中
返回 self.func(*args)
类型错误:Start() 缺少 1 个必需的位置参数:'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()
我的程序的目的是让红色指针在按下录制按钮时以特定速率向右移动。但是,该程序的特定区域不断给我报告此页顶部附近的错误。
最佳答案
它告诉您错误发生在第 1699 行的原因是因为当 Tkinter 尝试调用您在 command=TimeCheck.Start
参数中指定的函数时发生错误。所以错误发生在 Tkinter 的模块代码内部,但是由您传入参数的方式引起的。
在调用其中的非静态方法之前,您需要创建 TimeCheck
类的实例。该消息告诉您 Start
函数所需的 self
参数未传入,当您在实例化对象上调用函数时,这会在 Python 的幕后发生。
您需要做的就是让您的按钮命令参数如下所示:
button_record = tk.Button(f1,
text="Record",
image=recordbutton,
command=timechecker.Start,
compound="top")
其中 timechecker
是已实例化的 TimeCheck 对象,可从中调用 Start
函数。
关于python - "TypeError: Start() missing 1 required positional argument: ' self '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546212/