Python Tkinter线程按钮命令和主循环

时间:2015-04-26 19:05:05

标签: python multithreading python-2.7 tkinter keylogger

我正在使用Tkinter创建一个键盘记录器(仅用于教育目的),我尝试实现线程,所以当我按下按钮时,键盘记录器会在运行root.mainloop()时运行。

代码是:

import keylogger
import time
from Tkinter import *
import threading

now = time.time()
done = lambda: time.time() < now + -1

def print_keys(modifiers, keys): print "%r   %r" % (keys, modifiers)  

def key():
    sys.stdout = open('keylogger.txt', 'w')
    keylogger.log(done, print_keys)

def keystop():
    sys.stdout.close()
    sys.exit()

def loop():
    root.mainloop()

def ft():
    pt = threading.Thread(target = key)
    ct = threading.Thread(target = loop)
    pt.start()
    ct.start()


root = Tk()
button1 = Button(root, text = "Start", command = ft)
button1.pack()

button2 = Button(root, text = "Stop", command = keystop)
button2.pack()

root.mainloop()

0 个答案:

没有答案