如何多次更新 python 中的 windowText?
how to update windowText in python multiple times?
我一直在寻找代码来帮助解决这个问题。
这是我能在下面找到的最佳答案。
import win32api
import win32con
import win32gui
import time
import threading
#Code example modified from:
#Christophe Keller
#Hello World in Python using Win32
# New code: Define global
windowText = 'Hello send by Python via Win32!'
def main():
#get instance handle
hInstance = win32api.GetModuleHandle()
# the class name
className = 'SimpleWin32'
# create and initialize window class
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(0, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
# register window class
wndClassAtom = None
try:
wndClassAtom = win32gui.RegisterClass(wndClass)
except Exception as e:
print (e)
raise e
hWindow = win32gui.CreateWindow(
wndClassAtom, #it seems message dispatching only works with the atom, not the class name
'Python Win32 Window',
win32con.WS_OVERLAPPEDWINDOW,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0,
0,
hInstance,
None)
# Show & update the window
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
# New code: Create and start the thread
thr = threading.Thread(target=customDraw, args=(hWindow,))
thr.setDaemon(False)
thr.start()
# Dispatch messages
win32gui.PumpMessages()
# New code: Attempt to change the text 1 second later
def customDraw(hWindow):
global windowText
time.sleep(1.0)
windowText = 'Something new'
win32gui.RedrawWindow(hWindow, None, None, win32con.RDW_INVALIDATE | win32con.RDW_ERASE)
def wndProc(hWnd, message, wParam, lParam):
if message == win32con.WM_PAINT:
hDC, paintStruct = win32gui.BeginPaint(hWnd)
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(
hDC,
windowText,
-1,
rect,
win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)
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)
if __name__ == '__main__':
main()
不幸的是输出是"Hello send by Python via Win32!"然后是"something new"
这是一个好的开始,但我需要 "something new" 更新到 "something new 2" 和 "something new 3" 等
我是否可以从包含 "something new" "something new 2" "something new 3" 等的文本文件中显示 "something new 2" 和 "something new 3" 等?
更新 "windowText = something new" 如果我不得不一遍又一遍地输入,那么更新会很长,所以也许最好逐行显示文本文件。
所以我的问题是
如何一次一行显示文本文件中的文本 "something new"
或者如何使用当前代码轻松显示 "something new 2" "something new 3" 等?
使用 SetTimer
而不是创建额外的线程。示例:
import win32api, win32con, win32gui
import timer
windowText = 'Hello send by Python via Win32!'
hWindow = 0
def main():
hInstance = win32api.GetModuleHandle()
className = 'SimpleWin32'
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(0, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
wndClassAtom = win32gui.RegisterClass(wndClass)
hWindow = win32gui.CreateWindow(wndClassAtom,
'Python Win32 Window',
win32con.WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, 0, 0, hInstance, None)
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
timer.set_timer(1000, customDraw)
win32gui.PumpMessages()
counter = 0
def customDraw(timer_id, time):
global hWindow
global counter
global windowText
if counter > 2:
counter = 0
text = ["test 0", "test 1", "test 2"]
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)
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(hDC, windowText, -1, rect,
win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)
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)
if __name__ == '__main__':
main()
我一直在寻找代码来帮助解决这个问题。 这是我能在下面找到的最佳答案。
import win32api
import win32con
import win32gui
import time
import threading
#Code example modified from:
#Christophe Keller
#Hello World in Python using Win32
# New code: Define global
windowText = 'Hello send by Python via Win32!'
def main():
#get instance handle
hInstance = win32api.GetModuleHandle()
# the class name
className = 'SimpleWin32'
# create and initialize window class
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(0, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
# register window class
wndClassAtom = None
try:
wndClassAtom = win32gui.RegisterClass(wndClass)
except Exception as e:
print (e)
raise e
hWindow = win32gui.CreateWindow(
wndClassAtom, #it seems message dispatching only works with the atom, not the class name
'Python Win32 Window',
win32con.WS_OVERLAPPEDWINDOW,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0,
0,
hInstance,
None)
# Show & update the window
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
# New code: Create and start the thread
thr = threading.Thread(target=customDraw, args=(hWindow,))
thr.setDaemon(False)
thr.start()
# Dispatch messages
win32gui.PumpMessages()
# New code: Attempt to change the text 1 second later
def customDraw(hWindow):
global windowText
time.sleep(1.0)
windowText = 'Something new'
win32gui.RedrawWindow(hWindow, None, None, win32con.RDW_INVALIDATE | win32con.RDW_ERASE)
def wndProc(hWnd, message, wParam, lParam):
if message == win32con.WM_PAINT:
hDC, paintStruct = win32gui.BeginPaint(hWnd)
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(
hDC,
windowText,
-1,
rect,
win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)
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)
if __name__ == '__main__':
main()
不幸的是输出是"Hello send by Python via Win32!"然后是"something new"
这是一个好的开始,但我需要 "something new" 更新到 "something new 2" 和 "something new 3" 等
我是否可以从包含 "something new" "something new 2" "something new 3" 等的文本文件中显示 "something new 2" 和 "something new 3" 等?
更新 "windowText = something new" 如果我不得不一遍又一遍地输入,那么更新会很长,所以也许最好逐行显示文本文件。
所以我的问题是 如何一次一行显示文本文件中的文本 "something new"
或者如何使用当前代码轻松显示 "something new 2" "something new 3" 等?
使用 SetTimer
而不是创建额外的线程。示例:
import win32api, win32con, win32gui
import timer
windowText = 'Hello send by Python via Win32!'
hWindow = 0
def main():
hInstance = win32api.GetModuleHandle()
className = 'SimpleWin32'
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(0, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
wndClassAtom = win32gui.RegisterClass(wndClass)
hWindow = win32gui.CreateWindow(wndClassAtom,
'Python Win32 Window',
win32con.WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, 0, 0, hInstance, None)
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
timer.set_timer(1000, customDraw)
win32gui.PumpMessages()
counter = 0
def customDraw(timer_id, time):
global hWindow
global counter
global windowText
if counter > 2:
counter = 0
text = ["test 0", "test 1", "test 2"]
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)
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(hDC, windowText, -1, rect,
win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)
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)
if __name__ == '__main__':
main()