首页 > 解决方案 > 应用程序没有属性“计数器”,两个功能试图协同工作

问题描述

我写了一个计数器,它的工作原理是其他功能无法识别的。我希望这个计数器可以在任何其他功能中工作。

def buttoncounter(self):
     self.showthis =  str([self.videofirstbutton.get(),
                    self.videosecondbutton.get(),
                    self.videothirdbutton.get(),
                    self.videofourthbutton.get(),
                    self.videofifthbutton.get(),
                    self.videosixthbutton.get(),
                    self.homevideofirstbutton.get(),
                    self.homevideosecondbutton.get(),
                    self.homevideothirdbutton.get(),
                    self.homevideofourthbutton.get(),
                    self.homevideofifthbutton.get(),
                    self.homevideosixthbutton.get(), 
                    self.castingfirstbutton.get(),
                    self.castingsecondbutton.get(),
                    self.castingthirdbutton.get(),
                    self.castingfourthbutton.get(),
                    self.castingfifthbutton.get(),
                    self.castingsixthbutton.get()])
     self.counter =  print(self.showthis.count('1'))

def printbutton(self):   
        self.printbutton = Button(self.frame, text="Print Events " + str(self.counter), width=15, command=self.buttoncounter) #str(self.counter)
        self.printbutton.pack( side = LEFT )

错误是:

line 211, in printbutton
    self.printbutton = Button(self.frame, text="Print Events " + str(self.counter), width=15, command=self.buttoncounter) #str(self.counter)
AttributeError: 'App' object has no attribute 'counter'

我希望按钮的输出是“打印事件(myanswer)”,但它只是一起拒绝

标签: pythontkinter

解决方案


正如@Paul Rooney 在评论中解释的那样,坏线是self.counter = print(self.showthis.count('1')). 该print函数将字符串作为输入,并将它们写入stdout。顺便说一句,在 Python 中,所有不返回任何内容的函数实际上都返回None. 因此,如果您决定存储不返回任何内容的函数的输出,您将得到None

>>> result = print('I\'m headed for your screen!')
I'm headed for your screen!
>>> result is None
True

推荐阅读