未绑定方法 x() 必须以 y 实例作为第一个参数调用(取而代之的是 int 实例)
unbound method x() must be called with y instance as first argument (got int instance instead)
我的 Python- 程序有很多代码,所以我希望你没事,我给你我的问题所在的部分代码。我为 Tkinter 创建了一个线程,我正在尝试访问该线程中的一个函数。这是它的样子:
class GUI (threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
def run(self):
window = Tk()
window.title('GUI')
window = Canvas(window, width=400, height=200)
window.pack()
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
GUI.output(21,0)
这是我收到的消息:
TypeError: unbound method output() must be called with GUI instance as first argument (got int instance instead)
老实说,我不知道我必须引用第一个参数的实例是什么。
Instance 是 python 函数需要的对象实例,在您的例子中 'self'
阅读 dive into python 中的精彩解释。 python中的class方法中为什么要使用self,你需要理解。对于您的问题,请查看此代码。
class GUI (threading.Thread):
window=object
def __init__(self, num):
threading.Thread.__init__(self)
def run(self):
self.window = Tk()
self.window.title('GUI')
self.window = Canvas(self.window, width=400, height=200)
self.window.pack()
@staticmethod
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
GUI.output(21,0)
OP 的其他实现
class Gui():
def __init__(self):
window = Tk()
window.title('GUI')
self.window = Canvas(window, width=400, height=200)
self.window.pack()
def output(self,lampe, status):
if status == 0:
if lampe == 21:
self.window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
self.window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
并实施这个
gui=Gui()
thread=threading.Thread(target=gui.output, args=(21,0))
thread.start()
您正在尝试将其作为静态方法访问,因此您需要使用 @staticmethod
对其进行注释
@staticmethod
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
我的 Python- 程序有很多代码,所以我希望你没事,我给你我的问题所在的部分代码。我为 Tkinter 创建了一个线程,我正在尝试访问该线程中的一个函数。这是它的样子:
class GUI (threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
def run(self):
window = Tk()
window.title('GUI')
window = Canvas(window, width=400, height=200)
window.pack()
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
GUI.output(21,0)
这是我收到的消息:
TypeError: unbound method output() must be called with GUI instance as first argument (got int instance instead)
老实说,我不知道我必须引用第一个参数的实例是什么。
Instance 是 python 函数需要的对象实例,在您的例子中 'self' 阅读 dive into python 中的精彩解释。 python中的class方法中为什么要使用self,你需要理解。对于您的问题,请查看此代码。
class GUI (threading.Thread):
window=object
def __init__(self, num):
threading.Thread.__init__(self)
def run(self):
self.window = Tk()
self.window.title('GUI')
self.window = Canvas(self.window, width=400, height=200)
self.window.pack()
@staticmethod
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
GUI.output(21,0)
OP 的其他实现
class Gui():
def __init__(self):
window = Tk()
window.title('GUI')
self.window = Canvas(window, width=400, height=200)
self.window.pack()
def output(self,lampe, status):
if status == 0:
if lampe == 21:
self.window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
self.window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
并实施这个
gui=Gui()
thread=threading.Thread(target=gui.output, args=(21,0))
thread.start()
您正在尝试将其作为静态方法访问,因此您需要使用 @staticmethod
@staticmethod
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")