Приложение не имеет атрибута «счетчик», две функции пытаются работать вместе - PullRequest
0 голосов
/ 23 мая 2019

Я написал счетчик, который не распознается другими функциями.Я хочу, чтобы этот счетчик работал в любой другой функции.

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)", ноэто просто отвергает все это вместе

Ответы [ 2 ]

0 голосов
/ 23 мая 2019

Как объяснил @Paul Руни в комментарии, плохая строка - 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
0 голосов
/ 23 мая 2019

Я не был уверен, где это поставить

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

Отрегулируйте события печати соответственно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...