Tkinter相关教程:realpython ,tutorialspoint
给一段框出桌面上任意图标的代码练习——实际上是结合win32的python开源库uiautomation获取桌面上任意图标/窗口的坐标后,利用tkinter在图标上建立一个半透明带边框的窗口。
from tkinter import Tk, Canvas
import uiautomation as uia
def draw_rectangle(event):
control = uia.GetRootControl() # 获取桌面窗口的坐标,窗口类型等信息(与本文无关)
rect_info = control.BoundingRectangle
left_x, left_y, right_x, right_y, w, h = rect_info.left, rect_info.top, rect.right, rect_info.bottom # 左上xy右下xy坐标
tk = Tk()
tk.overrideredirect(True)
tk.attributes("-alpha", 0.8)
tk.wm_attributes('-topmost', True)
tk.geometry("{width}x{height}+{left_x}+{left_y}".format(width=right_x - left_x, height=right_y - left_y,
left_x=left_x, left_y=left_y))
cav = Canvas(tk, width=right_x - left_x, height=right_y - left_y,
bd=0, highlightthickness=4, highlightcolor='#F08080',highlightbackground="#F08080")
rect = cav.create_rectangle(0, 0, right_x, right_y, fill='#48D1CC', outline='#F08080', width=4)
cav.pack()
# cav.bind("", click_handler)
# tk.update_idletasks()
# tk.update()
tk.mainloop()
tk = Tk()
tk.overrideredirect(True)
tk.attributes("-alpha", 0.5) # 整个窗口的透明度设置 - 影响全局。0.5 透明程度
tk.wm_attributes("-disabled", True) # 禁用窗口
tr.wm_attributes("-topmost", True) # 置于顶层
tk.geometry("200x300+20+30") # 宽x高+x轴+y轴, 注意宽x高是小写x不是*
Frame Label Listbox MenuButton Menu Message Radiobutton Scale Scrollbar Text Toplevel 顶层窗口 Spinbox PanedWindow LabelFrame tkMessageBox
cav = Canvas(tk, width=100, height=20)
# *args介绍
bd = 2 # 边框宽度 默认为2
bg = "blue" # 背景色
scrollregion = tuple(left, top, right, bottom) # 控制画布可以滚动的区域
confine = True # 默认为True,不可滚动到scrollregion定义的区域外
xscrollincrement # 横向滑动
yscrollincrement # 垂直运动
# 方法:
canvas.create_arc(coord, start=0, extent=150, fill="blue") # 扇形
canvas.create_image(50, 50, anchor=NE, image=filename) # 图片
canvas.create_line(x0, y0, x1, y1) # 线
canvas.create_line(left_x, left_y, right_x, right_y, options) # 方框
# options: fill 填充色,width 边框, outline 边框色
canvas.pack() # 将部件组织成块
canvas.grid() # 以表格相似的样式布局
canvas.place() # 将部件放在父类部件特定位置
pack,grid,place的样式