pywin32获得tkinter窗口句柄,并在上面绘图
想实现用win32 API在tkinter窗口上画图,那么应该先获得tkinter窗口的句柄hwnd,然后再获得tkinter的设备hdc。尝试了FindWindow(),GetActiveWindow(),GetForegroundWindow()这几个函数貌似都不能正确获得tkinter的句柄
后来在stackoverflow上看到一个列子,使用pywintypes.HANDLE(int(root.frame(), 16))才成功。
原例:https://stackoverflow.com/questions/25602986/using-python-tkinter-how-to-smoothly-move-and-resize-the-non-focusable-window
这里我把原例中创建一个non focusable window的tkinter窗口,修改为把窗口设置样式设置为WS_POPUP
import tkinter as tk
import win32api
import win32con
import pywintypes root = tk.Tk() root.geometry("+0+0")
root.lift() hWindow = pywintypes.HANDLE(int(root.frame(), 16)) win32api.SetWindowLong(hWindow,win32con.GWL_STYLE,win32con.WS_POPUP) root.mainloop()

这个窗口风格,类似overrideredirect(1)
我想实现的功能是在tkinter窗口上绘画,那么似乎要考虑在哪里开始使用绘制代码,以及窗口重绘的问题。事实上,我只有把画图代码定义成一个事件的回调函数时,图形才绘制出来。而对窗口重绘没有找到办法
from win32gui import *
import win32con
import pywintypes from tkinter import * root=Tk() root.title("tkinterApp") hwnd = pywintypes.HANDLE(int(root.frame(), 16)) print(hwnd) hdc=GetDC(hwnd) def callback():
hbrush=GetStockObject(win32con.NULL_BRUSH)
oldbrush=SelectObject(hdc,hbrush)
Rectangle(hdc,50,50,100,100) Button(root,text="显示画图",command=callback).pack()
#
#ReleaseDC(hwnd,hdc) root.geometry("400x400") #root.mainloop()

解决方法
from win32gui import *
import win32con
import pywintypes
from tkinter import * root=Tk() hwnd = pywintypes.HANDLE(int(root.frame(), 16))
hdc=GetDC(hwnd) def callback(event):
root.update() hbrush=GetStockObject(win32con.NULL_BRUSH)
oldbrush=SelectObject(hdc,hbrush)
Rectangle(hdc,50,50,100,100) root.geometry("400x400") root.bind("<Configure>",callback) root.mainloop()
pywin32获得tkinter窗口句柄,并在上面绘图的更多相关文章
- pywin32获得tkinter的Canvas窗口句柄,并在上面绘图
上一篇博文获得主窗口句柄使用的是root.frame或者通过子控件调用master.frame方法,但是子控件本身没有frame方法.那么怎么获得子控件的句柄呢?试过了很多办法,想过把Canvas当作 ...
- Window 32位 编程总结
Lesson01 win32 Window 32位 编程 1.Windows编程基础 2.Windows的字符 3.窗口处理 4.消息处理 5.绘图 6.对话框 7.控件 Win32编程: Windo ...
- 【原】使用Tkinter绘制GUI并结合Matplotlib实现交互式绘图
在数据分析的过程中,往往需要对所建立的模型进行可视化,并调整其中的某些参数. 通常情况下,在Python中可以通过Matplotlib来进行绘制图像.然而该绘制过程是静态的,也就是每次调整完参数需要重 ...
- GUI tkinter (Canvas)绘图篇
from tkinter import * root = Tk()root.title("中国象棋棋盘手绘") can = Canvas(root,width = 400, hei ...
- VC++绘图时,利用双缓冲解决屏幕闪烁 转载
最近做中国象棋,绘制界面时遇到些问题,绘图过程中屏幕闪烁,估计都会想到利用双缓冲来解决问题,但查了下网上双缓冲的资料,发现基本是MFC的,转化为VC++后,大概代码如下: void DrawBmp(H ...
- python和pywin32实现窗口查找、遍历和点击
Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和 ...
- Tkinter教程之Canvas篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811803 '''Tkinter教程之Canvas篇(1)'''# 提供可以用来进行绘图的Co ...
- 调用API函数,在窗口非客户区绘图(通过GetWindowDC获得整个窗口的DC,就可以随意作画了)
http://hi.baidu.com/3582077/item/77d3c1ff60f9fa5ec9f33754 调用API函数,在窗口非客户区绘图 GDI+的Graphics类里有个FromHdc ...
- matlab的绘图保存
matlab的绘图和可视化能力是不用多说的,可以说在业内是家喻户晓的.Matlab提供了丰富的绘图函数,比如ez**系类的简易绘图函数,surf.mesh系类的数值绘图函数等几十个.另外其他专业工 ...
随机推荐
- code first网站发布后数据表中没有数据问题
code first网站发布后数据表中没有数据问题 (1).将internal sealed class Configuration类访问修饰符改为public class Configuratio ...
- Ubuntu跨版本安装软件
更新到Ubuntu 19.10之后,源里的Goldendict就会不时的崩溃,让我十分心累.过了这么长时间也一直没有更新,估计在20.04之前是不会更新了.这段时间因为疫情不能出门,正好看看这个问题, ...
- 十八、JavaScript之布尔类型
一.代码如下 二.运行效果 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...
- Spark 2.x Troubleshooting Guide
IBM在spark summit上分享的内容,包括编译spark源码,运行spark时候常见问题(缺包.OOM.GC问题.hdfs数据分布不均匀等),spark任务堆/thread dump 目录 编 ...
- 留学Essay写作中常见的两类要求词盘点
写essay的时候,我们会常常因为各式各样的要求词而头疼:discuss,describing,evaluate,explain,等等,他们之间有何区别?如果你在思考这个问题,那么这篇文章就是为你写的 ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- 关于 with 语句
class C(object): def __enter__(self): print('jinru') return self def __exit__(self, exc_type, exc_va ...
- POJ 1004:Financial Management
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 165062 Accepted: ...
- Vulkan 开发学习资料汇总
开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...
- 66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下: from django.db import models class Category(models.Model): name = models.Char ...