鱼C论坛

 找回密码
 立即注册
查看: 3272|回复: 14

[作品展示] 一个能锻炼你想象力的画板程序

[复制链接]
发表于 2022-4-3 09:30:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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

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


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

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

  8.         #调整页面大小的变量

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

  11.         
  12.         self.width = self.start_width #页面的宽(变)
  13.         self.height = self.start_height #页面的高(变)
  14.         
  15.         self.max_width = max_width #底部滑块最大的移动限制
  16.         self.max_height = max_height #右边滑块最大的移动限制
  17.         
  18.         self.min_width = min_width #底部滑块最小的移动限制
  19.         self.min_height = min_height #右边滑块最小的移动限制

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

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

  24.         

  25.         #调整画笔粗细的变量

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

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

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


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


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

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

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




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

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



  49.     def get_color(self): #获取颜色

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

  51.         self.check_answer_color()

  52.         self.color = self.answer[1]
  53.         


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

  55.         self.get_color()

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



  60.     def new(self): #更新

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

  62.         if self.stay: #保留设置

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

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

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


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

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

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

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



  78.             else: #保留画布内容

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

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



  83.             #保留画笔粗细
  84.             self.style = self.new_style.get() #更新粗细
  85.                     
  86.                     
  87.         else:
  88.                
  89.             #恢复页面长宽
  90.             self.width = self.start_width #恢复页面默认的宽
  91.             self.height = self.start_height #恢复页面默认的长

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

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

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

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

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

  105.             try:
  106.                 self.color
  107.             except:
  108.                 self.have_color = False

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

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

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

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


  120.     def clean_bord(self): #清空画板

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

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

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

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

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

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



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

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

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

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


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

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

  150.         


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

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

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

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

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

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

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

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

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

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

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


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



  185.     def draw(self,event):

  186.         try:
  187.             self.color
  188.         except: #如果还没有选过颜色
  189.             tkinter.messagebox.showwarning("错误!","请先选择颜色后再进行绘画")
  190.             return -1 #终止程序
  191.         
  192.         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)



  193.     def main(self):         

  194.         self.root_frame() #初始化

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




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

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

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

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

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

  208. start.main()



复制代码



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

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

评分

参与人数 4荣誉 +9 鱼币 +8 收起 理由
yayc_zcyd + 2 + 2 鱼C有你更精彩^_^
shiyouroc + 1 鱼C有你更精彩^_^
小伤口 + 5 + 5 感谢楼主无私奉献!
ckblt + 1 + 1 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-3 13:40:00 | 显示全部楼层
还没人评论
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-4-3 14:29:02 | 显示全部楼层

好像画布的大小无法改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-3 14:46:01 | 显示全部楼层
myqf123 发表于 2022-4-3 14:29
好像画布的大小无法改变

可以呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-3 15:44:10 | 显示全部楼层
厉害!期待后续作品

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
python爱好者. + 5 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-4-3 16:07:42 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-6 11:54:29 | 显示全部楼层
建议贴几张图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-6 11:57:52 | 显示全部楼层

谢谢建议
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-4 17:50:11 | 显示全部楼层
帅!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-29 22:14:18 | 显示全部楼层
厉害了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-25 22:33:29 | 显示全部楼层
我一开始以为你会用turtle

点评

我很赞同!: 1.0 不,我坚决不同意楼主的看法!: 5.0
我很赞同!: 1 不,我坚决不同意楼主的看法!: 5
turtle只适用于绘制简单图形,至于复杂任务,还得交给专业的pygame等  发表于 2023-12-25 22:51

评分

参与人数 1荣誉 +4 鱼币 +4 贡献 +3 收起 理由
python爱好者. + 4 + 4 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2023-12-26 10:19:31 | 显示全部楼层
厉害~~~

评分

参与人数 1贡献 +3 收起 理由
python爱好者. + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
python爱好者. + 5 + 5 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

这不是bug,是特性,tkinter的缺陷
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表