应用没有属性"计数器",两个函数尝试协同工作



我编写了一个计数器,该计数器无法通过其他功能识别。我希望此计数器能够在任何其他功能中工作。

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(",但它只是将其全部拒绝

正如@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

我不确定要放在哪里

from tkinter import *
the_window = Tk()
the_window.title('Entertainment Guide')
the_window.resizable(width=False, height=False)
canvas = Canvas(the_window, width = 500, height = 100)
canvas.pack()
class App:
    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.counter =  print(self.showthis.count('1'))
    def __init__(self, master):
        self.frame = Frame(the_window)
        self.frame.pack()
        #the_window.geometry("500x120")
        canvas.configure(background='paleturquoise')
        self.videogames = Button(self.frame, text = 'Video Games', fg ='violet', width=15, command=self.create_videogameswindow) 
        self.videogames.pack( side = LEFT ) 
    def printbutton(self):   
        self.printbutton = Button(self.frame, text="Print Events ", width=15, command=self.buttoncounter) #str(self.counter)
        self.printbutton.pack( side = LEFT )
    def create_videogameswindow(self):
        self.a = videogameswindow = Toplevel(the_window)
        self.b = videogameswindow.title('Video Games')
        self.c = videogameswindow.resizable(width=False, height=False)
        self.heading = Label(videogameswindow, text="Video Game Selector", bg="violet", font=("Helvetica", 16))
        self.heading.pack()
        self.videofirstbutton = IntVar()
        self.videosecondbutton = IntVar()
        self.videothirdbutton = IntVar()
        self.videofourthbutton = IntVar()
        self.videofifthbutton = IntVar()
        self.videosixthbutton = IntVar()
        self.V1 = Checkbutton(videogameswindow, text = "Video", variable =self.videofirstbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.V2 = Checkbutton(videogameswindow, text = "Video", variable =self.videosecondbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.V3 = Checkbutton(videogameswindow, text = "Video", variable =self.videothirdbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.V4 = Checkbutton(videogameswindow, text = "Video", variable =self.videofourthbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.V5 = Checkbutton(videogameswindow, text = "Video", variable =self.videofifthbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.V6 = Checkbutton(videogameswindow, text = "Video", variable =self.videosixthbutton, onvalue = 1, offvalue = 0, bg='violet').pack(anchor=W)
        self.d = videogameswindow.configure(background='violet')
        self.printbutton()
app = App(the_window)
the_window.mainloop()

相应地调整打印事件。

最新更新