Created
July 30, 2014 03:14
我的蝕刻繪圖板02.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# myEtchASketch.py, 我的蝕刻繪圖板02.py | |
# a application from Coding Club: Python Basics | |
# by Chris Roffey, 翻譯、改作:呂仁園。 | |
# | |
from tkinter import * | |
##### 設定 全區變數 (global variables): | |
布高= 400 | |
布寬= 600 | |
布色= "black" | |
點x= 布寬/2 | |
點y= 布高 | |
點色= "green" | |
線長= 5 | |
線寬= 5 | |
##### 玩家控制 函數: | |
def 點移上(): | |
global 點y | |
布.create_line(點x, 點y, 點x, 點y-線長, | |
width= 線寬, | |
fill= 點色) | |
點y= 點y - 線長 | |
def 點移下(): | |
global 點y | |
布.create_line(點x, 點y, 點x, 點y+線長, | |
width= 線寬, | |
fill= 點色) | |
點y= 點y + 線長 | |
def 點移右(): | |
global 點x | |
布.create_line(點x, 點y, 點x+線長, 點y, | |
width= 線寬, | |
fill= 點色) | |
點x= 點x + 線長 | |
def 點移左(): | |
global 點x | |
布.create_line(點x, 點y, 點x-線長, 點y, | |
width= 線寬, | |
fill= 點色) | |
點x= 點x - 線長 | |
def 清除全部(e= None): | |
布.delete(ALL) | |
def 點隨鼠(x,y): | |
global 點x, 點y | |
點x, 點y= x,y | |
布.create_line(點x, 點y, 點x+線長, 點y+線長, | |
width= 線寬, | |
fill= 點色) | |
def 點改色(色= 'red'): | |
global 點色 | |
點色= 色 | |
def 印使用方法(e= None): | |
global 使用方法 | |
print('使用方法= \n', 使用方法) | |
布.create_text(0, 0, text= 使用方法, fill='white', anchor='nw') | |
def 處理按鍵(e): | |
k,n,c= e.keysym, hex(e.keysym_num), e.char | |
print(k,n,c) | |
if k == 'Up': 點移上() | |
elif k == 'Down': 點移下() | |
elif k == 'Left': 點移左() | |
elif k == 'Right': 點移右() | |
elif k == 'Delete': 清除全部() | |
elif k == 'r': 點改色('red') | |
elif k == 'g': 點改色('green') | |
elif k == 'b': 點改色('blue') | |
elif k == 'c': 點改色('cyan') | |
elif k == 'y': 點改色('yellow') | |
elif k == 'm': 點改色('magenta') | |
elif k == 'p': 點改色('pink') | |
elif k == 'o': 點改色('orange') | |
elif k == 'w': 點改色('white') | |
elif k == 'G': 點改色('grey') | |
elif k == 'B': 點改色('black') | |
elif k == 'h': 印使用方法() | |
else: pass | |
def 處理滑鼠(e): | |
x,y= e.x, e.y | |
print(x,y) | |
點隨鼠(x,y) | |
########## 主程式: | |
窗= Tk() | |
窗.title("我的蝕刻繪圖板") | |
布= Canvas( | |
bg= 布色, | |
height= 布高, | |
width= 布寬) | |
布.pack() | |
# | |
# 綁定(連結) 按鍵 滑鼠 與 處理函數 | |
# | |
窗.bind("<Key>", 處理按鍵) | |
窗.bind("<Button-1>", 處理滑鼠) | |
窗.bind("<B1-Motion>", 處理滑鼠) | |
窗.bind("<Button-3>", 清除全部) | |
窗.bind("<Double-Button-3>", 印使用方法) | |
# | |
# 使用方法 | |
# | |
import inspect as ip | |
使用方法= ip.getsource(處理按鍵) +'\n' | |
使用方法+= ip.getsource(處理滑鼠) +'\n' | |
#print('使用方法= \n', 使用方法) | |
印使用方法() | |
# | |
# 進入視窗應用程式的主迴圈 | |
# | |
窗.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment