0

I need to run a .py at startup. the code works however the only way I can get it to start is to use this method-

import os
import subprocess

DIR = os.path.join('C:\\', 'Users', '7', 'Desktop', 'windowlessecclesiasticusworking.py')

subprocess.call(['python', DIR])

is there a way I can write a batch file that runs at startp that can run this method to start the .py file right after start up?

for some reason the .py no longer starts unless it is opened by opening a python shell and using the method stated above to start the file 'windowlessecclesiasticusworking.py'

heres how the windowlessecclesiasticusworking.py file looks in code.

    import win32api, win32con, win32gui, win32ui, timer, threading

    windowText = 'Ecclesiasticus'
    hWindow = 0

    def main():
    hInstance = win32api.GetModuleHandle()
    className = 'MyWindowClassName'

    wndClass                = win32gui.WNDCLASS()
    wndClass.style          = win32con.CS_HREDRAW | win32con.CS_VREDRAW
    wndClass.lpfnWndProc    = wndProc
    wndClass.hInstance      = hInstance
    wndClass.hIcon          = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
    wndClass.hCursor        = win32gui.LoadCursor(None, win32con.IDC_ARROW)
    wndClass.hbrBackground  = win32gui.GetStockObject(win32con.WHITE_BRUSH)
    wndClass.lpszClassName  = className
    wndClassAtom = win32gui.RegisterClass(wndClass)

    exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT

    style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE

    hWindow = win32gui.CreateWindowEx(
        exStyle,
        wndClassAtom,
        None,
        style,
        0, # x
        0, # y
        win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width
        win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height
        None, # hWndParent
        None, # hMenu
        hInstance,
        None # lpParam
)
        win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255,           win32con.LWA_COLORKEY | win32con.LWA_ALPHA) ####### COLOR

      win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
        win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE |     win32con.SWP_SHOWWINDOW)
    thr = threading.Thread(target=customDraw, args=(hWindow,))
    thr.setDaemon(False)
    thr.start()

    win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
    win32gui.UpdateWindow(hWindow)
    timer.set_timer(10000, customDraw)
    win32gui.PumpMessages()

    counter = 0
    def customDraw(timer_id, time):
    global hWindow
    global counter
    global windowText
    if counter > 40:
            counter = 0
            text = ["1:1 All wisdom is from the Lord God, and hath been always with him, and is before all time.   ",
    "1:2 Who hath numbered the sand of the sea, and the drops of rain, and the days of the world? Who hath measured the height of heaven, and the breadth of the earth, and the depth of the abyss?   ",]
    windowText = text[counter]
    counter = counter + 1
    win32gui.InvalidateRect(hWindow, None, True)

    def wndProc(hWnd, message, wParam, lParam):
    if message == win32con.WM_PAINT:
        hdc, paintStruct = win32gui.BeginPaint(hWnd)
        dpiScale = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSX) / 60.0
        fontSize = 18
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "Comic Sans"
        lf.lfHeight = int(round(dpiScale * fontSize))
        hf = win32gui.CreateFontIndirect(lf)
        win32gui.SelectObject(hdc, hf)
        rect = win32gui.GetClientRect(hWnd)
        win32gui.DrawText(hdc, windowText, -1, rect,
          win32con.DT_LEFT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE
        )
        win32gui.EndPaint(hWnd, paintStruct)
        return 0

    elif message == win32con.WM_DESTROY:
        print('Being destroyed')
        win32gui.PostQuitMessage(0)
        return 0

    else:
        return win32gui.DefWindowProc(hWnd, message, wParam, lParam)
        calrect = win32gui.DrawText(hdc, text, -1, rect, textformat | win32con.DT_CALCRECT);

        rect.top = rect.bottom - calcrect.bottom;
        win32gui.DrawText(hDC, text, -1, rect, textformat)

    if __name__ == '__main__':
    main()

originally when I was working on just trying to get the windowlessecclesiasticusworking.py python file to open and run after months of not touching it I ran into some errors with the win32api.. I updated python and used the first method talked about to start the .py file from python shell. now it is working, however I need it to start at startup so perhaps there is a way to run the 'import os import subprocess' snippet of shell code in a batch file. the solution can be anything I just think batch file for windows would be the easiest to run at startup. but I am not the expert obviously so any and all suggestions would help greatly. thanks.

Steed
  • 61
  • 9

2 Answers2

3

If you only want it to start when your user account logs in, simply place a shortcut to the .py file here:

C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

If you want this to happen for all users on the machine, put it here:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
Tom Lubenow
  • 1,109
  • 7
  • 15
1

Add your top code snipped to a py file, let's call it 'pythontarter.py' -- Then make a .bat file that references it like this:

@echo off
python c:\temp\pythonstarter.py

If you want this to start with the user logon add your new batch file to this location replacing username with the specific user:

C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

If you want this to start with windows BEFORE user login then follow these instructions:

Run Batch File On Start-up

brazosFX
  • 342
  • 1
  • 11
  • 1
    I am getting a cmd window open that says this::: Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\7\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\Users\7\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) TypeError: customDraw() missing 1 required positional argument: 'time' the program launches at startup now (THANK YOU) however the .py only stays open while the cmd window displaying that error is open – Steed Aug 15 '19 at 23:41
  • So the .py is actually crashing, and you are just getting the error output. Did you confirm that your latest stuff works in the current location if you just run it? customDraw() missing 1 required positional argument: 'time' --> Does this happen if you just run the batch file from a command line? What if you just go to a command line where the python is located and type: python pythonstarter.py ? – brazosFX Aug 16 '19 at 00:59
  • 1
    I used: pyinstaller —onefile -w pythonfilename.py /////in powershell and it converted the .py to exe and now it runs without the cmd error window and boots up when the user logs on by putting the new .exe in the start up folder. It was a good work around but everything is working good. Thank you for your help. – Steed Aug 16 '19 at 19:26