问题相关代码,请勿粘贴截图
在class_instance.py模块中,我创建了print_time方法。并实例化对象app
rom tkinter import Button, Tk, HORIZONTALfrom tkinter.ttk import Progressbarimport timeclass MonApp(Tk): def __init__(self): super().__init__() bt1 = Button(self, text='Traitement', command=self.traitement) bt1.grid() self.progress = Progressbar(self, orient=HORIZONTAL, length=100, mode='indeterminate') self.progress.grid() self.progress.grid_forget() def traitement(self): self.progress.grid() self.a = self.progress.start() time.sleep(15) ## Just like you have many, many code lines... self.b = self.progress.stop() def print_time(self): print("下载开始时间{}" % self.a) print("下载结束时间{}" % self.b)if __name__ == '__main__': app = MonApp() app.mainloop()
在另外fun.py模块中,我想用类的print_time方法
import class_instancedef c(): print("调入类方法的结果") class_instance.app.print()if __name__ == '__main__': c()
运行结果及报错内容
但运行结果却告知module 'class_instance' has no attribute 'app'模块.
AttributeError: module 'class_instance' has no attribute 'app'
我的解答思路和尝试过的方法
1.我又不能在第二个模块中重新实化一个新对象,那样就想是重新启动了一个GUI;
2.我考虑过@staticmethod和@classmethod,对print_time函数静态方法,没有self,这样就不能读取到self.a,self.b。而动态方法传入cls,也是无法读取到self.a, self.b参数的。
我想要达到的结果
想要fun.py中,如何不重新实例化一个新对象的情况下,访问类方法print_time