0

我希望能够在 PythonWin 编辑器中保存我的会话状态(例如,这三个文件被打开并定位在 PythonWin 窗口中的这些特定位置)。我可以使用 win32gui 获取 PythonWin 中每个子窗口的句柄,以及每个文件的标题和窗口的位置/大小。我不清楚如何获取列为子窗口名称的文件的完整路径(即,如果子窗口名称是 test.py 并且 test.py 位于 c:\python\test.py,我不知道知道如何获取 c:\python)。我在想我会写出打开了哪些文件以及它们的窗口位置到一个小文件中,然后我会在 PythonWin 开始时间调用该文件进行加载。

关于如何获取子窗口名称的完整路径的任何想法?

或者,如果有人已经有了在 PythonWin 中保存会话状态的更优雅的解决方案,请传递它。

下面是我现在正在使用的代码(感谢 Michal Niklas 提供了使用win32gui的起始代码)。


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

于 2010-09-04T20:32:06.017 回答