Python AttributeError 实例没有属性
Python AttributeError instance has no attribute
我收到以下错误:-
AttributeError: PageOne instance has no attribute 'scann'
我正在尝试 运行 一个 bash 脚本(运行 重点)。
仍然无法弄清楚为什么我会收到此错误。
我的代码如下:-
class PageOne(tk.Frame):
def __init__(self, parent, controller):
running = False # Global flag
tk.Frame.__init__(self, parent)
self.controller = controller
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
strt = tk.Button(self, text="Start Scan", command=self.start)
stp = tk.Button(self, text="Stop", command=self.stop)
button.pack()
strt.pack()
stp.pack()
self.after(1000, self.scann) # After 1 second, call scanning
def scann(self):
if running:
sub.call(['./runfocus'], shell=True)
self.after(1000, self.scann)
def start(self):
"""Enable scanning by setting the global flag to True."""
global running
running = True
def stop(self):
"""Stop scanning by setting the global flag to False."""
global running
running = False
请提出您宝贵的建议。
我无法重现 AttributeError: PageOne instance has no attribute 'scann' error
,但由于 running
标志,您的脚本还有其他问题。您应该避免使用可修改的全局变量,并且当您已经有了 class 时,绝对没有必要使用单独的全局变量。只需创建一个属性作为标志。
这是您的代码的可运行修复版本。我用一个简单的 print
调用替换了 sub.call(['./runfocus'], shell=True)
调用,这样我们就可以看到 start
和 stop
行为正确。
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, controller):
self.running = False
tk.Frame.__init__(self, parent)
self.controller = controller
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
strt = tk.Button(self, text="Start Scan", command=self.start)
stp = tk.Button(self, text="Stop", command=self.stop)
button.pack()
strt.pack()
stp.pack()
self.after(1000, self.scann) # After 1 second, call scanning
def scann(self):
if self.running:
#sub.call(['./runfocus'], shell=True)
print("calling runfocus")
self.after(1000, self.scann)
def start(self):
"""Enable scanning by setting the flag to True."""
self.running = True
def stop(self):
"""Stop scanning by setting the flag to False."""
self.running = False
root = tk.Tk()
frame = PageOne(root, root)
frame.pack()
root.mainloop()
我收到以下错误:-
AttributeError: PageOne instance has no attribute 'scann'
我正在尝试 运行 一个 bash 脚本(运行 重点)。 仍然无法弄清楚为什么我会收到此错误。 我的代码如下:-
class PageOne(tk.Frame):
def __init__(self, parent, controller):
running = False # Global flag
tk.Frame.__init__(self, parent)
self.controller = controller
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
strt = tk.Button(self, text="Start Scan", command=self.start)
stp = tk.Button(self, text="Stop", command=self.stop)
button.pack()
strt.pack()
stp.pack()
self.after(1000, self.scann) # After 1 second, call scanning
def scann(self):
if running:
sub.call(['./runfocus'], shell=True)
self.after(1000, self.scann)
def start(self):
"""Enable scanning by setting the global flag to True."""
global running
running = True
def stop(self):
"""Stop scanning by setting the global flag to False."""
global running
running = False
请提出您宝贵的建议。
我无法重现 AttributeError: PageOne instance has no attribute 'scann' error
,但由于 running
标志,您的脚本还有其他问题。您应该避免使用可修改的全局变量,并且当您已经有了 class 时,绝对没有必要使用单独的全局变量。只需创建一个属性作为标志。
这是您的代码的可运行修复版本。我用一个简单的 print
调用替换了 sub.call(['./runfocus'], shell=True)
调用,这样我们就可以看到 start
和 stop
行为正确。
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, controller):
self.running = False
tk.Frame.__init__(self, parent)
self.controller = controller
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
strt = tk.Button(self, text="Start Scan", command=self.start)
stp = tk.Button(self, text="Stop", command=self.stop)
button.pack()
strt.pack()
stp.pack()
self.after(1000, self.scann) # After 1 second, call scanning
def scann(self):
if self.running:
#sub.call(['./runfocus'], shell=True)
print("calling runfocus")
self.after(1000, self.scann)
def start(self):
"""Enable scanning by setting the flag to True."""
self.running = True
def stop(self):
"""Stop scanning by setting the flag to False."""
self.running = False
root = tk.Tk()
frame = PageOne(root, root)
frame.pack()
root.mainloop()