当前位置: 首页 > news >正文

我叕更新了~

import tkinter as tk
from tkinter import messagebox, colorchooser, Spinbox
import win32api
import win32con
import win32gui
import win32clipboard
import ctypes
import time
from PIL import ImageGrab  # 用于截图保存
import random

# 扩展图形类型枚举(移除平行四边形,新增梯形和弧线)
SQUARE, CIRCLE, TRIANGLE, TRAPEZOID, ARC = 0, 1, 2, 3, 4
g_currentShape = SQUARE
g_startPos = None
g_endPos = None
g_isDrawing = False

# 定义光标切换的状态
ARROW, HAND, CROSS = 0, 1, 2
currentCursor = CROSS

root = tk.Tk()
root.title(" 绘画")
root.geometry("1000x1000")

g_isInTray = False
g_isFullscreen = False
g_isPasswordMode = True  # 初始为密码模式

# 当前绘图颜色
current_color = "black"
# 线条/填充模式,True 表示填充,False 表示线条
fill_mode = True
# 当前线条粗细
line_width = 2

# 梯形顶点计算
def CalculateTrapezoidPoints(start, end):
    height = abs(end[1] - start[1])         # 垂直高度
    topWidth = abs(end[0] - start[0])       # 上底宽度
    bottomWidth = topWidth + height * 2     # 下底宽度

    points = [
        start,
        (start[0] + topWidth, start[1]),
        (end[0] + bottomWidth, end[1]),
        (end[0] - bottomWidth, end[1])
    ]
    return points

# 三角形顶点计算函数
def CalculateTrianglePoints(start, end):
    # 计算底边中点
    baseMid = (
        (start[0] + end[0]) // 2,
        (start[1] + end[1]) // 2
    )

    # 计算高度方向
    height = int((((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) ** 0.5) / 2)

    # 设置三个顶点(等腰三角形)
    points = [
        start,
        end,
        (baseMid[0], baseMid[1] - height)
    ]
    return points

# 初始化托盘图标
def InitTrayIcon():
    global g_nid
    g_nid = (
        win32gui.NOTIFYICONDATA()
    )
    g_nid.cbSize = ctypes.sizeof(g_nid)
    g_nid.hWnd = root.winfo_id()
    g_nid.uID = 1
    g_nid.uFlags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
    g_nid.uCallbackMessage = win32con.WM_USER + 1
    g_nid.hIcon = win32gui.LoadIcon(0, win32con.IDI_INFORMATION)
    g_nid.szTip = "MyApp"

# 切换托盘状态
def ToggleTrayMode():
    global g_isInTray
    if not g_isInTray:
        # 最小化到托盘
        root.withdraw()
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, g_nid)
    else:
        # 恢复窗口
        root.deiconify()
        win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, g_nid)
    g_isInTray = not g_isInTray

# 切换全屏函数
def ToggleFullscreen():
    global g_isFullscreen
    if not g_isFullscreen:
        # 进入全屏
        root.attributes('-fullscreen', True)
    else:
        # 退出全屏
        root.attributes('-fullscreen', False)
    g_isFullscreen = not g_isFullscreen

# 切换密码显示模式
def TogglePasswordMode():
    global g_isPasswordMode
    # 保存当前文本
    text = entry.get()

    # 销毁旧编辑框
    entry.destroy()

    # 创建新编辑框(切换样式)
    if g_isPasswordMode:
        new_entry = tk.Entry(root, show="*")
    else:
        new_entry = tk.Entry(root)

    new_entry.insert(0, text)
    new_entry.place(x=20, y=250, width=200, height=25)

    # 更新模式状态
    g_isPasswordMode = not g_isPasswordMode

    # 更新按钮文本
    if g_isPasswordMode:
        toggle_button.config(text="隐藏密码")
    else:
        toggle_button.config(text="显示明文")

# 窗口过程函数
def on_event(event):
    global g_startPos, g_endPos, g_isDrawing
    if event.type == tk.EventType.ButtonPress and event.num == 1:
        g_startPos = (event.x, event.y)
        g_isDrawing = True
    elif event.type == tk.EventType.ButtonRelease and event.num == 1:
        g_isDrawing = False
    elif event.type == tk.EventType.Motion and g_isDrawing:
        g_endPos = (event.x, event.y)
        canvas.delete("all")
        draw_shape()

# 绘制图形
def draw_shape():
    global g_currentShape, g_startPos, g_endPos, current_color, fill_mode, line_width
    if g_startPos and g_endPos:
        if g_currentShape == CIRCLE:
            radius = int(((g_endPos[0] - g_startPos[0]) ** 2 + (g_endPos[1] - g_startPos[1]) ** 2) ** 0.5)
            if fill_mode:
                canvas.create_oval(
                    g_startPos[0] - radius, g_startPos[1] - radius,
                    g_startPos[0] + radius, g_startPos[1] + radius,
                    fill=current_color
                )
            else:
                canvas.create_oval(
                    g_startPos[0] - radius, g_startPos[1] - radius,
                    g_startPos[0] + radius, g_startPos[1] + radius,
                    outline=current_color, width=line_width
                )
        elif g_currentShape == SQUARE:
            if fill_mode:
                canvas.create_rectangle(
                    min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1]),
                    max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1]),
                    fill=current_color
                )
            else:
                canvas.create_rectangle(
                    min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1]),
                    max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1]),
                    outline=current_color, width=line_width
                )
        elif g_currentShape == TRIANGLE:
            points = CalculateTrianglePoints(g_startPos, g_endPos)
            if fill_mode:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], fill=current_color)
            else:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], outline=current_color, width=line_width)
        elif g_currentShape == TRAPEZOID:
            points = CalculateTrapezoidPoints(g_startPos, g_endPos)
            if fill_mode:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], fill=current_color)
            else:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], outline=current_color, width=line_width)
        elif g_currentShape == ARC:
            x1, y1 = min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1])
            x2, y2 = max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1])
            start_angle = random.randint(0, 360)
            extent_angle = random.randint(0, 360)
            if fill_mode:
                canvas.create_arc(x1, y1, x2, y2, start=start_angle, extent=extent_angle, fill=current_color)
            else:
                canvas.create_arc(x1, y1, x2, y2, start=start_angle, extent=extent_angle, outline=current_color, width=line_width)

# 处理托盘图标消息
def on_tray_icon_event(msg, wparam, lparam):
    if lparam == win32con.WM_LBUTTONDBLCLK:
        ToggleTrayMode()
    return True

# 处理命令事件
def on_command(event):
    global g_currentShape, currentCursor, current_color, fill_mode, line_width
    if event.widget == shape_button:
        g_currentShape = (g_currentShape + 1) % 5  # 这里改为5,因为增加了弧线类型
        # 更新按钮文本
        texts = [
            "切换图形(当前:长方形)",
            "切换图形(当前:圆形)",
            "切换图形(当前:三角形)",
            "切换图形(当前:梯形)",
            "切换图形(当前:弧线)"
        ]
        shape_button.config(text=texts[g_currentShape])
        canvas.delete("all")
    elif event.widget == copy_button:
        # 获取输入框内容
        text = entry.get()
        # 打开剪贴板
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardText(text)
        win32clipboard.CloseClipboard()
        messagebox.showinfo("成功", "内容已复制到剪贴板!")
    elif event.widget == cursor_button:
        if currentCursor == CROSS:
            currentCursor = HAND
        elif currentCursor == HAND:
            currentCursor = ARROW
        else:
            currentCursor = CROSS
        # 强制更新光标
        root.config(cursor=get_cursor(currentCursor))
    elif event.widget == time_button:
        # 获取本地时间
        localtime = time.localtime()
        time_str = time.strftime("%Y-%m-%d %H:%M:%S", localtime)
        messagebox.showinfo("系统时间", time_str)
    elif event.widget == tray_button:
        ToggleTrayMode()
    elif event.widget == shortcut_button:
        messagebox.showinfo("提示", "请先在D盘创建Draw.cpp,若已经创建,请忽略!")
        try:
            win32api.ShellExecute(
                0,
                "runas",
                "powershell",
                "-command \"$WshShell=New-Object -comObject WScript.Shell; $Shortcut=$WshShell.CreateShortcut(\'%UserProfile%\\Desktop\\Draw.lnk\'); $Shortcut.TargetPath=\'D:\\Draw.exe'; $Shortcut.Save()\"",
                None,
                1
            )
            messagebox.showinfo("成功", "创建成功!")
        except:
            messagebox.showerror("错误", "创建失败!")
    elif event.widget == submit_button:
        text = entry.get()
        if not text:
            messagebox.showerror("错误", "输入不能为空!")
        else:
            messagebox.showinfo("输入内容", text)
    elif event.widget == toggle_button:
        TogglePasswordMode()
    elif event.widget == version_button:
        messagebox.showinfo("版本号", "v1.17.15")
    elif event.widget == close_button:
        root.destroy()
    elif event.widget == fullscreen_button:
        ToggleFullscreen()
    elif event.widget == color_button:
        color = colorchooser.askcolor()[1]
        if color:
            current_color = color
    elif event.widget == mode_button:
        fill_mode = not fill_mode
        if fill_mode:
            mode_button.config(text="切换到线条")
        else:
            mode_button.config(text="切换到填充")
    elif event.widget == save_button:
        save_canvas_as_image()
    elif event.widget == width_button:
        choose_line_width()
    elif event.widget == auto_generate_button:
        auto_generate_graphic()

# 获取光标
def get_cursor(state):
    if state == ARROW:
        return "arrow"
    elif state == HAND:
        return "hand2"
    elif state == CROSS:
        return "crosshair"

# 保存画布内容为图片
def save_canvas_as_image():
    x = root.winfo_rootx() + canvas.winfo_x()
    y = root.winfo_rooty() + canvas.winfo_y()
    x1 = x + canvas.winfo_width()
    y1 = y + canvas.winfo_height()
    img = ImageGrab.grab(bbox=(x, y, x1, y1))
    img.save("drawing.png")  # 保存到当前目录,可修改路径
    messagebox.showinfo("保存成功", "绘图作品已保存到 drawing.png")

# 选择线条粗细的函数
def choose_line_width():
    global line_width
    def set_width():
        nonlocal line_width
        line_width = int(spinbox.get())
        width_window.destroy()

    width_window = tk.Toplevel(root)
    width_window.title("选择线条粗细")
    label = tk.Label(width_window, text="请选择线条粗细:")
    label.pack(pady=10)
    spinbox = Spinbox(width_window, from_=1, to=20, width=5)
    spinbox.set(line_width)
    spinbox.pack(pady=10)
    button = tk.Button(width_window, text="确定", command=set_width)
    button.pack(pady=10)

# 自动生成图形的函数
def auto_generate_graphic():
    global g_currentShape, current_color, fill_mode, line_width
    g_currentShape = random.randint(0, 4)  # 随机选择图形类型
    current_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))  # 随机生成颜色
    fill_mode = random.choice([True, False])  # 随机选择填充或线条模式
    line_width = random.randint(1, 20)  # 随机选择线条粗细

    # 随机生成起始点和结束点
    x1, y1 = random.randint(0, canvas.winfo_width()), random.randint(0, canvas.winfo_height())
    x2, y2 = random.randint(0, canvas.winfo_width()), random.randint(0, canvas.winfo_height())
    g_startPos = (x1, y1)
    g_endPos = (x2, y2)

    draw_shape()

    # 更新按钮文本
    texts = [
        "切换图形(当前:长方形)",
        "切换图形(当前:圆形)",
        "切换图形(当前:三角形)",
        "切换图形(当前:梯形)",
        "切换图形(当前:弧线)"
    ]
    shape_button.config(text=texts[g_currentShape])

# 创建画布
canvas = tk.Canvas(root, bg="white")
canvas.pack(fill=tk.BOTH, expand=True)
canvas.bind("<Button-1>", on_event)
canvas.bind("<B1-Motion>", on_event)
canvas.bind("<ButtonRelease-1>", on_event)

# 创建按钮
shape_button = tk.Button(root, text="切换图形(当前:方形)", command=lambda: on_command(None))
shape_button.place(x=400, y=10, width=220, height=30)

tray_button = tk.Button(root, text="最小化到托盘", command=lambda: on_command(None))
tray_button.place(x=400, y=50, width=150, height=30)

cursor_button = tk.Button(root, text="切换光标", command=lambda: on_command(None))
cursor_button.place(x=20, y=450, width=100, height=30)

time_button = tk.Button(root, text="获取系统时间", command=lambda: on_command(None))
time_button.place(x=20, y=350, width=150, height=30)

close_button = tk.Button(root, text="点击我关闭", command=lambda: on_command(None))
close_button.place(x=50, y=50, width=100, height=30)

version_button = tk.Button(root, text="版本号", command=lambda: on_command(None))
version_button.place(x=10, y=10, width=100, height=30)

fullscreen_button = tk.Button(root, text="点击我切换全屏", command=lambda: on_command(None))
fullscreen_button.place(x=850, y=50, width=100, height=30)

shortcut_button = tk.Button

还有

import tkinter as tk
from tkinter import messagebox, colorchooser, Spinbox
import win32api
import win32con
import win32gui
import win32clipboard
import ctypes
import time
from PIL import ImageGrab  # 用于截图保存
import random

# 扩展图形类型枚举(移除平行四边形,新增梯形和弧线)
SQUARE, CIRCLE, TRIANGLE, TRAPEZOID, ARC = 0, 1, 2, 3, 4
g_currentShape = SQUARE
g_startPos = None
g_endPos = None
g_isDrawing = False

# 定义光标切换的状态
ARROW, HAND, CROSS = 0, 1, 2
currentCursor = CROSS

root = tk.Tk()
root.title(" 绘画")
root.geometry("1000x1000")

g_isInTray = False
g_isFullscreen = False
g_isPasswordMode = True  # 初始为密码模式

# 当前绘图颜色
current_color = "black"
# 线条/填充模式,True 表示填充,False 表示线条
fill_mode = True
# 当前线条粗细
line_width = 2

# 梯形顶点计算
def CalculateTrapezoidPoints(start, end):
    height = abs(end[1] - start[1])         # 垂直高度
    topWidth = abs(end[0] - start[0])       # 上底宽度
    bottomWidth = topWidth + height * 2     # 下底宽度(网页5的增量模式)

    points = [
        start,
        (start[0] + topWidth, start[1]),
        (end[0] + bottomWidth, end[1]),
        (end[0] - bottomWidth, end[1])
    ]
    return points

# 三角形顶点计算函数
def CalculateTrianglePoints(start, end):
    # 计算底边中点
    baseMid = (
        (start[0] + end[0]) // 2,
        (start[1] + end[1]) // 2
    )

    # 计算高度方向
    height = int((((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) ** 0.5) / 2)

    # 设置三个顶点(等腰三角形)
    points = [
        start,
        end,
        (baseMid[0], baseMid[1] - height)
    ]
    return points

# 初始化托盘图标
def InitTrayIcon():
    global g_nid
    g_nid = (
        win32gui.NOTIFYICONDATA()
    )
    g_nid.cbSize = ctypes.sizeof(g_nid)
    g_nid.hWnd = root.winfo_id()
    g_nid.uID = 1
    g_nid.uFlags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
    g_nid.uCallbackMessage = win32con.WM_USER + 1
    g_nid.hIcon = win32gui.LoadIcon(0, win32con.IDI_INFORMATION)
    g_nid.szTip = "MyApp"

# 切换托盘状态
def ToggleTrayMode():
    global g_isInTray
    if not g_isInTray:
        # 最小化到托盘
        root.withdraw()
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, g_nid)
    else:
        # 恢复窗口
        root.deiconify()
        win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, g_nid)
    g_isInTray = not g_isInTray

# 切换全屏函数
def ToggleFullscreen():
    global g_isFullscreen
    if not g_isFullscreen:
        # 进入全屏
        root.attributes('-fullscreen', True)
    else:
        # 退出全屏
        root.attributes('-fullscreen', False)
    g_isFullscreen = not g_isFullscreen

# 切换密码显示模式
def TogglePasswordMode():
    global g_isPasswordMode
    # 保存当前文本
    text = entry.get()

    # 销毁旧编辑框
    entry.destroy()

    # 创建新编辑框(切换样式)
    if g_isPasswordMode:
        new_entry = tk.Entry(root, show="*")
    else:
        new_entry = tk.Entry(root)

    new_entry.insert(0, text)
    new_entry.place(x=20, y=250, width=200, height=25)

    # 更新模式状态
    g_isPasswordMode = not g_isPasswordMode

    # 更新按钮文本
    if g_isPasswordMode:
        toggle_button.config(text="隐藏密码")
    else:
        toggle_button.config(text="显示明文")

# 窗口过程函数
def on_event(event):
    global g_startPos, g_endPos, g_isDrawing
    if event.type == tk.EventType.ButtonPress and event.num == 1:
        g_startPos = (event.x, event.y)
        g_isDrawing = True
    elif event.type == tk.EventType.ButtonRelease and event.num == 1:
        g_isDrawing = False
    elif event.type == tk.EventType.Motion and g_isDrawing:
        g_endPos = (event.x, event.y)
        canvas.delete("all")
        draw_shape()

# 绘制图形
def draw_shape():
    global g_currentShape, g_startPos, g_endPos, current_color, fill_mode, line_width
    if g_startPos and g_endPos:
        if g_currentShape == CIRCLE:
            radius = int(((g_endPos[0] - g_startPos[0]) ** 2 + (g_endPos[1] - g_startPos[1]) ** 2) ** 0.5)
            if fill_mode:
                canvas.create_oval(
                    g_startPos[0] - radius, g_startPos[1] - radius,
                    g_startPos[0] + radius, g_startPos[1] + radius,
                    fill=current_color
                )
            else:
                canvas.create_oval(
                    g_startPos[0] - radius, g_startPos[1] - radius,
                    g_startPos[0] + radius, g_startPos[1] + radius,
                    outline=current_color, width=line_width
                )
        elif g_currentShape == SQUARE:
            if fill_mode:
                canvas.create_rectangle(
                    min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1]),
                    max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1]),
                    fill=current_color
                )
            else:
                canvas.create_rectangle(
                    min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1]),
                    max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1]),
                    outline=current_color, width=line_width
                )
        elif g_currentShape == TRIANGLE:
            points = CalculateTrianglePoints(g_startPos, g_endPos)
            if fill_mode:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], fill=current_color)
            else:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], outline=current_color, width=line_width)
        elif g_currentShape == TRAPEZOID:
            points = CalculateTrapezoidPoints(g_startPos, g_endPos)
            if fill_mode:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], fill=current_color)
            else:
                canvas.create_polygon([p[0] for p in points], [p[1] for p in points], outline=current_color, width=line_width)
        elif g_currentShape == ARC:
            x1, y1 = min(g_startPos[0], g_endPos[0]), min(g_startPos[1], g_endPos[1])
            x2, y2 = max(g_startPos[0], g_endPos[0]), max(g_startPos[1], g_endPos[1])
            start_angle = random.randint(0, 360)
            extent_angle = random.randint(0, 360)
            if fill_mode:
                canvas.create_arc(x1, y1, x2, y2, start=start_angle, extent=extent_angle, fill=current_color)
            else:
                canvas.create_arc(x1, y1, x2, y2, start=start_angle, extent=extent_angle, outline=current_color, width=line_width)

# 处理托盘图标消息
def on_tray_icon_event(msg, wparam, lparam):
    if lparam == win32con.WM_LBUTTONDBLCLK:
        ToggleTrayMode()
    return True

# 处理命令事件
def on_command(event):
    global g_currentShape, currentCursor, current_color, fill_mode, line_width
    if event.widget == shape_button:
        g_currentShape = (g_currentShape + 1) % 5  # 这里改为5,因为增加了弧线类型
        # 更新按钮文本
        texts = [
            "切换图形(当前:长方形)",
            "切换图形(当前:圆形)",
            "切换图形(当前:三角形)",
            "切换图形(当前:梯形)",
            "切换图形(当前:弧线)"
        ]
        shape_button.config(text=texts[g_currentShape])
        canvas.delete("all")
    elif event.widget == copy_button:
        # 获取输入框内容
        text = entry.get()
        # 打开剪贴板
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardText(text)
        win32clipboard.CloseClipboard()
        messagebox.showinfo("成功", "内容已复制到剪贴板!")
    elif event.widget == cursor_button:
        if currentCursor == CROSS:
            currentCursor = HAND
        elif currentCursor == HAND:
            currentCursor = ARROW
        else:
            currentCursor = CROSS
        # 强制更新光标
        root.config(cursor=get_cursor(currentCursor))
    elif event.widget == time_button:
        # 获取本地时间
        localtime = time.localtime()
        time_str = time.strftime("%Y-%m-%d %H:%M:%S", localtime)
        messagebox.showinfo("系统时间", time_str)
    elif event.widget == tray_button:
        ToggleTrayMode()
    elif event.widget == shortcut_button:
        messagebox.showinfo("提示", "请先在D盘创建Draw.cpp,若已经创建,请忽略!")
        try:
            win32api.ShellExecute(
                0,
                "runas",
                "powershell",
                "-command \"$WshShell=New-Object -comObject WScript.Shell; $Shortcut=$WshShell.CreateShortcut(\'%UserProfile%\\Desktop\\Draw.lnk\'); $Shortcut.TargetPath=\'D:\\Draw.exe'; $Shortcut.Save()\"",
                None,
                1
            )
            messagebox.showinfo("成功", "创建成功!")
        except:
            messagebox.showerror("错误", "创建失败!")
    elif event.widget == submit_button:
        text = entry.get()
        if not text:
            messagebox.showerror("错误", "输入不能为空!")
        else:
            messagebox.showinfo("输入内容", text)
    elif event.widget == toggle_button:
        TogglePasswordMode()
    elif event.widget == version_button:
        messagebox.showinfo("版本号", "v1.17.15")
    elif event.widget == close_button:
        root.destroy()
    elif event.widget == fullscreen_button:
        ToggleFullscreen()
    elif event.widget == color_button:
        color = colorchooser.askcolor()[1]
        if color:
            current_color = color
    elif event.widget == mode_button:
        fill_mode = not fill_mode
        if fill_mode:
            mode_button.config(text="切换到线条")
        else:
            mode_button.config(text="切换到填充")
    elif event.widget == save_button:
        save_canvas_as_image()
    elif event.widget == width_button:
        choose_line_width()
    elif event.widget == auto_generate_button:
        auto_generate_graphic()

# 获取光标
def get_cursor(state):
    if state == ARROW:
        return "arrow"
    elif state == HAND:
        return "hand2"
    elif state == CROSS:
        return "crosshair"

# 保存画布内容为图片
def save_canvas_as_image():
    x = root.winfo_rootx() + canvas.winfo_x()
    y = root.winfo_rooty() + canvas.winfo_y()
    x1 = x + canvas.winfo_width()
    y1 = y + canvas.winfo_height()
    img = ImageGrab.grab(bbox=(x, y, x1, y1))
    img.save("drawing.png")  # 保存到当前目录,可修改路径
    messagebox.showinfo("保存成功", "绘图作品已保存到 drawing.png")

# 选择线条粗细的函数
def choose_line_width():
    global line_width
    def set_width():
        nonlocal line_width
        line_width = int(spinbox.get())
        width_window.destroy()

    width_window = tk.Toplevel(root)
    width_window.title("选择线条粗细")
    label = tk.Label(width_window, text="请选择线条粗细:")
    label.pack(pady=10)
    spinbox = Spinbox(width_window, from_=1, to=20, width=5)
    spinbox.set(line_width)
    spinbox.pack(pady=10)
    button = tk.Button(width_window, text="确定", command=set_width)
    button.pack(pady=10)

# 自动生成图形的函数
def auto_generate_graphic():
    global g_currentShape, current_color, fill_mode, line_width
    g_currentShape = random.randint(0, 4)  # 随机选择图形类型
    current_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))  # 随机生成颜色
    fill_mode = random.choice([True, False])  # 随机选择填充或线条模式
    line_width = random.randint(1, 20)  # 随机选择线条粗细

    # 随机生成起始点和结束点
    x1, y1 = random.randint(0, canvas.winfo_width()), random.randint(0, canvas.winfo_height())
    x2, y2 = random.randint(0, canvas.winfo_width()), random.randint(0, canvas.winfo_height())
    g_startPos = (x1, y1)
    g_endPos = (x2, y2)

    draw_shape()

    # 更新按钮文本
    texts = [
        "切换图形(当前:长方形)",
        "切换图形(当前:圆形)",
        "切换图形(当前:三角形)",
        "切换图形(当前:梯形)",
        "切换图形(当前:弧线)"
    ]
    shape_button.config(text=texts[g_currentShape])

# 创建画布
canvas = tk.Canvas(root, bg="white")
canvas.pack(fill=tk.BOTH, expand=True)
canvas.bind("<Button-1>", on_event)
canvas.bind("<B1-Motion>", on_event)
canvas.bind("<ButtonRelease-1>", on_event)

# 创建按钮
shape_button = tk.Button(root, text="切换图形(当前:方形)", command=lambda: on_command(None))
shape_button.place(x=400, y=10, width=220, height=30)

tray_button = tk.Button(root, text="最小化到托盘", command=lambda: on_command(None))
tray_button.place(x=400, y=50, width=150, height=30)

cursor_button = tk.Button(root, text="切换光标", command=lambda: on_command(None))
cursor_button.place(x=20, y=450, width=100, height=30)

time_button = tk.Button(root, text="获取系统时间", command=lambda: on_command(None))
time_button.place(x=20, y=350, width=150, height=30)

close_button = tk.Button(root, text="点击我关闭", command=lambda: on_command(None))
close_button.place(x=50, y=50, width=100, height=30)

version_button = tk.Button(root, text="版本号", command=lambda: on_command(None))
version_button.place(x=10, y=10, width=100, height=30)

fullscreen_button = tk.Button(root, text

相关文章:

  • 透明的卡组收费模式IC++
  • STM32单片机入门学习——第6节: [3-2] LED闪烁LED流水灯蜂鸣器
  • go游戏后端开发20:房间消息推送处理
  • Java基础 4.3
  • Linux文件属性
  • hanzi-writer-miniprogram真机显示不出来Path2D问题已解决(真机能显示了!)
  • UE5Actor模块源码深度剖析:从核心架构到实践应用
  • webrtc 本地运行的详细操作步骤 1
  • LoRa模块通信距离优化:如何实现低功耗覆盖30公里无线传输要求
  • 基于lora的llama2二次预训练
  • 力扣算法ing(47 / 100)
  • 使用ssh连接上开发板
  • Java基础:面向对象进阶(二)
  • 创维E900V22C/E900V22D_S905L3(B)_安卓9.0_指示灯正常_线刷固件包
  • Oracle数据库数据编程SQL<4.2 锁机制>
  • YOLO与SSD对比
  • 前端Uniapp接入UviewPlus详细教程!!!
  • 从零到1搭建流媒体服务器
  • C++基础系列【35】巧用assert
  • ARM Cortex-M用于控制中断和异常处理的寄存器:BASEPRI、PRIMASK 和 FAULTMASK