Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 831|回复: 1

python 桌面截图

[复制链接]

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
发表于 2024-12-24 23:36:25 | 显示全部楼层 |阅读模式
  1. from tkinter import *
  2. from PIL import Image, ImageGrab, ImageTk

  3. def area_sel():
  4.     def getPress(event):
  5.         global press_x,press_y
  6.         press_x,press_y = event.x,event.y

  7.     def mouseMove(event):
  8.         global press_x, press_y, rectangleId
  9.         cancas.delete(rectangleId)
  10.         rectangleId = cancas.create_rectangle(press_x,press_y,event.x,event.y,width=1)

  11.     def getRelease(event):
  12.         global press_x, press_y, rectangleId
  13.         top.withdraw()
  14.         img = ImageGrab.grab((press_x, press_y,event.x,event.y))
  15.         img.save('1.png')
  16.         img_tk = ImageTk.PhotoImage(img)
  17.         img_label.config(image=img_tk)
  18.         img_label.image = img_tk  # 防止垃圾回收

  19.     top = Toplevel()
  20.     top.state('zoomed')
  21.     top.overrideredirect(1)
  22.     cancas = Canvas(top)

  23.     background = ImageTk.PhotoImage(ImageGrab.grab().convert("L"))
  24.     cancas.create_image(0,0,anchor="nw",image=background)

  25.     cancas.bind('<Button-1>',getPress)
  26.     cancas.bind('<B1-Motion>',mouseMove)
  27.     cancas.bind('<ButtonRelease-1>',getRelease)

  28.     cancas.pack(expand="YES",fill="both")
  29.     top.mainloop()

  30. root = Tk()
  31. rectangleId = None
  32. sel_btn = Button(root, text='点我截图', width=20, command=area_sel)
  33. sel_btn.pack()

  34. img_label = Label(root)
  35. img_label.pack(pady=20)

  36. root.mainloop()
复制代码
回复

使用道具 举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2024-12-25 00:57:10 | 显示全部楼层
  1. from tkinter import *
  2. from tkinter import ttk
  3. from PIL import Image, ImageGrab, ImageTk

  4. def area_sel():
  5.     def getPress(event):
  6.         global press_x,press_y
  7.         press_x,press_y = event.x,event.y

  8.     def mouseMove(event):
  9.         global press_x, press_y, rectangleId
  10.         cancas.delete(rectangleId)
  11.         rectangleId = cancas.create_rectangle(press_x,press_y,event.x,event.y,width=1)

  12.     def getRelease(event):
  13.         global press_x, press_y, rectangleId
  14.         top.withdraw()
  15.         img = ImageGrab.grab((press_x, press_y,event.x,event.y))
  16.         img.save('1.png')
  17.         img_tk = ImageTk.PhotoImage(img.resize((30, 30), Image.Resampling.LANCZOS))
  18.         img_label.config(image=img_tk)
  19.         img_label.image = img_tk  # 防止垃圾回收
  20.    

  21.     top = Toplevel()
  22.     top.state('zoomed')
  23.     top.overrideredirect(1)
  24.     cancas = Canvas(top)

  25.     background = ImageTk.PhotoImage(ImageGrab.grab().convert("L"))
  26.     cancas.create_image(0,0,anchor="nw",image=background)

  27.     cancas.bind('<Button-1>',getPress)
  28.     cancas.bind('<B1-Motion>',mouseMove)
  29.     cancas.bind('<ButtonRelease-1>',getRelease)

  30.     cancas.pack(expand="YES",fill="both")
  31.     top.mainloop()

  32. def on_select(event):
  33.     value = combo.get()
  34.     print("选中的值: ", value)

  35. def add_op():
  36.     # 创建下拉列表
  37.     combo = ttk.Combobox(root)
  38.     combo["values"] = ("左击", "右击", "移至")
  39.     combo.current(0)  # 设置默认选中第一项
  40.     combo.pack(pady=10)
  41.     # 绑定事件
  42.     combo.bind("<<ComboboxSelected>>", on_select)

  43.     img_label = Label(root)
  44.     img= Image.open('1.png')
  45.     img_tk = ImageTk.PhotoImage(img.resize((30, 30), Image.Resampling.LANCZOS))
  46.     img_label.config(image=img_tk)
  47.     img_label.image = img_tk  # 防止垃圾回收
  48.     img_label.pack(pady=10)
  49.         
  50.     sel_btn = Button(root, text='点我截图', width=20, command=area_sel)
  51.     sel_btn.pack()

  52. #用一个列表来记录所有的操作
  53. op = list()


  54. root = Tk()
  55. root.geometry('460x240')
  56. rectangleId = None




  57. op_btn = Button(root, text='添加操作', width=20, command=add_op)
  58. op_btn.pack()

  59. root.mainloop()
复制代码
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-5 03:45 , Processed in 0.036052 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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