|

楼主 |
发表于 2024-12-25 00:57:10
|
显示全部楼层
- from tkinter import *
- from tkinter import ttk
- from PIL import Image, ImageGrab, ImageTk
-
- def area_sel():
- def getPress(event):
- global press_x,press_y
- press_x,press_y = event.x,event.y
-
- def mouseMove(event):
- global press_x, press_y, rectangleId
- cancas.delete(rectangleId)
- rectangleId = cancas.create_rectangle(press_x,press_y,event.x,event.y,width=1)
-
- def getRelease(event):
- global press_x, press_y, rectangleId
- top.withdraw()
- img = ImageGrab.grab((press_x, press_y,event.x,event.y))
- img.save('1.png')
- img_tk = ImageTk.PhotoImage(img.resize((30, 30), Image.Resampling.LANCZOS))
- img_label.config(image=img_tk)
- img_label.image = img_tk # 防止垃圾回收
-
-
- top = Toplevel()
- top.state('zoomed')
- top.overrideredirect(1)
- cancas = Canvas(top)
-
- background = ImageTk.PhotoImage(ImageGrab.grab().convert("L"))
- cancas.create_image(0,0,anchor="nw",image=background)
-
- cancas.bind('<Button-1>',getPress)
- cancas.bind('<B1-Motion>',mouseMove)
- cancas.bind('<ButtonRelease-1>',getRelease)
-
- cancas.pack(expand="YES",fill="both")
- top.mainloop()
- def on_select(event):
- value = combo.get()
- print("选中的值: ", value)
- def add_op():
- # 创建下拉列表
- combo = ttk.Combobox(root)
- combo["values"] = ("左击", "右击", "移至")
- combo.current(0) # 设置默认选中第一项
- combo.pack(pady=10)
- # 绑定事件
- combo.bind("<<ComboboxSelected>>", on_select)
- img_label = Label(root)
- img= Image.open('1.png')
- img_tk = ImageTk.PhotoImage(img.resize((30, 30), Image.Resampling.LANCZOS))
- img_label.config(image=img_tk)
- img_label.image = img_tk # 防止垃圾回收
- img_label.pack(pady=10)
-
- sel_btn = Button(root, text='点我截图', width=20, command=area_sel)
- sel_btn.pack()
- #用一个列表来记录所有的操作
- op = list()
-
- root = Tk()
- root.geometry('460x240')
- rectangleId = None
-
- op_btn = Button(root, text='添加操作', width=20, command=add_op)
- op_btn.pack()
-
- root.mainloop()
复制代码 |
|