如何在windows上启动时运行.py文件?

2024-04-25 01:56:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在启动时运行.py。 代码工作,但我唯一能让它开始的方法是使用这个方法-

import os
import subprocess

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

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

有没有一种方法可以编写一个在startp上运行的批处理文件,在启动后立即运行这个方法来启动.py文件?你知道吗

出于某种原因,.py不再启动,除非通过打开python shell并使用上述方法启动文件“windowlesse”来打开它cclesiasticusworking.py文件'

这是窗户是怎么开的cclesiasticusworking.py文件文件在代码中查找。你知道吗

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

最初我只是想弄个窗户cclesiasticusworking.py文件python文件打开并运行在几个月没有接触它之后,我在win32api中遇到了一些错误。。我更新了python,并使用前面提到的第一种方法从pythonshell启动.py文件。现在它正在工作,但是我需要它在启动时启动,所以也许有一种方法可以在批处理文件中运行shell代码的“import os import subprocess”片段。解决方案可以是任何我认为windows批处理文件在启动时最容易运行的东西。但我显然不是专家,所以任何建议都会大有帮助。谢谢。你知道吗


Tags: and文件ofthe方法pyrectimport
2条回答

如果只希望在用户帐户登录时启动,只需在此处放置.py文件的快捷方式:

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

如果您希望计算机上的所有用户都这样做,请将其放在此处:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

把你剪下来的代码添加到py文件中,我们称之为Python'然后创建一个引用它的.bat文件,如下所示:

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

如果要从用户登录开始,请将新批处理文件添加到此位置,并用特定用户替换用户名:

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

如果您希望在用户登录之前从windows开始,请按照以下说明操作:

Run Batch File On Start-up

相关问题 更多 >