我得到了以下错误:-
AttributeError: PageOne instance has no attribute 'scann'我正在尝试运行bash脚本(run聚焦)。还是找不出我为什么会犯这个错误。我的代码如下:
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请提供你的宝贵建议。
发布于 2018-05-25 11:23:57
我无法复制AttributeError: PageOne instance has no attribute 'scann' error,但是由于running标志,您的脚本还有其他问题。您应该避免使用可修改的全局值,并且当您已经拥有一个类时,绝对没有必要使用单独的全局。只需创建一个属性作为标志。
下面是一个可运行的修复版本的代码。我将sub.call(['./runfocus'], shell=True)调用替换为一个简单的print调用,这样我们就可以看到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()https://stackoverflow.com/questions/50527404
复制相似问题