【问题标题】:return the value of a a variable from a class to a function将变量的值从类返回给函数
【发布时间】:2020-12-23 13:04:22
【问题描述】:

我希望从 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()

当我运行此代码时,我得到pyvar 的值与timestr 的值

【问题讨论】:

  • and %s = %s ——不能使用 SQL 参数填写列名。在这种情况下,甚至不确定您要如何处理。
  • 您在lap_done 函数中创建了一个名为timestrStopWatch 对象,因此您可以通过timestr.timestr 访问timestr 变量

标签: python class stopwatch


【解决方案1】:

你的语法错误。 timestr = StopWatch() 创建一个 StopWatch 类的对象,它不会从那里提取值。

您应该做的是为 StopWatch 创建一个对象为obj = StopWatch(),然后以obj.timestr 访问timestr 属性

所以,您的recordTuple 将是: recordTuple = (lap2,race,obj.timestr,id)

【讨论】:

    猜你喜欢
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多