我一直在寻找帮助解决此问题的代码。 这是我在下面可以找到的最佳答案。
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由Python通过Win32发送!”然后“新内容”
这是一个好的开始,但我需要将“新内容”更新为“新内容2”和“新内容3”等
无论如何,我是否可以从包含“新事物”,“新事物2”,“新事物3”等的文本文件中显示“新事物2”和“新事物3”等?
如果我必须一遍又一遍地输入更新内容,那么更新“ windowText =某些新内容”将非常漫长,因此也许最好逐行显示一行文本文件。
所以我的问题是 如何一次将一行文本文件中的文本显示为“新内容”
或者如何使用当前代码轻松显示“新2”,“新3”等?
答案 0 :(得分:1)
使用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()