劉任昌Python類別class函數function

VS Code截圖

VS Code程式碼

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
from math import *    #從函式庫 math 輸入所有 * 方法
class Regular:
    def __init__(self, cx, cy, cr, s, t, c, w): #類別共同的設定
        self.cx, self.cy, self.cr = cx, cy, cr  #取得中心座標cx, cy, 半徑cr
        self.s, self.t = s, t    #取得邊角數目s,t尖銳程度,取代原來的k = s.get()
        self.c, self.w = c, w    #取得顏色c,寬度w
        self.u = 2 * pi / self.s #使用模組 math 圓周率 pi
        self.x, self.y = [], []
        for i in range( int(self.s * 1.5)):
            self.x.append(self.cx + self.cr*cos(i*self.u)) 
            self.y.append(self.cy + self.cr*sin(i*self.u)) 
    def draw(self):                                 #類別的方法
        for i in range( int(self.s * 1.5) - self.t):
            canvas.create_line(self.x[i], self.y[i], 
                    self.x[i + self.t], self.y[i + self.t], fill = self.c, width = self.w)
def show():          #畫圖方法
    poly = Regular(cx.get(), cy.get(), cr.get(), s.get(), t.get(), c.get(), w.get())
    polyList.append(poly)
    polyList[len(polyList)-1].draw()
def clear():         #清除視窗的all所有canvas圖
    canvas.delete('all')

polyList = []
xyr = (50,80,100,150,200,250,300,350,400)
st = (1,2,3,4,5,6,7,8,9,10,11,12,16,20,24,28,32)
tk = Tk()                            #建構法Tk建構一個視窗
tk.title("劉任昌視窗使用者介面GUI")  #也可以定義視窗名為 window, root課本都如此習慣
canvas = Canvas(tk, width=800, height=450) 
canvas.pack()
cx, cy, cr, s, t = IntVar(tk),IntVar(tk),IntVar(tk),IntVar(tk),IntVar(tk)
cx.set(xyr[3]) #預設座標 x=200
cy.set(xyr[3]) #預設座標 y=200
cr.set(xyr[1]) #預設半徑 r=100
s.set(st[9])   #預設邊形 8
t.set(st[0])   #預設堅度 1即凸多邊形
label0 = Label(tk, text="劉任昌",bg='purple',fg='white').pack(side=LEFT)
label1 = Label(tk, text="位置x ").pack(side=LEFT)  #距離左側
option1 = OptionMenu(tk, cx, *xyr).pack(side=LEFT)
label2 = Label(tk, text="位置y ").pack(side=LEFT)  #距離頂端
option2 = OptionMenu(tk, cy, *xyr).pack(side=LEFT)
label3 = Label(tk, text="半徑r ").pack(side=LEFT)  #半徑
option3 = OptionMenu(tk, cr, *xyr).pack(side=LEFT)
label4 = Label(tk, text="邊形s ").pack(side=LEFT)  #幾個邊
option4 = OptionMenu(tk, s, *st).pack(side=LEFT)
label5 = Label(tk, text="堅度t ").pack(side=LEFT)  #相鄰建構,尖銳度
option5 = OptionMenu(tk, t, *st).pack(side=LEFT)
label6 = Label(tk, text="顏色").pack(side=LEFT)    #顏色
c = StringVar(tk)
colorL = ('black','red', 'green', 'blue', 'purple', 'gray')
c.set(colorL[0])
option6 = OptionMenu(tk, c, *colorL).pack(side=LEFT)
label7 = Label(tk, text="寬度").pack(side=LEFT)  #寬度
w = IntVar(tk)
widthL = (1, 2, 3, 4, 5, 6)
w.set(widthL[0])
option7 = OptionMenu(tk, w, *widthL).pack(side=LEFT)
button = Button(tk, text=" 繪圖 ", command = show, bg='black',fg='white').pack(side=LEFT)
button1 = Button(tk, text="移除All", command = clear).pack(side=LEFT)
tk.mainloop()

W3Schools學習自訂函數

影片252與254

留言

  1. https://enchenghu0905.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  2. https://cpc920508123.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  3. https://d11150110.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  4. https://jasperyang.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  5. https://www.blogger.com/blog/post/edit/preview/3284037173979173764/2160160463817763573

    回覆刪除
  6. https://liiuzhewei.blogspot.com/2023/05/pythonclassfunciton.html

    回覆刪除
    回覆
    1. 優秀 確實自己做 但截圖太大 不清楚

      刪除
  7. https://d11150114.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  8. https://www.blogger.com/blog/post/edit/preview/8530990375848508323/4295485035977321063

    回覆刪除
    回覆
    1. 確實自己做 但截圖範圍太大 不清楚

      刪除
  9. https://wutimothy.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  10. https://d11150115.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  11. https://pandora-mo.blogspot.com/2023/05/blog-post.html

    回覆刪除
  12. https://trequiem.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  13. https://tgyhjikolujik2554.blogspot.com/2023/05/vscodepython-tkinterbuttonlable.html

    回覆刪除
  14. https://cindy-javascript.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  15. https://iuytjhg5261.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  16. https://spencer0309.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  17. https://d11150108.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  18. https://d11150112.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  19. https://d10917257.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
    回覆
    1. 很好 自己做 但你的網頁指令有問題 導致不能留言

      刪除
  20. https://jjjjjyyy.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  21. https://chenyouzheng0329.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
    回覆
    1. 自己做 很好 但截圖範圍要縮小 才看得清楚

      刪除
  22. https://643gjeytfru4312.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  23. https://fongcyf.blogspot.com/2023/05/pythonclassfunc.html

    回覆刪除
  24. https://html-css-javascript-yaoto.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
    回覆
    1. 陳映年超級優秀 但程式碼要使用pre

      刪除
  25. https://michael-d11117126.blogspot.com/2023/05/pythonclassfunction.html

    回覆刪除
  26. https://html-css-javascript-javadd.blogspot.com/2023/05/58-vs-vs-code.html

    回覆刪除
    回覆
    1. 很好 自己的做 下次截圖小一點 才看得清楚

      刪除

張貼留言

這個網誌中的熱門文章

劉任昌對照Python與JavaScript指令

劉任昌Python集合set清單list元組tuple字典dictionary

劉任昌python, input, str, float