0

我正在尝试获取“Dragon On”的值,该值应该以“On”开始。我想将其与“关闭”进行比较,但它无法识别更改。我第二次按下按钮时,它会将 textvariable 设置为 Off,但比较不会检测到它已更改。我也试过

if (self.e3.config('textvariable')[-1] == 'Off')

但它只是继续。

from Tkinter import *
import time
import datetime
import threading
import sys
import pdb; 

class App:

dragon = 300

def __init__(self, master):

    Label(master, text="Dragon Down").grid(row=2)
    self.e3 = Button(master,command=self.dragon_callback,width=15,text="Dragon On",textvariable="On")
    self.e4 = Entry(master)

    self.e3.grid(row=2, column=1)
    self.e4.grid(row=2, column=2)


def dragon_callback(self):
    print "%s" % self.e3.config('textvariable')[-1]
  #  print sys._current_frames()
    if self.e3.config('textvariable')[-1]:
        print "inside here the variable is '%s' " % self.e3.config('textvariable')[-1]
        try:
            self.t2_stop = threading.Event()
            t = threading.Thread(target=self.dragon_time,name="Dragon Thread",args=("eee",self.t2_stop))
            t.daemon = True
            t.start()
        except:
            print "Error: unable to start thread"
        self.e3.config(textvariable='Off')
    else:
        print " i stopped"
        self.t2_stop.set()
        self.e4.delete(0, END)
        self.e3.config(textvariable='True')



def dragon_time(self,bleh,stop_event):
    #print "did i get the value %s" % self.text_area.get()
    boom=300
    while(not stop_event.is_set() and boom > 0):
        #equivalent to time.sleep()
        self.e4.delete(0, END)
        self.e4.insert(0, boom)
        time.sleep(1)
        self.e4.delete(0, END)
        self.e4.insert(0, boom)
        boom -=1
        pass


root = Tk()
root.title("Timer")


app = App(root)

root.mainloop()
4

1 回答 1

2

要进行比较,您必须将对象转换为字符串

if (str(self.e3.config('textvariable')[-1]) == 'Off'):
于 2013-03-06T01:16:06.517 回答