应用程序没有“counter”属性,两个函数试图协同工作

2024-09-30 10:43:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个计数器,它的工作原理是不被其他函数识别。我想让这个计数器在任何其他功能中工作

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'

我希望按钮的输出是“Print Events(myanswer)”,但它只是一起拒绝了它


Tags: textselfgetdefcounter计数器buttonevents
2条回答

我不知道该把这个放哪儿

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()

相应地调整打印事件

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

相关问题 更多 >

    热门问题