依照计算机自带的画图软件的功能编写一个有可视化界面的python画图软件
时间: 2023-11-22 10:56:13 浏览: 112
好的,我可以为你提供一个简单的Python Tkinter GUI画图工具。下面是代码:
```python
import tkinter as tk
from tkinter import ttk
from tkinter import colorchooser
from tkinter import messagebox
from tkinter import filedialog
class Paint:
def __init__(self, root):
self.root = root
self.root.title("Paint")
self.root.geometry("800x600")
self.pen_color = "black"
self.bg_color = "white"
self.pen_width = 3
self.create_widgets()
self.create_menu()
def create_widgets(self):
self.canvas = tk.Canvas(self.root, bg=self.bg_color, width=800, height=500)
self.canvas.pack(fill=tk.BOTH, expand=True)
self.pen_color_button = tk.Button(self.root, text="Pen Color", command=self.choose_pen_color)
self.pen_color_button.pack(side=tk.LEFT, padx=5)
self.erase_button = tk.Button(self.root, text="Erase", command=self.erase)
self.erase_button.pack(side=tk.LEFT, padx=5)
self.pen_width_label = tk.Label(self.root, text="Pen Width:")
self.pen_width_label.pack(side=tk.LEFT, padx=5)
self.pen_width_entry = ttk.Combobox(self.root, values=["1", "2", "3", "4", "5"])
self.pen_width_entry.current(2)
self.pen_width_entry.pack(side=tk.LEFT)
self.clear_button = tk.Button(self.root, text="Clear", command=self.clear)
self.clear_button.pack(side=tk.LEFT, padx=5)
self.save_button = tk.Button(self.root, text="Save", command=self.save)
self.save_button.pack(side=tk.LEFT, padx=5)
def create_menu(self):
self.menu_bar = tk.Menu(self.root)
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="New", command=self.clear)
self.file_menu.add_command(label="Open", command=self.open_file)
self.file_menu.add_command(label="Save", command=self.save)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.root.quit)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
self.root.config(menu=self.menu_bar)
def choose_pen_color(self):
color = colorchooser.askcolor()
if color:
self.pen_color = color[1]
def erase(self):
self.pen_color = self.bg_color
def clear(self):
self.canvas.delete("all")
def save(self):
file_path = filedialog.asksaveasfilename(defaultextension=".png")
if file_path:
self.canvas.postscript(file=file_path + ".eps")
messagebox.showinfo("Save", "Image saved successfully!")
def open_file(self):
file_path = filedialog.askopenfilename(filetypes=[("EPS Files", "*.eps")])
if file_path:
self.clear()
self.canvas.create_image(400, 250, image=tk.PhotoImage(file=file_path))
def draw(self, event):
x1, y1 = (event.x - self.pen_width), (event.y - self.pen_width)
x2, y2 = (event.x + self.pen_width), (event.y + self.pen_width)
self.canvas.create_oval(x1, y1, x2, y2, fill=self.pen_color, outline=self.pen_color)
def start_draw(self, event):
self.canvas.bind("<B1-Motion>", self.draw)
def stop_draw(self, event):
self.canvas.unbind("<B1-Motion>")
def run(self):
self.canvas.bind("<Button-1>", self.start_draw)
self.canvas.bind("<ButtonRelease-1>", self.stop_draw)
self.root.mainloop()
if __name__ == '__main__':
paint = Paint(tk.Tk())
paint.run()
```
该程序实现了以下功能:
1. 画布
2. 选择画笔颜色
3. 橡皮擦
4. 选择画笔宽度
5. 清空画布
6. 保存画布
7. 打开画布
8. 实现了菜单栏和快捷键
你可以在本地运行这个程序并进行测试。
阅读全文