Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,不使用';睡不着_Python_Multithreading_Countdown - Fatal编程技术网

Python,不使用';睡不着

Python,不使用';睡不着,python,multithreading,countdown,Python,Multithreading,Countdown,我是python新手,我正在尝试在点击按钮时创建一个倒计时计时器。但是我希望这个倒计时计时器开始倒计时,并将当前的倒计时值放在文本区域中。此外,我需要应用程序的其余部分在倒计时运行时不睡眠。到目前为止,它将在控制台中输出倒计时,但将冻结应用程序的其余部分。有人能给我指出正确的方向吗 from Tkinter import * import time import threading import thread class App: def __init__(self, master

我是python新手,我正在尝试在点击按钮时创建一个倒计时计时器。但是我希望这个倒计时计时器开始倒计时,并将当前的倒计时值放在文本区域中。此外,我需要应用程序的其余部分在倒计时运行时不睡眠。到目前为止,它将在控制台中输出倒计时,但将冻结应用程序的其余部分。有人能给我指出正确的方向吗

from Tkinter import *
import time
import threading
import thread

class App:


    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.getvalue = Button(frame, text="Get the Text Area", command=self.thevalue)
        self.getvalue.pack(side=LEFT)

        self.text_area = Entry()
        self.text_area.pack(side=RIGHT)


    def thevalue(self):
        print "In the value"
        try:
            t = threading.Thread(target=self.print_time("I am in print_time"))
            t.daemon = True
            t.start()
        except:
            print "Error: unable to start thread"

    def print_time(self,bleh):
        print bleh
        print "The text area value is %s" % self.text_area.get()
        boom=5
        while boom >0:
            time.sleep(1)
            self.text_area.delete(0, END)
            self.text_area.insert(0, boom)
            print(boom)
            boom -=1

root = Tk()

app = App(root)

root.mainloop()
这不会做你想让它做的事。这里发生的是调用函数
self.print\u time
,然后将其返回值传递给
threading.Thread
的构造函数

您需要像这样创建线程:

t = threading.Thread(target=self.print_time, args=("I am in print_time",))

哦,好的,太好了,谢谢你!看起来我还有更多的阅读要做:请记住
something()
将立即调用函数,而
something
只是对它的引用:)
t = threading.Thread(target=self.print_time, args=("I am in print_time",))