鱼C论坛

 找回密码
 立即注册
查看: 2379|回复: 5

使用pywin32在tkinter上画图的问题

[复制链接]
发表于 2019-9-10 11:52:48 | 显示全部楼层 |阅读模式
50鱼币
本帖最后由 傻纸 于 2019-9-10 11:59 编辑

获得了tkinter的窗口句柄之后,貌似还要捕获tkinter窗口的创建事件(消息)和重绘事件(消息)
因为我直接在tkinter上写绘图的代码是无效的,所以不得不创建一个按钮,通过按钮的回调事件,把图形画出来
  1. from win32gui import *
  2. import win32con
  3. import pywintypes

  4. from tkinter import *

  5. root=Tk()

  6. root.title("tkinterApp")

  7. hwnd = pywintypes.HANDLE(int(root.frame(), 16))

  8. print(hwnd)

  9. hdc=GetDC(hwnd)

  10. def callback():
  11.     hbrush=GetStockObject(win32con.NULL_BRUSH)
  12.     oldbrush=SelectObject(hdc,hbrush)
  13.     Rectangle(hdc,50,50,100,100)

  14. Button(root,text="显示画图",command=callback).pack()
  15. #
  16. #ReleaseDC(hwnd,hdc)

  17. root.geometry("400x400")

  18. #root.mainloop()
复制代码

ff.jpg
这个程序通过点击按钮,画出图形,但是我改变窗口大小的时候,图形就消失了
(我想实现的效果是,tkinter窗口加载完就显示图形,而不需要使用控件的事件回调,窗口刷新的时候,图形还能保持不变)
有熟悉pywin32和tkinter的小伙伴吗,能不能告诉我解决的办法....

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-10 13:17:56 | 显示全部楼层
什么无效
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-10 13:46:20 | 显示全部楼层

比方我这样写,它就显示不了...
  1. from win32gui import *
  2. import win32con
  3. import pywintypes
  4. from tkinter import *

  5. root=Tk()

  6. hwnd = pywintypes.HANDLE(int(root.frame(), 16))

  7. hdc=GetDC(hwnd)

  8. hbrush=GetStockObject(win32con.NULL_BRUSH)
  9. oldbrush=SelectObject(hdc,hbrush)
  10. Rectangle(hdc,50,50,100,100)

  11. root.geometry("400x400")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-10 13:55:43 | 显示全部楼层
本帖最后由 傻纸 于 2019-9-10 14:17 编辑


按理来说,可能也是要写在回调函数里面,就像win32,它也是在回调函数的,窗口重绘消息里面去画图。可能是要怎么捕获到tkinter窗口的重绘消息,然后把绘图的代码加进去。或者要在tk窗口的WM_CREATE消息里面,加入。。。感觉我对tkinter的事件都感到迷惑了,我理解得有点混乱

-----------
我好像又明白了一些,比如
  1. from win32gui import *
  2. import win32con
  3. import pywintypes
  4. from tkinter import *

  5. root=Tk()

  6. hwnd = pywintypes.HANDLE(int(root.frame(), 16))
  7. hdc=GetDC(hwnd)

  8. def callback(event):
  9.     #root.destroy()

  10.     hbrush=GetStockObject(win32con.NULL_BRUSH)
  11.     oldbrush=SelectObject(hdc,hbrush)
  12.     Rectangle(hdc,50,50,100,100)

  13. root.geometry("400x400")

  14. root.bind("<1>",callback)

  15. #root.mainloop()
复制代码

这样点击鼠标左键画出图。。那应该相当于win32里面的
case WM_LBUTTONDOWN:
{
}

那root窗口重绘事件也应该有,我还没找到...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-10 14:26:28 | 显示全部楼层
我貌似找到解决办法了
  1. from win32gui import *
  2. import win32con
  3. import pywintypes
  4. from tkinter import *

  5. root=Tk()

  6. hwnd = pywintypes.HANDLE(int(root.frame(), 16))
  7. hdc=GetDC(hwnd)

  8. def callback(event):
  9.     root.update()

  10.     hbrush=GetStockObject(win32con.NULL_BRUSH)
  11.     oldbrush=SelectObject(hdc,hbrush)
  12.     Rectangle(hdc,50,50,100,100)

  13. root.geometry("400x400")

  14. root.bind("<Configure>",callback)

  15. root.mainloop()
复制代码


这样感觉就没啥问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-23 08:31:21 | 显示全部楼层
顶上去
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-28 15:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表