Skip to content

Instantly share code, notes, and snippets.

@renyuanL
Created July 10, 2014 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renyuanL/89106c0e4899544d0ad8 to your computer and use it in GitHub Desktop.
Save renyuanL/89106c0e4899544d0ad8 to your computer and use it in GitHub Desktop.
ryBonusArtApp2.py
'''
ryBonusArtApp2.py
ry美術應用.py
呂仁園, 2014/07/10
'''
# 改自 BonusArtApp2.py
import tkinter
import random
隨機整數= random.randint
#
# 中文別名
#
窗類= 視窗類= tkinter.Tk
圖類= 圖像類= tkinter.PhotoImage
布類= 畫布類= tkinter.Canvas
框類= 畫框類= tkinter.Frame
鈕類= 按鈕類= tkinter.Button
全部= tkinter.ALL
左邊= tkinter.LEFT
#
# 全域 (global) 變數
#
布寬= 600
布高= 400
布色= "black"
座標x= 布寬/2
座標y= 布高
線色= "red"
線寬= 5
線長= 5
此線= None
此圖= None
#
# 啟動一個 窗,
# 從此就可使用其他相關的元件
#
窗= 窗類()
窗.title("美術應用")
#
# 準備好一些 美美 的 圖檔,在此為一些 *.gif 檔案
#
背景= 圖類(file= "treescape.gif")
紅線= 圖類(file= "red.gif")
藍線= 圖類(file= "blue.gif")
綠線= 圖類(file= "green.gif")
黃線= 圖類(file= "yellow.gif")
黑線= 圖類(file= "black.gif")
白線= 圖類(file= "white.gif")
加號= 圖類(file= "plus.gif")
減號= 圖類(file= "minus.gif")
小樹= 圖類(file= "tree.gif")
小康= 圖類(file= "campbell.gif")
小艾= 圖類(file= "ayesha.gif")
小傑= 圖類(file= "jeff.gif")
小可= 圖類(file= "krysta.gif")
小漢= 圖類(file= "hana.gif")
小荷= 圖類(file= "jose.gif")
小莉= 圖類(file= "leela.gif")
#
# 設計一些 函數 (動作)
#
def 選紅線():
global 線色
global 此圖
此圖 = None
線色 = "red"
def 選綠線():
global 線色
global 此圖
此圖 = None
線色 = "green"
def 選藍線():
global 線色
global 此圖
此圖 = None
線色 = "blue"
def 選黃線():
global 線色
global 此圖
此圖 = None
線色 = "yellow"
def 選黑線():
global 線色
global 此圖
此圖 = None
線色 = "black"
def 選白線():
global 線色
global 此圖
此圖 = None
線色 = "white"
def 減小():
global 線寬
global 線長
if (線寬 > 5):
線寬 -= 5
線長 -= 5
def 加大():
global 線寬
global 線長
if (線寬 <= 50):
線寬 += 5
線長 += 5
def 選小樹():
選人(小樹)
def 選小康():
選人(小康)
def 選小艾():
選人(小艾)
def 選小傑():
選人(小傑)
def 選小可():
選人(小可)
def 選小漢():
選人(小漢)
def 選小荷():
選人(小荷)
def 選小莉():
選人(小莉)
def 畫(e):
'''
這個 e 會夾帶 滑鼠座標 .x, .y 傳進來
'''
if (此圖 != None):
布.create_image(e.x, e.y, image= 此圖)
else:
寬= 線寬/2
x1= e.x - 寬
y1= e.y - 寬
x2= e.x + 寬
y2= e.y + 寬
布.create_oval(x1, y1, x2, y2, fill= 線色)
def 選人(img):
global 此圖
此圖= img
def 製造群眾():
n=0
while n < 100:
選擇= 隨機整數(1, 7)
x= 隨機整數 ( 0, 布寬)
y= 隨機整數 ( 0, 布高)
if 選擇 == 1:
布.create_image(x, y, image= 小艾)
elif 選擇 == 2:
布.create_image(x, y, image= 小康)
elif 選擇 == 3:
布.create_image(x, y, image= 小傑)
elif 選擇 == 4:
布.create_image(x, y, image= 小可)
elif 選擇 == 5:
布.create_image(x, y, image= 小漢)
elif 選擇 == 6:
布.create_image(x, y, image= 小荷)
else:
布.create_image(x, y, image= 小莉)
n += 1
def 擦掉全部():
布.delete(全部)
布.create_image(布寬/2, 布高/2, image= 背景)
#
# 開始造各種元件,包含:
#
# 窗、布、框、鈕
#
布= 布類(bg= 布色, height= 布高, width= 布寬)
布.create_image(布寬/2, 布高/2, image= 背景)
布.bind("<B1-Motion>", 畫)
布.bind("<Button-1>", 畫)
上框= 框類(窗)
中框= 框類(窗)
下框= 框類(窗)
鈕群= []
鈕= 鈕類(上框, image= 紅線, command= 選紅線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 綠線, command= 選綠線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 藍線, command= 選藍線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 黃線, command= 選黃線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 黑線, command= 選黑線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 白線, command= 選白線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 加號, command= 加大); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 減號, command= 減小); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小康, command= 選小康); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小艾, command= 選小艾); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小傑, command= 選小傑); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小可, command= 選小可); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小漢, command= 選小漢); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小荷, command= 選小荷); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小莉, command= 選小莉); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小樹, command= 選小樹); 鈕群 += [鈕]
鈕= 鈕類(下框, text= '製造群眾', command= 製造群眾); 鈕群 += [鈕]
鈕= 鈕類(下框, text= '擦掉全部', command= 擦掉全部); 鈕群 += [鈕]
#
# 把所有元件包裝安置起來,各就各位。
#
布.pack()
上框.pack()
中框.pack()
下框.pack()
for 鈕 in 鈕群:
鈕.pack(side= 左邊)
#
# 一切準備就緒,讓程式進入 主迴圈。
#
窗.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment