今天进行了Python大作业可视化界面设计器的部分编写,以下代码为画图的功能,以及调节笔刷大小的功能
import tkinter as tk from tkinter import filedialog, messagebox, colorchooser, simpledialog class LoginWindow: def __init__(self, root): self.root = root self.root.title("Login") self.root.geometry("300x150") self.root.config(bg="#f0f0f0") self.username_label = tk.Label(self.root, text="Username:", bg="#f0f0f0") self.username_label.pack(pady=5) self.username_entry = tk.Entry(self.root) self.username_entry.pack(pady=5) self.password_label = tk.Label(self.root, text="Password:", bg="#f0f0f0") self.password_label.pack(pady=5) self.password_entry = tk.Entry(self.root, show="*") self.password_entry.pack(pady=5) self.login_button = tk.Button(self.root, text="Login", command=self.login) self.login_button.pack(pady=5) def login(self): username = self.username_entry.get() password = self.password_entry.get() if username == "123456" and password == "123456": self.root.destroy() app = GUIEditor(tk.Tk()) else: messagebox.showerror("Error", "Invalid username or password") class GUIEditor: def __init__(self, root): self.root = root self.root.title("GUI Editor") self.root.geometry("600x500") self.root.config(bg="#f0f0f0") self.canvas = tk.Canvas(self.root, bg="white", width=400, height=400, bd=2, relief=tk.SOLID) self.canvas.pack(pady=20) self.shape = None self.start_x = None self.start_y = None self.draw_type = tk.StringVar() self.draw_type.set("rectangle") self.color = "black" self.brush_size = tk.IntVar() self.brush_size.set(2) self.eraser_size = tk.IntVar() self.eraser_size.set(2) toolbar_frame = tk.Frame(self.root, bg="#f0f0f0") toolbar_frame.pack(fill=tk.X, padx=10, pady=10) self.rectangle_button = tk.Radiobutton(toolbar_frame, text="Rectangle", variable=self.draw_type, value="rectangle", bg="#f0f0f0", command=self.set_draw_type) self.rectangle_button.pack(side=tk.LEFT, padx=10) self.circle_button = tk.Radiobutton(toolbar_frame, text="Circle", variable=self.draw_type, value="circle", bg="#f0f0f0", command=self.set_draw_type) self.circle_button.pack(side=tk.LEFT, padx=10) self.triangle_button = tk.Radiobutton(toolbar_frame, text="Triangle", variable=self.draw_type, value="triangle", bg="#f0f0f0", command=self.set_draw_type) self.triangle_button.pack(side=tk.LEFT, padx=10) self.line_button = tk.Radiobutton(toolbar_frame, text="Line", variable=self.draw_type, value="line", bg="#f0f0f0", command=self.set_draw_type) self.line_button.pack(side=tk.LEFT, padx=10) self.brush_button = tk.Radiobutton(toolbar_frame, text="Brush", variable=self.draw_type, value="brush", bg="#f0f0f0", command=self.set_draw_type) self.brush_button.pack(side=tk.LEFT, padx=10) self.eraser_button = tk.Radiobutton(toolbar_frame, text="Eraser", variable=self.draw_type, value="eraser", bg="#f0f0f0", command=self.set_draw_type) self.eraser_button.pack(side=tk.LEFT, padx=10) self.color_button = tk.Button(toolbar_frame, text="Color", command=self.choose_color) self.color_button.pack(side=tk.LEFT, padx=10) self.brush_size_label = tk.Label(toolbar_frame, text="Brush Size:", bg="#f0f0f0") self.brush_size_label.pack(side=tk.LEFT, padx=10) self.brush_size_scale = tk.Scale(toolbar_frame, from_=1, to=10, orient=tk.HORIZONTAL, variable=self.brush_size, bg="#f0f0f0", command=self.set_brush_size) self.brush_size_scale.pack(side=tk.LEFT, padx=5) self.eraser_size_label = tk.Label(toolbar_frame, text="Eraser Size:", bg="#f0f0f0") self.eraser_size_label.pack(side=tk.LEFT, padx=10) self.eraser_size_scale = tk.Scale(toolbar_frame, from_=1, to=10, orient=tk.HORIZONTAL, variable=self.eraser_size, bg="#f0f0f0", command=self.set_eraser_size) self.eraser_size_scale.pack(side=tk.LEFT, padx=5) self.canvas.bind("<Button-1>", self.on_click) self.canvas.bind("<B1-Motion>", self.on_drag) self.canvas.bind("<ButtonRelease-1>", self.on_release) self.menu = tk.Menu(self.root) self.root.config(menu=self.menu) self.file_menu = tk.Menu(self.menu, tearoff=False) self.menu.add_cascade(label="File", menu=self.file_menu) self.file_menu.add_command(label="Save", command=self.save_as_python) def on_click(self, event): self.start_x = event.x self.start_y = event.y def on_drag(self, event): if self.draw_type.get() == "brush": self.canvas.create_line(self.start_x, self.start_y, event.x, event.y, fill=self.color, width=self.brush_size.get()) self.start_x = event.x self.start_y = event.y elif self.draw_type.get() == "eraser": self.canvas.create_line(self.start_x, self.start_y, event.x, event.y, fill="white", width=self.eraser_size.get()) elif self.draw_type.get() in ["rectangle", "circle", "line"]: if self.shape: self.canvas.delete(self.shape) if self.draw_type.get() == "rectangle": self.shape = self.canvas.create_rectangle(self.start_x, self.start_y, event.x, event.y, outline=self.color, width=self.brush_size.get()) elif self.draw_type.get() == "circle": self.shape = self.canvas.create_oval(self.start_x, self.start_y, event.x, event.y, outline=self.color, width=self.brush_size.get()) elif self.draw_type.get() == "line": self.shape = self.canvas.create_line(self.start_x, self.start_y, event.x, event.y, fill=self.color, width=self.brush_size.get()) elif self.draw_type.get() == "triangle": if self.shape: self.canvas.delete(self.shape) self.shape = self.canvas.create_polygon(self.start_x, self.start_y, event.x, event.y, self.start_x - (event.x - self.start_x), event.y, outline=self.color, width=self.brush_size.get()) def on_release(self, event): self.shape = None def set_draw_type(self): if self.draw_type.get() in ["rectangle", "circle", "triangle"]: self.canvas.config(cursor="tcross") else: self.canvas.config(cursor="arrow") def choose_color(self): color = colorchooser.askcolor(title="Choose color") if color[1]: self.color = color[1] def set_brush_size(self, size): pass def set_eraser_size(self, size):
标签:draw,root,self,学习,6.6,进度,type,tk,size From: https://www.cnblogs.com/lijianlongCode13/p/18247309