2023年5月29日 星期一

連宸宏Python類別class函數function

VS Code多邊形編輯

VS Code程式碼

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
from math import *    #從函式庫 math 輸入所有 * 方法
import time           #連宸宏從函數庫
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 * 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)
            time.sleep(0.5)   #停留1秒
            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')

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課本都如此習慣
canvas = Canvas(tk, width=800, height=500)
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即凸多邊形
l0 = Label(tk, text="連宸宏", bg='yellow').pack(side=LEFT)  #距離左側
l1 = Label(tk, text="位置x ").pack(side=LEFT)  #距離左側
option1 = OptionMenu(tk, cx, *xyr).pack(side=LEFT)
l2 = Label(tk, text="位置y ").pack(side=LEFT)  #距離頂端
option2 = OptionMenu(tk, cy, *xyr).pack(side=LEFT)
l3 = 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()

2023年5月22日 星期一

連宸宏python, input, str, float

VS Code截圖

程式碼

from math import *
def abc(r): 
  print("劉任昌輸入的半徑 " + str(r))
  print("圓面積:  "+str(pi*r*r))
  print("圓周長:  "+str(pi*r*2))
  print("球體積:  "+str(pi*r*r*r*4/3))
  print("球表面積:"+str(pi*r*r*4))
def tri(z):
  print("劉任昌輸入的度 " + str(y))
  print("正弦sin "+str(sin(z)))
  print("餘弦cos "+str(cos(z)))
def group(r, t):
  abc(r)
  tri(t)
r = float(input("輸入半徑: "))
y = float(input("輸入角度360度單位: "))
t = y/180*pi
group(r,t)
<>

2023年5月15日 星期一

連宸宏python自訂函數built-in內建函數import輸入函式庫

from math import * #連宸宏從math函式庫輸入所有函數
#取代原來的import math
def f(r): #定義函數 define 名稱(參數),以下相同縮排都是
  print("圓面積pi r*r: "+str(pi*r*r))
  print("圓周長pi r*2: "+str(pi*r*2))
  print("球體積pi r*r*r*4/3:"+str(pi*r*r*r*4/3))
  print("球表面積pi r*r*4:  "+str(pi*r*r*4))
def g(angle):
  print("正弦sin:"+str(sin(angle)))
  print("餘弦cos:"+str(cos(angle)))
def h(x,y): #用在模組化你的程式碼
  f(x)
  g(y)
print("連宸宏:自訂函數h呼叫f,g再呼叫內建pi,sin,cos\n")
h(1,pi/6)

2023年5月8日 星期一

連宸宏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練習遞迴recursion函數

練習程式碼

def f(k):#連宸宏自訂函數遞迴recursion函數
  if(k > 0):
    result = k + f(k - 1) #函數呼叫自己
    print(result)
  else:
    result = 0
  return result

x = 10
print("反斜線n換列遞迴函數")
f(x)
print("使用公式 \n n(n+1)/2:")
print( x*(x+1)/2 )

2023年5月1日 星期一

連宸宏VSCode編輯Python,tkinter建構Button,Lable

微軟VS Code截圖

程式碼

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
# math只用三個沒必要輸入所有*, math.pi比 pi 更清楚
import math #連線去找函式庫
t = (3,4,5,6,7,8,9,10,11,12,16,20)#宣告一元組tuple(...)
tk = Tk()
tk.title("大衰哥視窗使用者介面GUI")
canvas = Canvas(tk, width=800, height=500)
canvas.pack()

def show(event):                        #定義由事件event(按鈕選單)呼叫的函數show
   cx = 200           #宣告圓中心座標cx, cy半徑cr
   cy = 210
   cr = 140
   x, y =[],[]                          #宣告二陣列[...]
   k = s.get()                          #取得 ge t按鈕選單的選擇變數
   u = 2 * math.pi / k                       #使用模組 math 圓周率 pi
   for i in range(k):
      x.append(cx + cr*math.cos(i*u))        #加入陣列的元素
      y.append(cy + cr*math.sin(i*u))        #使用模組 math 三角函數cos, sin
   for i in range(k-1):
      canvas.create_line(x[i], y[i], x[i+1], y[i+1])
   canvas.create_line(x[k-1], y[k-1], x[0], y[0], fill="blue",width=5)   #可考慮增加width寬度,fill顏色
def diagonal():
   cx, cy, cr = 500, 210, 150           #宣告圓中心座標cx, cy半徑cr
   x, y =[],[]                          #宣告二陣列[...]
   k = s.get()                          #取得 ge t按鈕選單的選擇變數
   u = 2 * pi / k                       #模組 math 圓周率 pi
   for i in range(k):
      x.append(cx + cr*cos(i*u)) #加入陣列的元素
      y.append(cy + cr*sin(i*u))
   for i in range(k):
      for j in range(i+2, k):
         canvas.create_line(x[i], y[i], x[j], y[j], fill="green", width=4)

def I_am_a_monkey():
    canvas.delete('all')         
s = IntVar(tk)
label1 = Label(tk,text="連宸宏製作視窗",bg='black',fg='white').pack(side=LEFT) #建構者OptionMenu, Button
combo = OptionMenu(tk, s, *t, command = show).pack(side=LEFT)        #下拉式選單menu
button = Button(tk, text="對角線", command = diagonal).pack(side=LEFT)#按鈕
button1 = Button(tk, text="刪除所有", command = I_am_a_monkey).pack(side=LEFT)#按鈕
label2 = Label(tk,text="我成功了!",bg='purple',fg='white').pack(side=LEFT) #建構者OptionMenu, Button
tk.mainloop()  #套裝軟體的重新整理無限次