0

セッション状態を PythonWin エディタ内に保存できるようにしたいと考えています (たとえば、これら 3 つのファイルが開かれ、PythonWin ウィンドウ内の特定の場所に配置されます)。win32gui を使用して、PythonWin 内の各子ウィンドウへのハンドル、および各ファイルのタイトルとウィンドウの位置/サイズを取得できます。ただし、子ウィンドウ名としてリストされているファイルのフル パスを取得する方法は不明です (つまり、子ウィンドウ名が test.py で、test.py が c:\python\test.py にある場合、私は知りませんc:\python を取得する方法を知っています)。開かれたファイルとそのウィンドウの位置を小さなファイルに書き出し、それを PythonWin の開始時に呼び出してロードすることを考えていました。

子ウィンドウ名への完全なパスを取得する方法についてのアイデアはありますか?

または、誰かが PythonWin でセッション状態を保存するためのより洗練されたソリューションを既に持っている場合は、それを渡してください。

以下は、私が現在使用しているコードです ( win32guiを使用するためのスターター コードを提供してくれた Michal Niklas に感謝します)。


import win32gui
import re

MAIN_HWND = 0

def is_win_ok(hwnd, starttext):
    s = win32gui.GetWindowText(hwnd)
    if s.startswith(starttext):
        global MAIN_HWND
        MAIN_HWND = hwnd
        return None
    return 1


def find_main_window(starttxt):
    global MAIN_HWND
    win32gui.EnumChildWindows(0, is_win_ok, starttxt)
    return MAIN_HWND


def winPos(hwnd):
    if  type(hwnd) == type(1): ( left, top, right, bottom ) = win32gui.GetWindowRect(hwnd)
    return "%i, %i, %i, %i" % (left, right, top, bottom)


def winName(hwnd, children):
    s = win32gui.GetWindowText(hwnd)

    rePy = re.compile(r'[a-zA-Z1-9_ ]*.py')
    rePySearch = rePy.search(s)

    if rePySearch is not None:
        if rePySearch.group()[0:7] != "Running":
            s = s + ',' + winPos(hwnd) + '\n'
            children.append(s)
    return 1

def main():

    children = []
    main_app = 'PythonWin'
    hwnd = win32gui.FindWindow(None, main_app)

    if hwnd < 1:
        hwnd = find_main_window(main_app)

    if hwnd:
        win32gui.EnumChildWindows(hwnd, winName, children)

    filename = "sessionInfo.txt"
    sessionFile = os.path.join(sys.path[0],filename)

    fp=open(sessionFile, 'wb')
    for i in range(len(children)):
        fp.write(children[i])
    fp.close()

main()
4

1 に答える 1

0

私は間違っているかもしれませんが、PythonWin は Python で書かれていませんか?

ソースを「保存」コマンドに読み取って、フルパスがどこに保存されているかを調べてみましたか?

(私も調べてみましたが、Windows は 5 年間使用していません)

于 2010-09-04T20:32:06.017 に答える