Giới thiệu sản phẩm
im
def reset(event):
global old_x, old_y
old_x = old_y = None
def undo_action():
if canvas_actions:
canvas.delete(canvas_actions.pop())
def save_canvas():
filename = asksaveasfilename(defaultextension='.png', filetypes=[('PNG files', '*.png'), ('All files', '*.*')])
if filename:
x, y = canvas.winfo_rootx(), canvas.winfo_rooty()
x1, y1 = x + canvas.winfo_width(), y + canvas.winfo_height()
ImageGrab.grab().crop((x, y, x1, y1)).save(filename)
def open_image():
filename = askopenfilename(filetypes=[('Image files', '*.png;*.jpg;*.jpeg'), ('All files', '*.*')])
if filename:
img = Image.open(filename)
img.thumbnail((canvas.winfo_width(), canvas.winfo_height()))
img_tk = ImageTk.PhotoImage(img)
canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
canvas.image = img_tk # Avoid garbage collection
def exit_app():
if messagebox.askokcancel("Quit", "Do you want to exit?"):
window.quit()
def add_tooltips(widget, text):
tooltip = tk.Toplevel(window, bg='yellow', padx=5, pady=3)
tooltip.withdraw()
label = tk.Label(tooltip, text=text, bg='yellow')
label.pack()
def show_tooltip(event):
tooltip.geometry(f"+{event.x_root + 10}+{event.y_root + 10}")
tooltip.deiconify()
def hide_tooltip(_):
tooltip.withdraw()
widget.bind("<Enter>", show_tooltip)
widget.bind("<Leave>", hide_tooltip)
window = tk.Tk()
window.title('Paint Application')
window.geometry('800x600')
menu_bar = tk.Menu(window)
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Save", command=save_canvas)
file_menu.add_command(label="Open", command=open_image)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_app)
window.config(menu=menu_bar)
toolbar = tk.Frame(window, relief=tk.RIDGE, borderwidth=2)
toolbar.pack(side=tk.LEFT, fill=tk.Y)
pen_button = tk.Button(toolbar, text="Pen", command=use_pen)
pen_button.pack(pady=5)
brush_button = tk.Button(toolbar, text="Brush", command=use_brush)
brush_button.pack(pady=5)
eraser_button = tk.Button(toolbar, text="Eraser", command=use_eraser)
eraser_button.pack(pady=5)
color_button = tk.Button(toolbar, text="Color", command=choose_color)
color_button.pack(pady=5)
undo_button = tk.Button(toolbar, text="Undo", command=undo_action)
undo_button.pack(pady=5)
pen_size_label = tk.Label(toolbar, text="Pen Size")
pen_size_label.pack(pady=5)
choose_size_button = tk.Scale(toolbar, from_=1, to=10, orient=tk.VERTICAL)
choose_size_button.pack()
canvas = tk.Canvas(window, bg='white')
canvas.pack(fill=tk.BOTH, expand=True)
add_tooltips(pen_button, "Use the pen tool (P)")
add_tooltips(brush_button, "Use the brush tool (B)")
add_tooltips(eraser_button, "Use the eraser tool (E)")
add_tooltips(color_button, "Select a color")
add_tooltips(undo_button, "Undo the last action (Ctrl+Z)")
window.bind('<Control-z>', lambda _: undo_action())
window.bind('<p>', lambda _: use_pen())
window.bind('<b>', lambda _: use_brush())
window.bind('<e>', lambda _: use_eraser())
setup(canvas, choose_size_button)
window.mainloop()
window.mainloop()
Hình ảnh sản phẩm
Hãy bình luận để nhặt 100 thóc nhé
Sản phẩm cùng tác giả
Sản phẩm liên quan
Không có sản phẩm nào
Đăng nhập để tham gia bình luận