简体   繁体   English

将 aa 变量的值从类返回到函数

[英]return the value of a a variable from a class to a function

I am looking to take the value self.timestr from the class StopWatch and then take that value into the function lap_done我希望从类StopWatch获取值self.timestr ,然后将该值带入函数lap_done

class StopWatch(Frame):  
                                           
    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.makeWidgets()      

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Arial", 25, "bold"))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=2, padx=2)  
        

    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)
        return timestr
    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 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._setTime(self._elapsedtime)


def lap_done()

    timestr = StopWatch()
    query = ("""UPDATE places SET lapnum = %s and %s = %s WHERE userID = %s""")
    print("1")
    recordTuple = (lap2,race,timestr,id)
    print("2")
    cursor.execute(query,recordTuple)
    print("3")
    mydb.commit()

When I run this code I get the value of pyvar for the value of timestr当我运行这段代码时,我得到了pyvar的值作为timestr的值

You got your syntax wrong.你的语法错了。 timestr = StopWatch() creates an object of the class StopWatch, it doesn't extract the value from there. timestr = StopWatch()创建类 StopWatch 的对象,它不会从那里提取值。

What you should be doing instead is create an object for StopWatch as obj = StopWatch() , and then access the timestr property as obj.timestr你应该做的是为 StopWatch 创建一个对象obj = StopWatch() ,然后访问timestr属性作为obj.timestr

So, your recordTuple will be: recordTuple = (lap2,race,obj.timestr,id)所以,你的recordTuple将是: recordTuple = (lap2,race,obj.timestr,id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM