劉任昌類別,必須有 兩底線init兩底線的起始initiate函數

w3schools練習class

class Person:  #建立類別,必須有 兩底線init兩底線的起始initiate函數
  def __init__(self, name, age):
    self.name = name
    self.age = age
  def myfunc(self):
    print("靠!我的")
    print("名字是" + self.name)
    print("靠!我的\n名字是" + self.name) #字串中\n換列

p1 = Person("劉任昌", 36)
p1.myfunc()
print("印出p1.name: " + p1.name)

w3schools練習class截圖

VS code練習class Stars

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
import math    #從函式庫 math 輸入所有 * 方法
from time import *
from random import *
class Regular:
    def __init__(self, cx, cy, cr, s, t, c, w): #類別共同的設定必然 def __init__ initiate發起
        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 * math.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*math.cos(i*self.u)) 
            self.y.append(self.cy + self.cr*math.sin(i*self.u)) 
    def drawLine(self, x0, y0, x1, y1):
        canvas.create_line(x0, y0, x1, y1, width = self.w, fill=self.c)
    def draw(self):                                 #類別的方法
        secondTime = second.get()             #取得輸入的second變數,當作區域變數secondTime
        for i in range( int(self.s * 1.5) - self.t):
            self.drawLine(self.x[i], self.y[i], self.x[i + self.t], self.y[i + self.t])
            sleep(secondTime)   #休息
            tk.update()
def show():          #畫圖 define自訂函數
    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')
def showAll():
    for i in range(len(polyList)):
        polyList[i].draw()
def showRemove():
    removeId = remove.get()    #取得輸入的remove變數,當作區域變數removeId
    polyList.pop(removeId - 1)
    clear()
    showAll()
def begin():
    label_0 = Label(tk, text="劉德華輸入時間:",font=('標楷體',16)).pack(side=TOP, anchor=NW)
    entry_0 = Entry(tk,textvariable = second, font=('微軟中黑體',16)).pack(side=TOP, anchor=NW)
    label_1 = Label(tk, text="要移除第幾個?",font=('標楷體',16)).pack(side=TOP, anchor=NW)
    entry_1 = Entry(tk,textvariable = remove, font=('微軟中黑體',16)).pack(side=TOP, anchor=NW)
    button3 = Button(tk, text="移除後的更新", font=('微軟中黑體',16),command = showRemove, bg='red', fg='white').pack(side=TOP, anchor=NW)

polyList = []
xyr = (50,75,100,150,200,250,300,350,400,500,600)
st = (1,2,3,4,5,6,7,8,9,10,11,12,16,20,24,28,32)
tk = Tk()
tk.title("劉任昌視窗使用者介面GUI")  #也可以定義視窗名為 window, root課本都如此習慣
second, remove = DoubleVar(tk), IntVar(tk)
second.set(0.5)
remove.set(1)
begin()
canvas = Canvas(tk, width=1000, 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即凸多邊形

l1 = Label(tk, text="位置x ",font=('微軟中黑體',14)).pack(side=LEFT)  #距離左側
option1 = OptionMenu(tk, cx, *xyr).pack(side=LEFT)
l2 = Label(tk, text="位置y ",font=('微軟中黑體',14)).pack(side=LEFT)  #距離頂端
option2 = OptionMenu(tk, cy, *xyr).pack(side=LEFT)
l3 = Label(tk, text="半徑r ",font=('微軟中黑體',14)).pack(side=LEFT)  #半徑
option3 = OptionMenu(tk, cr, *xyr).pack(side=LEFT)
label4 = Label(tk, text="邊形s ",font=('微軟中黑體',14)).pack(side=LEFT)  #幾個邊
option4 = OptionMenu(tk, s, *st).pack(side=LEFT)
label5 = Label(tk, text="堅度t ",font=('微軟中黑體',14)).pack(side=LEFT)  #相鄰建構,尖銳度
option5 = OptionMenu(tk, t, *st).pack(side=LEFT)
label6 = Label(tk, text="顏色",font=('微軟中黑體',14)).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="寬度",font=('微軟中黑體',14)).pack(side=LEFT)  #寬度
w = IntVar(tk)
widthL = (1, 2, 3, 4, 5, 6)
w.set(widthL[3])
option7 = OptionMenu(tk, w, *widthL).pack(side=LEFT)
button = Button(tk, text=" 繪圖 ", command = show, bg='black',fg='white',font=('微軟中黑體',14)).pack(side=LEFT)
button1 = Button(tk, text="移除All", command = clear,font=('微軟中黑體',14)).pack(side=LEFT)
button2 = Button(tk, text="復原All", command = showAll, bg='red', fg='white',font=('微軟中黑體',14)).pack(side=LEFT)

tk.mainloop()

276

277

留言

  1. https://liiuzhewei.blogspot.com/2023/06/blog-post.html

    回覆刪除
  2. https://trequiem.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  3. https://d11150110.blogspot.com/2023/06/class-person-def-initself-name-age-self.html

    回覆刪除
  4. https://changmasaxciksdj.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  5. https://jasperyang.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  6. https://enchenghu0905.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  7. 作者已經移除這則留言。

    回覆刪除
  8. https://unnnn20.blogspot.com/2023/06/initinitiate.html

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

    回覆刪除
  10. https://wxxx17.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  11. https://yuzcc28.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  12. https://spencer0309.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  13. https://cindy-javascript.blogspot.com/2023/06/initinitate.html

    回覆刪除
  14. https://liiuzhewei.blogspot.com/2023/06/initinitiate.html

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

    回覆刪除
  16. https://changmasaxciksdj.blogspot.com/2023/06/initinitiate_4.html

    回覆刪除
  17. https://yaun3415.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  18. https://d11150108.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  19. 作者已經移除這則留言。

    回覆刪除
  20. https://d11150112.blogspot.com/2023/06/blog-post.html

    回覆刪除
  21. https://cpc920508123.blogspot.com/2023/06/vs-code-from-tkinter-import-tkinter.html

    回覆刪除
  22. https://iuytjhg5261.blogspot.com/2023/06/initinitate.html

    回覆刪除
  23. 作者已經移除這則留言。

    回覆刪除
  24. https://zongde824.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  25. https://d11150115.blogspot.com/2023/06/initinitiate-6-04-2023.html

    回覆刪除
  26. https://17richprince.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  27. https://yaun3415.blogspot.com/2023/06/initinitiate_4.html

    回覆刪除
  28. https://d10917257.blogspot.com/2023/06/blog-post.html

    回覆刪除
  29. https://d11150114.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  30. https://tgyhjikolujik2554.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  31. https://643gjeytfru4312.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  32. https://efnmklfnmk.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  33. https://linpeiyud11117127.blogspot.com/2023/06/initinitiate.html

    回覆刪除
  34. https://hsiehyuche.blogspot.com/2023/06/class-initself.html

    回覆刪除
  35. https://jennysuwanting.blogspot.com/2023/06/class-initself.html

    回覆刪除
  36. https://sususu0123.blogspot.com/2023/06/preborder-2px-red-solidh1background.html

    回覆刪除
  37. https://d11150116.blogspot.com/2023/06/initinitiate.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

劉任昌對照Python與JavaScript指令

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

劉任昌python, input, str, float