發表文章

目前顯示的是 5月, 2023的文章

林稊葦python time.sleep()自建函數畫線

圖片
from tkinter import * #從函式庫 tkinter 輸入所有 * 方法 import math #從函式庫 math 輸入所有 * 方法 from time 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): #類別的方法 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(0.1) #休息1秒 tk....

林稊葦python自訂函數built-in內建函數math程式庫

圖片
#import math #林稊葦輸入math函式庫 from math import * #從數學函式庫math輸入所有函式 from tkinter import * #從tkinter視窗函式庫輸入所有函式 def f(r): #定義函數def 函數名(參數): print("圓周率"+str(pi))#字串 + str(數字) print("圓面積 "+str(pi*r*r)) print("圓周長 "+str(pi*r*2)) print("球表面積"+str(pi*r*r*4)) print("球體積 "+str(pi*r*r*4/3)) 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呼叫內建函數") print("數學pi, sin, cos以下式主程式\n") h(1,pi/6) #30度=pi/6, 180度=pi window = Tk() #建構視窗 window.title("林稊葦建構tkinter套件視窗") can = Canvas(window,width=600,height=400).pack() lab1 = Label(window, text="輸入半徑",font=('標楷體',30)).pack(side=LEFT) window.mainloop()

林稊葦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.app...

林稊葦Python視窗GUI自訂方法canvas.delete('all')

圖片
截圖 程式碼 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], fill="Pink",width=5) canvas.create_line(x[k-1], y[k-1], x[0], y[0], fill="Pink",width=5) #可考慮增加width寬度,fill顏色 def diagonal(): cx, cy, cr = 500, 210, 150 #宣告圓中心座標cx,...

林稊葦微軟vscode編輯python圖形視窗介面GUI

圖片
期中考複習 #林稊葦集合{},字典{key:value,},元組(),清單或陣列[]' p = ("台積電", "鴻海", "聯發科") r = {"台積電", "鴻海", "聯發科"} s = ["台積電", "鴻海", "聯發科"] t = ["中華電", "台塑化", "台達電"] d= {2330:"台積電",2317:"鴻海",2454:"聯發科"} u = s v = s.copy() s.extend(t) #原來的 s=s.extend(t)是錯誤 s.append("林稊葦") print(u) print(v) print(len(d)) i = 0 for a in s : #然後嘗試取代p為s(產生六個),r(產生三個) i = i+1 print("台灣第" + str(i) + "大的公司是") print(" " + a) w3schools截圖練習append 239 244 from tkinter import * #從函式庫 tkinter 輸入所有 * 方法 from math import * #從函式庫 math 輸入所有 * 方法 t = (3,4,5,6,7,8,9,10,11,12,16,20,32) #宣告一元組tuple(...) tk = Tk() tk.title("林稊葦圖形使用者介面GUI=Graphical Unser Interface") canvas = Canvas(tk, width=500, height=500) canvas.pack() def show(event): #定義由事件event(按鈕選單)呼叫的函數show cx, cy, cr = 210, 210, 200 ...

林稊葦python陣列array

圖片
w3schools截圖n w3schools程式碼 #林稊葦陣列 #for x in 'Takming gold': # 第一个实例 # print("當前字母: %s" % x) fruits = ['台積電', '鴻海', '聯發科'] for x in fruits: # 第二个实例 print ("最大公司水果: %s" % x) print ("練習陣列的11個內建方法") fruits.append("中華電") #append加元素 fruits.clear() fruits = ['台積電', '鴻海', '聯發科', '中華電'] print(fruits) pig = fruits.copy() print("輸出pig=fruits.copy()結果" + str(pig)) cat = fruits print("輸出cat=fruits的結果"+str(cat)) fruits.extend(fruits) #extend尾巴+陣列, append加元素 print("extend的 "+str(fruits)) print("copy 的 "+str(pig)) print(" = 的 "+str(cat)) print(fruits.count("台積電")) fruits.insert(1,"富邦金") print(fruits) print(fruits.index("富邦金")) fruits.reverse() print(fruits) fruits.sort() print(fruits) fruits.remove("富邦金") #拿走富邦金 print(fruits) fruits.pop(2) #拿走012的2 print(fruits) #體會到只要我有耐心與興趣,我也可以當一個專業的...

林稊葦python集合set元組tuple清單或串列list迴圈輸出str,int

圖片
w3schools截圖n w3schools程式碼 #林稊葦 集合{},清單[],元組(),字典{key:value} s = {"台積電", "鴻海", "聯發科", "中華電", "台塑化"} t = ("台積電", "鴻海", "聯發科", "中華電", "台塑化") list = ["台積電", "鴻海", "聯發科"] d = {2330:"台積電", 2317:"鴻海", 2454:"聯發科"} print("s型態" + str(type(s))) print("t型態" + str(type(t))) print("d型態" + str(type(d))) print("t型態" + str(type(list))) #字串與字串+ i = 0 for a in t: i = i + 1 print("台灣市場價格第" + str(i) + "大公司是") print(" " + a) print("聯發科的位置" + str(t.index("聯發科"))) '''python大區塊的註解,前後用三個引號 str 轉成字串 int 轉成數字 w3schools元組tuples的方法 Method Description count() Returns the number of times a specified value occurs in a tuple index() Searches the tuple for a specified value and returns the position of where it was found w3schools...

林稊葦python字典dictionaries

圖片
w3schools截圖n w3schools程式碼 #林稊葦 字典 keys:value市場價值最大的公司 a = { 2330: "台積電", 2317: "鴻海", 2454: "聯發科", 2412: "中華電", 6505:"台塑化",2308: "台達電"} print(a) print("迴圈列出字典的所有值") for t in a: print(a[t]) b = a.copy() #字典不能直接assign print(b) a.update({2881:"富邦金控"})#台灣第七大 a.update({2303:"聯電"}) #台灣大八大 for t in a: print(a[t]) a.setdefault(1303,"南亞") a.setdefault(2882,"國泰金") i = 0 for t in a: #python迴圈不使用{...}縮排整齊整齊 i = i + 1 print("台灣第" + str(i) + a[t]) w3schools字典方法列表 Method Description clear() 移除字典內容Removes all the elements from the dictionary copy() 拷貝Returns a copy of the dictionary fromkeys() Returns a dictionary with the specified keys and value get() 取得鍵Returns the value of the specified key items() Returns a list containing a tuple for each key value pair keys() Returns a list containing the dictionary's keys pop() 移除Removes the element with the...