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

时间:2019-05-23 02:45:37

标签: python tkinter

我写了一个计数器,该计数器不能被其他功能识别。我希望此计数器可用于其他任何功能。

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'

错误是:

>>> def my_func(s):
...     if s['A']:
...         s['B'] = s['B'] ** 2
...     return s
... 
>>> thing = pd.DataFrame({'A': [True, False, True], 'B': [1, 2, 3]})
>>> thing.apply(my_func, axis=1)
       A   B
0   True   1
1  False   2
2   True  81

我希望按钮的输出为“打印事件(我的答案)”,但它只是一起拒绝所有内容

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

我不确定该放在哪里

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

相应地调整打印事件。