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()


AICurator
- 粉丝: 1w+
最新资源
- (建议下载)超算在云计算网络中的部署方案.pdf
- (完整版)通信原理自测题和答案解析.doc
- (建议下载)单片机教案(第6章存储器的扩展).pdf
- (完整版)高中物理知识点总结和知识网络图(大全).pdf
- (完整版)高中数学必修三算法知识点总结-推荐文档.pdf
- (完整版)高中数学第一章基本初等函数的导数公式及导数的运算法则(一)练习(可编辑修改word版).pdf
- (完整版)高中数学必修3算法习题(含答案).pdf
- (完整版)高中数学必修三算法知识点总结.pdf
- (建议下载)动态规划算法原理与的应用.pdf
- (完整版)运筹学第九章网络计划胡运权.ppt
- (建议下载)各种网络安全设备巡检报告.pdf
- (建议下载)高中化学知识结构网络图.pdf
- (完整版)青岛科技大学信息工程VB期末考试题库及答案.pdf
- (建议下载)基于MATLAB的模拟通信系统的设计.pdf
- (建议下载)基于PLC在污水处理厂中的控制系统设计.pdf
- (建议下载)基于单片机的压力超限报警系统设计.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


