python爱好者. 发表于 2022-4-3 09:30:26

一个能锻炼你想象力的画板程序

本帖最后由 python爱好者. 于 2022-4-3 09:39 编辑

小缺陷:
1.布局不是很好看
2.不能保留 "两者" —— 说明页面有
3.没有撤回功能(好像也不选缺陷)

注意事项:
1.使用说明在页面打开后会出现

废话不多说,上代码:
import tkinter as tk
import tkinter.colorchooser
import tkinter.messagebox


class Start:
    def __init__(self,start_width,start_height,style,max_width,max_height,min_width,min_height,min_style,max_style):

      #起始页面
      self.root = tk.Tk()

      #调整页面大小的变量

      self.start_width = start_width #页面默认的宽(不变)
      self.start_height = start_height #页面默认的高(不变)

      
      self.width = self.start_width #页面的宽(变)
      self.height = self.start_height #页面的高(变)
      
      self.max_width = max_width #底部滑块最大的移动限制
      self.max_height = max_height #右边滑块最大的移动限制
      
      self.min_width = min_width #底部滑块最小的移动限制
      self.min_height = min_height #右边滑块最小的移动限制

      self.new_width = tk.IntVar() #记录 调整页面长的滑块 的变量
      self.new_height = tk.IntVar() #记录 调整页面宽的滑块 的变量

      self.new_width.set(self.min_width) #初始化 调整页面长的滑块 位置
      self.new_height.set(self.min_height) #初始化 调整页面宽的滑块 位置

      

      #调整画笔粗细的变量

      self.start_style = style #画笔默认粗细(不变)
      
      self.style = self.start_style #画笔粗细(变)
      
      self.min_style = min_style #粗细滑块最小的移动限制
      self.max_style = max_style #粗细滑块最大的移动限制

      self.new_style = tk.IntVar() #记录 调整画笔粗细的滑块 的变量

      #其他
      self.the_color_square_frame_width = 50 #设置 颜色方块边长 的变量


    def make_bord(self,row,column): #制作画板
      
      self.the_bord = tk.Canvas(self.the_bord_frame,width = self.width - 50,height = self.height - 50)
      self.the_bord.grid(row = row,column = column)
            
      self.the_bord.focus_set()


    def make_the_color_square(self): #制作对应颜色的方块

      self.color_square = tk.Canvas(self.the_color_square_frame,width = self.the_color_square_frame_width,height = self.the_color_square_frame_width)
      self.color_square.grid(row = 3,column = 1)

      self.color_square.create_rectangle(0,0,self.the_color_square_frame_width,self.the_color_square_frame_width,fill = self.color)




    def check_answer_color(self): #检查颜色是否合法

      while self.answer == None:
            tk.messagebox.showwarning("错误!","请选择正确的颜色!")
            self.answer = tkinter.colorchooser.askcolor(title = "请选择颜色")



    def get_color(self): #获取颜色

      self.answer = tkinter.colorchooser.askcolor(title = "请选择颜色")

      self.check_answer_color()

      self.color = self.answer
      


    def divide_color(self): #获取颜色并显示

      self.get_color()

      self.the_color_label = tk.Label(self.root,text = "当前颜色:")
      self.the_color_label.grid(row = 3,column = 0,sticky = tk.W)
      
      self.make_the_color_square()



    def new(self): #更新

      self.stay = tkinter.messagebox.askyesno("提示","是否要将现在的设置在更新后仍然保留?") #是否保留设置

      if self.stay: #保留设置

            self.select = tkinter.messagebox.askyesno("提示","应程序原因,\n此操作不能兼得保留\n您新定义的画布大小和画布内容\n保留画布内容?") #保留大小?

            if not self.select: #保留画布大小

                self.width = self.new_width.get() #更新长
                self.height = self.new_height.get() #更新宽


                #删除旧画布框架
                self.the_bord_frame.destroy()

                #删除旧画布
                self.the_bord.destroy()

                  
                #重绘画布框架
                self.the_bord_frame = tk.Frame(self.root,width = self.width - 50,height = self.height - 50) #重绘画布框架
                self.the_bord_frame.grid(row = 1,column = 0,sticky = tk.W)

                #重绘画布
                self.make_bord(1,0) #重绘画布
                self.the_bord.bind("<B1-Motion>",self.draw) #检测是否开始绘画



            else: #保留画布内容

                #恢复调整页面长的滑块
                self.new_width.set(self.min_width) #恢复 调整页面长的滑块 位置

                #恢复调整页面宽的滑块
                self.new_height.set(self.min_height) #恢复 调整页面宽的滑块 位置



            #保留画笔粗细
            self.style = self.new_style.get() #更新粗细
                  
                  
      else:
               
            #恢复页面长宽
            self.width = self.start_width #恢复页面默认的宽
            self.height = self.start_height #恢复页面默认的长

            #删除旧画布框架
            self.the_bord_frame.destroy()

            #删除旧画布
            self.the_bord.destroy()

            
            #重绘画布框架
            self.the_bord_frame = tk.Frame(self.root,width = self.width - 50,height = self.height - 50) #重绘画布框架
            self.the_bord_frame.grid(row = 1,column = 0,sticky = tk.W)

            #重绘画布
            self.make_bord(1,0) #重绘画布
            self.the_bord.bind("<B1-Motion>",self.draw) #检测是否开始绘画

            #删除当前选中的颜色
            self.have_color = True #记录变量 color 是否存在

            try:
                self.color
            except:
                self.have_color = False

            if self.have_color: #如果选过颜色
                self.the_color_label.destroy() #删除文本
                  
                self.color_square.destroy() #删除色块

            #恢复画笔粗细
            self.style = self.start_style #恢复画笔粗细
            self.new_style.set(self.style)

            #恢复调整页面长的滑块
            self.new_width.set(self.min_width) #恢复 调整页面长的滑块 位置

            #恢复调整页面宽的滑块
            self.new_height.set(self.min_height) #恢复 调整页面宽的滑块 位置


    def clean_bord(self): #清空画板

      self.the_bord.delete(tk.ALL) #删除画板全部内容
               

    def fill_the_how_root(self): #往使用说明的页面中添加内容

      self.theScroll = tk.Scrollbar(self.the_how_root) #滚动条
      self.theScroll.pack(side = tk.RIGHT,fill = tk.Y)

      self.theText = tk.Text(self.the_how_root,yscrollcommand = self.theScroll.set) #文本
      self.theText.pack(fill = tk.BOTH)

      for each_line in self.the_word.split("\n"):
            self.theText.insert(tk.END,f"{each_line}\n")

      self.theScroll.config(command = self.theText.yview)



    def pop_how_to_use(self): #弹出使用说明

      self.space_lines = 2 #设置每条规则之间的空行

      self.space_lines = self.space_lines * "\n" #创造出对应的空格行数

      self.the_word = f"""1.画图前先选择颜色{self.space_lines}
2.画笔的颜色是可以实时更新使用的,但画笔粗细和画布大小是无法实时更新的,必须使用更新按钮{self.space_lines}
3.关于使用更新:
    1.若不想保留当前画板上和页面上的设置,可以直接点否
    2.若想保留当前的设置:
      1.点击是,代表您放弃保存您新设置的画板大小(如果您不想更新画板大小,选这个)
      2.点击否,代表您放弃保存您当前在画板上画的内容(如果您想更新画板大小,选这个)
      3.除画板大小的设置和画板内容的设置无法同时保留外:
          选择的颜色设置,画笔粗细设置可以保留{self.space_lines}
4.调节画布大小可以使用最下方的滑块和最右方的滑块{self.space_lines}
5.使用退出按钮需单独点击程序文件运行,在文件内运行是没有效果的{self.space_lines}
6.待更新"""


      self.the_how_root = tk.Toplevel() #新创建一个窗口
      self.the_how_root.title("使用说明")

      self.fill_the_how_root() #添加文本内容
      

      


    def root_frame(self): #整体框架
      
      #标题
      tk.Label(self.root,text = "请快乐地作图吧!").grid(row = 0,column = 0,sticky = tk.W)

      #画板
      self.the_bord_frame = tk.Frame(self.root,width = self.width - 50,height = self.height - 50)
      self.the_bord_frame.grid(row = 1,column = 0)
      
      self.make_bord(1,0)

      #选择颜色提示
      tk.Button(self.root,text = "选择颜色",command = self.divide_color).grid(row = 2,column = 0,sticky = tk.W)

      #颜色方块的框架
      self.the_color_square_frame = tk.Frame(self.root,width = self.the_color_square_frame_width,height = self.the_color_square_frame_width)
      self.the_color_square_frame.grid(row = 3,column = 1,sticky = tk.W)

      #画笔粗细
      tk.Label(self.root,text = "调整粗细").grid(row = 4,column = 0,sticky = tk.W)

      #调整画笔粗细的滑块
      tk.Scale(self.root,from_ = self.min_style,to = self.max_style,orient = tk.HORIZONTAL,showvalue = False,length = self.width,variable = self.new_style)\
                            .grid(row = 5,column = 0,sticky = tk.W)

      #更新
      tk.Button(self.root,text = "更新",command = self.new).grid(row = 6,column = 0,sticky = tk.W)

      #退出
      tk.Button(self.root,text = "退出",command = self.root.quit).grid(row = 7,column = 0,sticky = tk.W)

      #控制页面长的滑块
      tk.Scale(self.root,from_ = self.min_width,to = self.max_width,orient = tk.HORIZONTAL,showvalue = False,length = self.width,variable = self.new_width)\
                            .grid(row = 8,column = 0,sticky = tk.W)

      #使用说明
      tk.Button(self.root,text = "使用说明",command = self.pop_how_to_use).grid(row = 0,column = 1,sticky = tk.W)
      

      #一键清空按钮
      tk.Button(self.root,text = "一\n键\n清\n空",command = self.clean_bord).grid(row = 0,rowspan = 3,column = 2,sticky = tk.W)


      #控制页面宽的滑块
      tk.Scale(self.root,from_ = self.min_height,to = self.max_height,showvalue = False,length = self.height,variable = self.new_height)\
                            .grid(row = 0,rowspan = 3,column = 4,sticky = tk.N)



    def draw(self,event):

      try:
            self.color
      except: #如果还没有选过颜色
            tkinter.messagebox.showwarning("错误!","请先选择颜色后再进行绘画")
            return -1 #终止程序
      
      self.the_bord.create_oval(event.x - 1,event.y - 1,event.x + 1,event.y + 1,fill = self.color,outline = self.color,width = self.style)



    def main(self):         

      self.root_frame() #初始化

      self.the_bord.bind("<B1-Motion>",self.draw) #检测是否开始绘画
      
      tk.mainloop()




start_width = 400 #设置默认起始页面的宽
start_height = 400 #设置默认起始页面的高
style = 1 #设置默认的画笔粗细

max_width = 1100 #设置底部滑块最大的移动限制
max_height = 700 #设置右边滑块最大的移动限制

min_width = 100 #设置底部滑块最小的移动限制
min_height = 100 #设置右边滑块最小的移动限制

min_style = 1 #设置粗细滑块最小的移动限制
max_style = 100 #设置粗细滑块最大的移动限制

start = Start(start_width,start_height,style,max_width,max_height,min_width,min_height,min_style,max_style)

start.main()






竟然都学完 Tkinter 了,那离更新《一个能帮你背单词的程序》 还会远吗......{:10_256:} {:10_298:}
@ckblt @小伤口 @shiyouroc

注:
1.代码已优化,本来是打算前天发布的,后来想想代码太乱了,不适合后期更新和你们看,就打包成类了!{:10_297:}

python爱好者. 发表于 2022-4-3 13:40:00

还没人评论{:10_245:}

myqf123 发表于 2022-4-3 14:29:02

{:10_275:}
好像画布的大小无法改变

python爱好者. 发表于 2022-4-3 14:46:01

myqf123 发表于 2022-4-3 14:29
好像画布的大小无法改变

可以呀

小伤口 发表于 2022-4-3 15:44:10

厉害!期待后续作品{:9_236:}

hornwong 发表于 2022-4-3 16:07:42

{:5_95:}

shiyouroc 发表于 2022-4-6 11:54:29

建议贴几张图

python爱好者. 发表于 2022-4-6 11:57:52

shiyouroc 发表于 2022-4-6 11:54
建议贴几张图

谢谢建议

元豪 发表于 2022-9-4 17:50:11

帅!!!!!!{:10_256:}

张维宁ZWN 发表于 2022-10-29 22:14:18

厉害了

stumblemodel 发表于 2023-12-25 22:33:29

我一开始以为你会用turtle

sucreshin 发表于 2023-12-26 10:19:31

厉害~~~

stumblemodel 发表于 2023-12-27 22:02:35

为什么不在画板上同时执行999个相同的关于点的作画程序,这样就可以消除BUG

python爱好者. 发表于 2023-12-27 22:44:41

stumblemodel 发表于 2023-12-27 22:02
为什么不在画板上同时执行999个相同的关于点的作画程序,这样就可以消除BUG

这不是bug,是特性,tkinter的缺陷
页: [1]
查看完整版本: 一个能锻炼你想象力的画板程序