首页 > 其他分享 >6.6学习进度

6.6学习进度

时间:2024-06-14 10:32:32浏览次数:29  
标签:draw root self 学习 6.6 进度 type tk size

今天进行了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

相关文章

  • 【课程总结】Day8(上):深度学习基本流程
    前言在上一篇课程《【课程总结】Day7:深度学习概述》中,我们了解到:模型训练过程→本质上是固定w和b参数的过程;让模型更好→本质上就是让模型的损失值loss变小;让loss变小→本质上就是求loss函数的最小值;本篇文章,我们将继续深入了解深度学习的项目流程,包括:批量化打包数据、模......
  • STM学习记录(四)———中断及NVIC
    文章目录前言一、中断1.中断的定义2.中断的优先级3.中断嵌套二、STM32中断总结前言一个学习32单片机的小白~有问题评论或私信指出~提示:以下是本篇文章正文内容,下面案例可供参考一、中断1.中断的定义在主程序运行过程中,出现了特定的中断触发条件(中断源),使得CPU......
  • STM32学习记录(二)————基本GPIO控制
    文章目录前言1.GPIO基础知识2.IO八种工作模式3固件库实现LED点灯前言一个学习STM32的小白有问题评论区或私信指出~1.GPIO基础知识GPIO(General-Purposeinput/output,通用输入/输出接口)用于感知外部信号(输入模式)和控制外部设备(输出模式)简单模块:LED,按键,蜂......
  • 第四届物联网与机器学习国际学术会议(IoTML 2024)
    【ACM独立出版,高录用,见刊检索快速稳定】第四届物联网与机器学习国际学术会议(IoTML2024)【IoTML2023会后三个半月内完成EI检索】20244thInternationalConferenceonInternetofThingsandMachineLearning2024年8月9-11日,中国-南昌大会官网:https://ais.cn/u/qM7ruu【更......
  • 事件注册与事件代理学习
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title><......
  • C++学习笔记-----类和构造函数
    类和结构体class和struct的区别在于class默认是private的而struct默认是public的struct继承自另一个struct或class时,默认继承方式是public继承class继承自另一个class或struct时,默认继承方式是private继承class继承自struct,则默认继承方式还是public,以保持......
  • Spring系统学习 - Bean的作用域
    bean作用域介绍Spring框架提供了不同的作用域来管理Bean的生命周期和可见性,这对于控制不同类型的组件和处理并发请求尤其重要。singleton(默认):每个SpringIoC容器只有一个bean实例。当容器创建bean后,它会被缓存起来,后续请求将返回同一个实例。这是默认的作用域,适用于无状......
  • 白冠鸡算法改进的深度极限学习机DELM的分类
    白冠鸡算法改进的深度极限学习机DELM的分类文章目录白冠鸡算法改进的深度极限学习机DELM的分类1.ELM原理2.深度极限学习机(DELM)原理3.白冠鸡算法4.白冠鸡算法改进DELM5.实验结果6.参考文献7.Matlab代码1.ELM原理ELM基础原理请参考:https://blog.csdn.net/u011835903/......
  • 金枪鱼群算法改进的深度极限学习机DELM的分类
    金枪鱼群算法改进的深度极限学习机DELM的分类文章目录金枪鱼群算法改进的深度极限学习机DELM的分类1.ELM原理2.深度极限学习机(DELM)原理3.金枪鱼群算法4.金枪鱼群算法改进DELM5.实验结果6.参考文献7.Matlab代码1.ELM原理ELM基础原理请参考:https://blog.csdn.net/u01......
  • 运维必备Linux学习day4(Vmware磁盘扩容)
    虚拟机添加磁盘与linux逻辑卷扩容1.1 虚拟机添加磁盘1.1.1. 检查linux系统的磁盘分区及逻辑卷的相关信息是否正常(1)cat/etc/redhat-release查看系统版本【适用于RHEL及CentOS】:我已自己电脑上的虚拟机,50G内存2G运行内存为例>>>(2)df -h看挂载点空间使用率情况:(3)blkid查看......