Python &引用;TypeError:Start()缺少1个必需的位置参数:';自我'&引用;
我不明白“TypeError:Start()缺少1个必需的位置参数:'self'”是什么意思。我需要澄清此错误消息所指的内容以及修复它所需的内容 虽然问题的答案是“需要实例化应用程序对象并在该对象上调用start,而不是在类上”,但我完全不知道这个解释意味着使用尽可能最简单的语言。要修复此类错误,具体需要哪种代码修复 我正在做的程序也有这种错误,但我不知道我的程序中到底在哪里出错;当我按下自定义录制按钮时,它显示以下错误:Python &引用;TypeError:Start()缺少1个必需的位置参数:';自我'&引用;,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我不明白“TypeError:Start()缺少1个必需的位置参数:'self'”是什么意思。我需要澄清此错误消息所指的内容以及修复它所需的内容 虽然问题的答案是“需要实例化应用程序对象并在该对象上调用start,而不是在类上”,但我完全不知道这个解释意味着使用尽可能最简单的语言。要修复此类错误,具体需要哪种代码修复 我正在做的程序也有这种错误,但我不知道我的程序中到底在哪里出错;当我按下自定义录制按钮时,它显示以下错误: import tkinter as tk from tkinter im
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()
Tkinter回调中出现异常
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\Python36-32\lib\tkinter\\ uuuuu init\uuuuuu.py”,第1699行,在调用中__
返回self.func(*args)
TypeError:Start()缺少1个必需的位置参数:“self”
Tkinter回调中的异常
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\Python36-32\lib\tkinter\\ uuuuu init\uuuuuu.py”,第1699行,在调用中__
返回self.func(*args)
TypeError: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
函数。错误是因为在按钮命令TimeCheck.Start
中调用函数时未实例化TimeCheck
对象。它希望传入self
,当您创建该对象的实例时,类方法会发生这种情况。您对此有何建议?您必须将TimeCheck
类的初始化对象传入命令参数。我在回答中给出了一个例子。如果删除所有与问题无关的代码,您的问题会更好。例如,您可以删除三个按钮中的两个、所有图像和其他一些代码位。请参阅。很难知道代码的哪一部分导致了此类问题。如果我知道是哪部分代码导致了这个问题,我就会找出那些不相关的代码。