1. 程式人生 > >python-Tkinter教程之Canvas篇

python-Tkinter教程之Canvas篇

https://blog.csdn.net/u011650048/article/details/13022019

python-Tkinter教程之Canvas篇(1)

2013年10月25日 16:15:26 做一個有魅力的程式設計師 閱讀數:4897 標籤: python canvas tkinter create_rectangle 更多

個人分類: python

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u011650048/article/details/13022019

'''Tkinter教程之Canvas篇(1)'''
# 提供可以用來進行繪圖的Container,支援基本的幾何元素,使用Canvas進行繪圖時,所有的操作都是通過Canvas,不是通過它的元素
# 元素的表示可以使用handle或tag。
'''1.

第一個Canvas程式'''
-*- coding: cp936 -*-
# 指定畫布的顏色為白色
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.pack()
root.mainloop()
# 為明顯起見,將背景色設定為白色,用以區別root

'''2.建立一個item'''
-*- coding:
 cp936 -*-
# 建立一個矩形,指定畫布的顏色為白色
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 建立一個矩形,座標為(10,10,110,110)
cv.create_rectangle(10,10,110,110)
cv.pack()
root.mainloop()
# 為明顯起見,將背景色設定為白色,用以區別root
'''3.
指定item的填充色'''
-*- coding: cp936 -*-
# 建立一個矩形,指定畫布的背景色為白色
# 使用屬性fill設定它的填充顏色
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,fill = 'red')
cv.pack()
root.mainloop()
# 指定矩形的填充色為紅色
'''4.指定item的邊框顏色'''
-*- coding: cp936 -*-
# 建立一個矩形,指定畫布的背景色為白色
# 使用屬性outline設定它的邊框顏色
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,outline = 'red')
cv.pack()
root.mainloop()
# 指定矩形的邊框顏色為紅色
'''5.指定邊框的寬度'''
-*- coding: cp936 -*-
# 指定畫布的背景色為白色
# 使用屬性width指定線的寬度
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,outline = 'red',width = 5)
cv.pack()
root.mainloop()
# 指定矩形的邊框顏色為紅色,設定線寬為5,注意與Canvas的width是不同的。
'''6.畫虛線'''
-*- coding: cp936 -*-
# 指定畫布的背景色為白色
# 使用屬性dash,這個值只能為奇數
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,
                    outline = 'red',
                    dash = 10,
                    fill = 'green')
cv.pack()
root.mainloop()
# 指定矩形的邊框顏色為紅色,畫虛線
'''7.使用畫刷填充'''
-*- coding: cp936 -*-
# 指定畫布的背景色為白色
# 使用屬性stipple
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,
                    outline = 'red',
                    stipple = 'gray12',
                    fill = 'green')
cv.pack()
root.mainloop()
# 指定矩形的邊框顏色為紅色,自定義畫刷
'''8.修改item的座標'''
-*- coding: cp936 -*-
# 指定畫布的背景色為白色
# 使用Canvas的方法來重新設定item的座標
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
rt = cv.create_rectangle(10,10,110,110,
                    outline = 'red',
                    stipple = 'gray12',
                    fill = 'green')
cv.pack()
# 重新設定rt的座標(相當於移動一個item)
cv.coords(rt,(40,40,80,80))
root.mainloop()
# 動態修改item的座標

 

python-Tkinter教程之Canvas篇(2)

2013年10月25日 16:16:53 做一個有魅力的程式設計師 閱讀數:2134

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u011650048/article/details/13022039

'''Tkinter教程之Canvas篇(2)'''
'''9.建立item的tags'''
-*- coding: cp936 -*-
# 使用屬性tags設定item的tag
# 使用Canvas的方法gettags獲取指定item的tags
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 使用tags指定一個tag('r1')
rt = cv.create_rectangle(10,10,110,110,
                         tags = 'r1'
                         )
cv.pack()

print cv.gettags(rt)
# 使用tags屬性指定多個tags,即重新設定tags的屬性
cv.itemconfig(rt,tags = ('r2','r3','r4'))
print cv.gettags(rt)
root.mainloop()
# 動態修改item的座標
'''10.多個item使用同一個tag'''
-*- coding: cp936 -*-
# 多個控制元件使用同一個tag
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 使用tags指定一個tag('r1')
rt = cv.create_rectangle(10,10,110,110,
                         tags = ('r1','r2','r3')
                         )
cv.pack()

cv.create_rectangle(20,20,80,80,tags = 'r3')
print cv.find_withtag('r3')
root.mainloop()
# 動態修改item的座標
#fid_withtag返回所有與tag繫結的item。
'''11.通過tag來訪問item'''
-*- coding: cp936 -*-
# 得到了tag值也就得到了這個item,可以對這個item進行相關的設定。
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 使用tags指定一個tag('r1')
rt = cv.create_rectangle(10,10,110,110,
                         tags = ('r1','r2','r3')
                         )
cv.pack()

cv.create_rectangle(20,20,80,80,tags = 'r3')
# 將所有與tag('r3')繫結的item邊框顏色設定為藍色
for item in cv.find_withtag('r3'):
    cv.itemconfig(item,outline = 'blue')
root.mainloop()
# 動態修改與tag('r3')繫結的item邊框顏色
'''13.向其它item新增tag'''
-*- coding: cp936 -*-
# 使用addtag_來向上一個或下一個item新增tag
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 建立三個rectangle
rt1 = cv.create_rectangle(
    10,10,110,110,
    tags = ('r1','r2','r3'))
rt2 = cv.create_rectangle(
    20,20,80,80,
    tags = ('s1','s2','s3'))
rt3 = cv.create_rectangle(
    30,30,70,70,
    tags = ('y1','y2','y3'))
# 向rt2的上一個item新增r4
cv.addtag_above('r4',rt2)
# 向rt2的下一個item新增r5
cv.addtag_below('r5',rt2)

for item in [rt1,rt2,rt3]:
    print cv.gettags(item)

cv.pack()
root.mainloop()
#Canvas使用了stack的技術,新建立的item總是位於前一個建立的item之上,故呼叫above時,它會查詢rt2上面的item為rt3,故rt3中添加了tag('r4'),同樣add_below會查詢下面的item。
'''14.返回其它item'''
-*- coding: cp936 -*-
# 使用find_xxx查詢上一個或下一個item
from Tkinter import *
root = Tk()
# 建立一個Canvas,設定其背景色為白色
cv = Canvas(root,bg = 'white')
# 建立三個rectangle
rt1 = cv.create_rectangle(
    10,10,110,110,
    tags = ('r1','r2','r3'))
rt2 = cv.create_rectangle(
    20,20,80,80,
    tags = ('s1','s2','s3'))
rt3 = cv.create_rectangle(
    30,30,70,70,
    tags = ('y1','y2','y3'))
# 查詢rt2的上一個item,並將其邊框顏色設定為紅色
cv.itemconfig(cv.find_above(rt2),outline = 'red')
# 查詢rt2的下一個item,並將其邊框顏色設定為綠色
cv.itemconfig(cv.find_below(rt2),outline = 'green')

cv.pack()
root.mainloop()
#Canvas使用了stack的技術,新建立的item總是位於前一個建立的item之上,故呼叫above時,它會查詢rt2上面的item為rt3,故rt3中邊框顏色設定為紅色,同樣add_below會查詢下面的item。

 

Tkinter教程之Canvas篇(3)

2007年10月05日 12:59:00 jcodeer 閱讀數:5617 標籤: tags import delete 測試 更多

個人分類: Python UI

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif'''Tkinter教程之Canvas篇(3)'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif'''16.移動item'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# move
指定x,y在偏移量
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立一個Canvas,設定其背景色為白色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv = Canvas(root,bg = 'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立兩個同樣的rectangle,比較移動前後的不同
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
移動rt1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.move(rt1,20,-10)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
# move可以指定x,y在相對偏移量,可以為負值
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
'''17.刪除item'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# delete
刪除給定的item
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv = Canvas(root,bg = 
'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立兩個rectangle
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifr2 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    20,20,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
's1','s2','s3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
使用id刪除rt1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.delete(rt1)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
使用tag刪除r2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.delete('s1')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
兩種方法刪除item(id/tag)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
'''18.縮放item'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# scale
縮放item,計算公式:(coords - offset)*scale + offset
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv = Canvas(root,bg = 
'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立兩個rectangle
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
將y座標放大為原來的2位,x座標值不變
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.scale(rt1,0,0,1,2)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
# scale的引數為(self,xoffset,yoffset,xscale,yscale)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
'''19.繫結item與event'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
使用tag_bind來繫結item與事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立一個Canvas,設定其背景色為白色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv = Canvas(root,bg = 'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立三個rectangle
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    width = 8,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
def printRect(event):
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
print 'rectangle'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif繫結item與事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.tag_bind('r1','<Button-1>',printRect)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
只有點選到矩形的邊框時才會觸發事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
'''20.新增繫結事件'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
使用tag_bind來繫結item與事件,與參考上測試結果不一致。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立一個Canvas,設定其背景色為白色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv = Canvas(root,bg = 'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立三個rectangle
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    width = 8,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
def printRect(event):
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
print 'rectangle'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifdef printLine(event):
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
print 'line'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif繫結item與左鍵事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.tag_bind('r1','<Button-1>',printRect)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
繫結item與右鍵事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.tag_bind('r1','<Button-3>',printLine)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
只有點選到矩形的邊框時才會觸發事件,不使用add引數,預設就是向這個item新增一個處理函式,它不會替換原來的事件函式,例子結果:既響應左鍵又響應右鍵
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
'''21.繫結新的item與現有的tags'''
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif# -*- coding: cp936 -*-
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
使用tag_bind來繫結item與事件,測試結果與參考上的說法不一致
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
from Tkinter import *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot = Tk()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立一個Canvas,設定其背景色為白色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv = Canvas(root,bg = 'white')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立三個rectangle
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
rt1 = cv.create_rectangle(
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    10,10,110,110,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    width = 8,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    tags = (
'r1','r2','r3'))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
def printRect(event):
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
print 'rectangle'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifdef printLine(event):
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
print 'line'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif繫結item與左鍵事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.tag_bind('r1','<Button-1>',printRect)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
繫結item與右鍵事件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.tag_bind('r1','<Button-3>',printLine)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
建立一個line,並將其tags設定為'r1'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
cv.create_line(10,200,100,200,width = 5,tags = 'r1')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcv.pack()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifroot.mainloop()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
將事件與tag('r1')繫結後,建立新的item並指定已繫結事件的tag,新建立的item同樣也與事件繫結,這個與參考上的說法也不一致