Attributeerror:'int'对象没有属性'insert'

时间:2015-04-28 02:05:43

标签: python tkinter stopwatch

基本上当我在Windows上运行它没有错误但是我在linux中运行它时,我显示了Attributeerror:'int'对象没有属性'insert'。有什么问题?

插入可以在Lap方法中找到,self.m.insert(END,self.laps [-1])。

from tkinter import *
import time

class StopWatch(Frame):  
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()
        self.e = 0
        self.m = 0
        self.makeWidgets()
        self.laps = []
        self.lapmod2 = 0
        self.today = time.strftime("%d %b %Y %H-%M-%S", time.localtime())

    def makeWidgets(self):                         
        """ Make the time label. """
        l1 = Label(self, text='----File Name----')
        l1.pack(fill=X, expand=NO, pady=1, padx=2)

        self.e = Entry(self)
        self.e.pack(pady=2, padx=2)

        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=3, padx=2)

        l2 = Label(self, text='----Laps----')
        l2.pack(fill=X, expand=NO, pady=4, padx=2)

        scrollbar = Scrollbar(self, orient=VERTICAL)
        self.m = Listbox(self,selectmode=EXTENDED, height = 10,font=("Helvetica", 12),yscrollcommand=scrollbar.set)
        self.m.pack(side=LEFT, fill=BOTH, expand=1, pady=5, padx=2)
        scrollbar.config(command=self.m.yview)
        scrollbar.pack(side=RIGHT, fill=Y)

    def _update(self): 
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(50, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))

    def _setLapTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)            
        return '%02d:%02d:%02d' % (minutes, seconds, hseconds)

    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1        

    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0

    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0
        self.laps = []   
        self._setTime(self._elapsedtime)

    def Lap(self):
        '''Makes a lap, only if started'''
        tempo = self._elapsedtime - self.lapmod2
        if self._running:
            self.laps.append(self._setLapTime(tempo))
            self.m.insert(END, self.laps[-1])

            self.m.yview_moveto(1)
            self.lapmod2 = self._elapsedtime

    def GravaCSV(self):
        '''Pega nome do cronometro e cria arquivo para guardar as laps'''
        arquivo = str(self.e.get()) + ' - '
        with open(arquivo + self.today + '.txt', 'wb') as lapfile:
            for Lap in self.laps:
                lapfile.write((bytes(str(Lap) + '\n', 'utf-8')))

def main():
    root = Tk()
    root.wm_attributes("-topmost", 1)     
    sw = StopWatch(root)
    sw.pack(side=TOP)

    Button(root, text='Lap', command=sw.Lap).pack(side=LEFT)
    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Save', command=sw.GravaCSV).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)    

    root.mainloop()

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:2)

看起来解释器在调用Lap()之前以某种方式调用makeWidgets()方法,这很奇怪,因为我没有在提供的代码中看到Lap()被调用。这是MCVE吗?

不是在self.m中将0初始化为__init__(),而是使用它应具有的值初始化它:Listbox小部件。

  1. self.m = 0
  2. 移除__init__()
  3. self.m = Listbox(self,selectmode=EXTENDED, height = 10,font=("Helvetica", 12),yscrollcommand=scrollbar.set)makeWidgets()移至__init__()
  4. makeWidgets()只会被调用一次,所以也许您可以将所有内容都放入__init__(),除非您计划扩展程序以多次调用makeWidgets()

答案 1 :(得分:0)

if (!empty($_POST['username'])) { $link->query("UPDATE firstsection SET username = '".$link->real_escape_string($_POST['username'])."' WHERE id = 2"); } if (!empty($_POST['description'])) { $link->query("UPDATE firstsection SET description = '".$link->real_escape_string($_POST['description'])."' WHERE id = 2"); } if (!empty($_POST['userdescription'])) { $link->query("UPDATE firstsection SET userdescription = '".$link->real_escape_string($_POST['userdescription'])."' WHERE id = 2"); } if (!empty($_POST['heading'])) { $link->query("UPDATE firstsection SET heading = '".$link->real_escape_string($_POST['heading'])."' WHERE id = 2"); } if (!empty($_POST['subheading'])) { $link->query("UPDATE firstsection SET subheading = '".$link->real_escape_string($_POST['subheading'])."' WHERE id = 2"); } if (!empty($_POST['subheading2'])) { $link->query("UPDATE firstsection SET subheading2 = '".$link->real_escape_string($_POST['subheading2'])."' WHERE id = 2"); } 构造函数中,您声明并设置sum(df$Sex == 'Female') StopWatch。我在代码中没有看到您将self.m = 0更改为任何具有int方法且self.m类型没有insert方法的对象叫做。当您尝试使用该方法时,您将获得AttributeError。