概述
雖然Python的強項在人工智能,數據處理方面,但是對于日常簡單的應用,Python也提供了非常友好的支持(如:Tkinter),本文主要一個簡單的畫圖小軟件,簡述Python在GUI(圖形用戶界面)方面的應用,僅供學習分享使用,如有不足之處,還請指正。
設計思路
- 頁面布局:主要分為上下兩部分 a. 繪圖區域,本例以Canvas實現 b. 下部:功能區,由按鈕實現
- 事件監聽:通過給功能按鈕綁定事件,來實現不同的功能,如:繪線,繪矩形等功能。
- 繪圖區域:監聽鼠標左鍵的按下(開始繪圖)和抬起(停止繪圖),再根據不同的按鈕實現繪制不同的圖形。
涉及知識點
開發工具:Python3.7 , PyCharm2019
Tkinter 是 Python 的標準 GUI 庫。Python 使用 Tkinter 可以快速的創建 GUI 應用程序。
Canvas控件提供了一個自定義的繪圖區域,可以通過不同的函數來繪制不同的圖形。
繪制直線 create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)
繪制帶箭頭的直線 create_line(self.x, self.y, event.x, event.y, arrow=LAST, fill=self.fgcolor)
繪制矩形 create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)
繪制曲線,是通過繪制不同的點來實現的
清除圖形 drawpad.delete('all')
Button 按鈕控件,通過綁定(bind)不同的監聽事件來實現不同的功能。
name屬性設置按鈕的名稱,
text屬性設置按鈕的顯示文本。
bind 綁定事件
示例效果圖
本例主要實現繪制直線,帶箭頭的直線,曲線,矩形,清除等功能,如下所示:
核心代碼
在本例中,主要功能如下:
創建畫板
1
2
3
|
"""創建畫圖區域""" self .drawpad = Canvas( self , width = win_width, height = win_height, bg = bgcolor) self .drawpad.pack() |
創建按鈕
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 創建按鈕 self .btn_start = Button( self , name = 'start' , text = '開始' ) self .btn_start.pack(side = 'left' , padx = 10 ) self .btn_pen = Button( self , name = 'pen' , text = '畫筆' ) self .btn_pen.pack(side = 'left' , padx = 10 ) self .btn_rect = Button( self , name = 'rect' , text = '矩形' ) self .btn_rect.pack(side = 'left' , padx = 10 ) self .btn_clear = Button( self , name = 'clear' , text = '清屏' ) self .btn_clear.pack(side = 'left' , padx = 10 ) self .btn_erasor = Button( self , name = 'erasor' , text = '橡皮擦' ) self .btn_erasor.pack(side = 'left' , padx = 10 ) self .btn_line = Button( self , name = 'line' , text = '直線' ) self .btn_line.pack(side = 'left' , padx = 10 ) self .btn_line_arrow = Button( self , name = 'line_arrow' , text = '箭頭直線' ) self .btn_line_arrow.pack(side = 'left' , padx = 10 ) self .btn_color = Button( self , name = 'color' , text = '顏色' ) self .btn_color.pack(side = 'left' , padx = 10 ) |
綁定事件
1
2
3
4
5
6
7
8
|
# 綁定事件 self .btn_line.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_line_arrow.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_rect.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_pen.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_erasor.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_clear.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 self .btn_color.bind( '<Button-1>' , self .eventManager) # 點擊按鈕事件 |
功能實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
def eventManager( self , event): name = event.widget.winfo_name() print (name) self .start_flag = True if name = = 'line' : # 左鍵拖動 self .drawpad.bind( '<B1-Motion>' , self .myline) elif name = = 'line_arrow' : self .drawpad.bind( '<B1-Motion>' , self .myline_arrow) elif name = = 'rect' : self .drawpad.bind( '<B1-Motion>' , self .myrect) elif name = = 'pen' : self .drawpad.bind( '<B1-Motion>' , self .mypen) elif name = = 'erasor' : self .drawpad.bind( '<B1-Motion>' , self .myerasor) elif name = = 'clear' : self .drawpad.delete( 'all' ) elif name = = 'color' : c = askcolor(color = self .fgcolor, title = '請選擇顏色' ) print (c) # c的值 ((128.5, 255.99609375, 0.0), '#80ff00') self .fgcolor = c[ 1 ] def startDraw( self , event): self .drawpad.delete( self .lastdraw) if self .start_flag: self .start_flag = False self .x = event.x self .y = event.y def stopDraw( self , event): self .start_flag = True self .lastdraw = 0 def myline( self , event): self .startDraw(event) self .lastdraw = self .drawpad.create_line( self .x, self .y, event.x, event.y, fill = self .fgcolor) def myline_arrow( self , event): self .startDraw(event) self .lastdraw = self .drawpad.create_line( self .x, self .y, event.x, event.y, arrow = LAST, fill = self .fgcolor) def myrect( self , event): self .startDraw(event) self .lastdraw = self .drawpad.create_rectangle( self .x, self .y, event.x, event.y, outline = self .fgcolor) def mypen( self , event): self .startDraw(event) print ( 'self.x=' , self .x, ',self.y=' , self .y) self .drawpad.create_line( self .x, self .y, event.x, event.y, fill = self .fgcolor) self .x = event.x self .y = event.y def myerasor( self , event): self .startDraw(event) print ( 'self.x=' , self .x, ',self.y=' , self .y) self .drawpad.create_rectangle(event.x - 3 , event.y - 3 , event.x + 3 , event.y + 3 , fill = bgcolor) self .x = event.x self .y = event.y |
快捷鍵的實現
1
2
3
4
5
|
self .master.bind( '<KeyPress-r>' , self .hotKey) # 綁定快捷鍵 self .master.bind( '<KeyPress-g>' , self .hotKey) # 綁定快捷鍵 self .master.bind( '<KeyPress-b>' , self .hotKey) # 綁定快捷鍵 self .master.bind( '<KeyPress-y>' , self .hotKey) # 綁定快捷鍵 self .drawpad.bind( '<ButtonRelease-1>' , self .stopDraw) # 左鍵釋放按鈕 |
快捷鍵功能實現
1
2
3
4
5
6
7
8
9
10
|
def hotKey( self , event): c = event.char if c = = 'r' : self .fgcolor = 'red' elif c = = 'g' : self .fgcolor = 'green' elif c = = 'b' : self .fgcolor = 'blue' elif c = = 'y' : self .fgcolor = 'yellow' |
有需要的朋友,可點擊鏈接下載整體代碼,如下所示:
備注
不積跬步,無以至千里;不積小流,無以成江海;鍥而舍之,朽木不折,鍥而不舍,金石可鏤。
到此這篇關于 Python實現畫圖軟件功能方法詳解的文章就介紹到這了,更多相關 Python實現畫圖軟件功能內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/hsiang/p/13381284.html