import tkinter as tk
from tkinter import colorchooser, filedialog
from tkinter import ttk
from PIL import ImageGrab
# 这段Python代码使用Tkinter库实现了一个交互式的图形用户界面(GUI),用于绘制简单的图形,如点线、直线、矩形和椭圆。用户可以通过鼠标在界面上绘制这些图形。主要功能如下:
#
# 图形选择:用户可以通过下拉菜单选择要绘制的图形(点线、直线、矩形和椭圆)。
# 线型选择:用户可以选择不同的线型(实线、虚线、点线、虚点线)。
# 颜色选择:用户可以通过颜色选择器选择绘图颜色。
# 保存功能:用户可以将绘制的图形保存为图像文件。
# 清除画布:用户可以清除画布上的所有图形。
# 退出程序:用户可以退出程序。
class DrawApp:
def __init__(self, root):
self.root = root
self.root.title("DrawApp")
self.root.geometry("800x600")
self.root.configure(background='white')
self.root.update_idletasks()
self.root.eval('tk::PlaceWindow . center')
self.current_color = "black"
self.current_shape = "dot"
self.current_line_type = "-"
self.canvas = tk.Canvas(self.root, bg="white", width=700, height=500)
self.canvas.pack(pady=20)
self.control_frame = ttk.Frame(self.root)
self.control_frame.pack(pady=10)
self.add_controls()
self.canvas.bind("<Button-1>", self.start_draw)
self.canvas.bind("<B1-Motion>", self.draw)
self.canvas.bind("<ButtonRelease-1>", self.end_draw)
self.start_x, self.start_y = None, None
self.current_obj = None
def add_controls(self):
ttk.Label(self.control_frame, text="Shape: ").grid(row=0, column=0, padx=5)
self.shape_menu = ttk.Combobox(self.control_frame, values=["dot", "line", "rectangle", "oval"])
self.shape_menu.grid(row=0, column=1, padx=5)
self.shape_menu.current(0)
self.shape_menu.bind("<<ComboboxSelected>>", self.change_shape)
ttk.Label(self.control_frame, text="Line Type: ").grid(row=0, column=2, padx=5)
self.line_menu = ttk.Combobox(self.control_frame, values=["-", "--", ":", "-."])
self.line_menu.grid(row=0, column=3, padx=5)
self.line_menu.current(0)
self.line_menu.bind("<<ComboboxSelected>>", self.change_line_type)
ttk.Label(self.control_frame, text="Color: ").grid(row=0, column=4, padx=5)
self.color_button = ttk.Button(self.control_frame, text="Choose Color", command=self.choose_color)
self.color_button.grid(row=0, column=5, padx=5)
self.save_button = ttk.Button(self.control_frame, text="Save", command=self.save_image)
self.save_button.grid(row=0, column=6, padx=5)
self.clear_button = ttk.Button(self.control_frame, text="Clear", command=self.clear_canvas)
self.clear_button.grid(row=0, column=7, padx=5)
self.exit_button = ttk.Button(self.control_frame, text="Exit", command=self.root.quit)
self.exit_button.grid(row=0, column=8, padx=5)
def change_shape(self, event):
self.current_shape = self.shape_menu.get()
def change_line_type(self, event):
self.current_line_type = self.line_menu.get()
def choose_color(self):
color_code = colorchooser.askcolor(title="Choose color")
if color_code:
self.current_color = color_code[1]
def start_draw(self, event):
self.start_x, self.start_y = event.x, event.y
if self.current_shape == "dot":
self.current_obj = self.canvas.create_oval(self.start_x, self.start_y, event.x + 1, event.y + 1,
fill=self.current_color, outline=self.current_color)
def draw(self, event):
if self.current_shape == "line":
if self.current_obj:
self.canvas.delete(self.current_obj)
self.current_obj = self.canvas.create_line(self.start_x, self.start_y, event.x, event.y,
fill=self.current_color,
dash=self.line_type_to_dash(self.current_line_type))
elif self.current_shape == "rectangle":
if self.current_obj:
self.canvas.delete(self.current_obj)
self.current_obj = self.canvas.create_rectangle(self.start_x, self.start_y, event.x, event.y,
outline=self.current_color,
dash=self.line_type_to_dash(self.current_line_type))
elif self.current_shape == "oval":
if self.current_obj:
self.canvas.delete(self.current_obj)
self.current_obj = self.canvas.create_oval(self.start_x, self.start_y, event.x, event.y,
outline=self.current_color,
dash=self.line_type_to_dash(self.current_line_type))
def end_draw(self, event):
self.current_obj = None
def line_type_to_dash(self, line_type):
if line_type == "--":
return (5, 2)
elif line_type == ":":
return (2, 2)
elif line_type == "-.":
return (5, 2, 2, 2)
else:
return None
def save_image(self):
file_path = filedialog.asksaveasfilename(defaultextension=".png",
filetypes=[("PNG files", "*.png"),
("All files", "*.*")])
if file_path:
x = self.root.winfo_rootx() + self.canvas.winfo_x()
y = self.root.winfo_rooty() + self.canvas.winfo_y()
x1 = x + self.canvas.winfo_width()
y1 = y + self.canvas.winfo_height()
ImageGrab.grab().crop((x, y, x1, y1)).save(file_path)
def clear_canvas(self):
self.canvas.delete("all")
if __name__ == "__main__":
root = tk.Tk()
app = DrawApp(root)
root.mainloop()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Python代码使用Tkinter库实现了一个交互式的图形用户界面(GUI),用于绘制简单的图形,如点线、直线、矩形和椭圆。用户可以通过鼠标在界面上绘制这些图形。主要功能如下: 图形选择:可以通过下拉菜单选择要绘制的图形(点线、直线、矩形和椭圆)。 线型选择:可以选择不同的线型(实线、虚线、点线、虚点线)。 颜色选择:可以通过颜色选择器选择绘图颜色。 保存功能:可以将绘制的图形保存为图像文件。 清除画布:可以清除画布上的所有图形。 退出程序:可以退出程序。
资源推荐
资源详情
资源评论
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 9 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/806f08fbc8b340bc9a603244936f1032_m0_51530640.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
AICurator
- 粉丝: 6071
- 资源: 101
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)