2023年6月12日 星期一

連宸宏tkinter視窗函式庫Entry工具widget

程式碼

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 = []         #建構串列(清單,陣列)list,還是空的,名稱是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()             #井Number sign, hash建構視窗,名為tk
tk.title("蘇詩涵視窗使用者介面GUI")  #也可以定義視窗名為 window, root課本都如此習慣
second, remove = DoubleVar(tk), IntVar(tk)
second.set(0.1)       #預設值0.1秒
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=('微軟正黑體',16)).pack(side=LEFT)  #距離左側
entry1 = Entry(tk, textvariable = cx, width=4, font=('微軟中黑體',16)).pack(side=LEFT)
l2 = Label(tk, text="位置y ",font=('微軟正黑體',16)).pack(side=LEFT)  #距離頂端
option2 = OptionMenu(tk, cy, *xyr).pack(side=LEFT)
l3 = Label(tk, text="半徑r ",font=('微軟正黑體',16)).pack(side=LEFT)  #半徑
entry3 = Entry(tk,textvariable = cr, width=4, font=('微軟中黑體',16)).pack(side=LEFT) #半徑改成手動輸入,Entry 建構輸入方塊, 建構在視窗tk, 輸入的文字變數textbariable存成cr變數, 寬度10, font字型, 打包pack排列在tk視窗靠左
label4 = Label(tk, text="邊形s ",font=('標楷體',16)).pack(side=LEFT)  #幾個邊
entry4 = Entry(tk, textvariable = s, width=4, font=('微軟中黑體',16)).pack(side=LEFT)
label5 = Label(tk, text="堅度t ").pack(side=LEFT)  #相鄰建構,尖銳度
entry5 = Entry(tk, textvariable = t,width=4, font=('微軟中黑體',16)).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[3])
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)
button2 = Button(tk, text="復原All", command = showAll, bg='red', fg='white').pack(side=LEFT)

tk.mainloop()

2023年6月5日 星期一

連宸宏類別class __init__(self, 其他參數)

W3school截圖

VS code練習class截圖

VS Code練習CLASS程式碼

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=('微軟正黑體',16)).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[3])
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)
button2 = Button(tk, text="復原All", command = showAll, bg='red', fg='white').pack(side=LEFT)

tk.mainloop()

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()  #套裝軟體的重新整理無限次

2023年4月24日 星期一

連宸宏期中考VS Code編輯Python圖形使用者介面GUI

期中考

解說

題目

# 集合{},字典{key:value,},元組(),清單或陣列[]
p = ("台積電", "鴻海", "聯發科")
r = {"台積電", "鴻海", "聯發科"}
s = ["台積電", "鴻海", "聯發科"]
t = ["中華電", "台塑化", "台達電"]
d= {2330:"台積電",2317:"鴻海",2454:"聯發科"}
u = s 
v = s.copy()
s = s.extend(t)
print(u)
print(v)
print(len(d))
i = 0
for a in p: #然後嘗試取代p為r,s,t,u
  i = i+1
  print("台灣第" + str(i) + "大的公司是")
  print("    " + a)

w3schools

微軟VS Code編輯Python圖形使用者介面

Python程式碼

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
from math 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=500, height=500)
canvas.pack()

def show(event):                        #定義由事件event(按鈕選單)呼叫的函數show
   cx, cy, cr = 210, 210, 200           #宣告圓中心座標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))        #使用模組 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])   #可考慮增加width寬度,fill顏色
   
def diagonal():
   cx, cy, cr = 210, 210, 200           #宣告圓中心座標cx, cy半徑cr外來學繼承 inheritance
   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="blue", width=3) 
         
s = IntVar(tk)
combo = OptionMenu(tk, s, *t, command = show).pack()         #下拉式按鈕combobox
button = Button(tk, text="對角線", command = diagonal).pack()#按鈕button
tk.mainloop()

VS Code截圖Python

140影片

242影片

2023年4月10日 星期一

連宸宏python陣列array

W3schools陣列截圖

W3schools陣列程式碼

#連宸宏拷貝自 201單元
"""for x in 'Takming':     #迴圈逐字元印出
   print("字母: %s" % x)三引號框起註解
"""
fruits = ['台積電', '鴻海', '聯發科'] #台灣市場價值最高的三公司
for x in fruits:        # 
   print ("公司: %s" % x)
print(fruits)
fruits.append("中華電")
print("使用append")
print(fruits)
fruits.clear()
print("使用clear")
print(fruits)
fruits = ['台積電', '鴻海', '聯發科', '中華電']
chicken = fruits.copy()
#和 chicken = fruits 有何不同?
print(chicken)
fruits.append("中華電")
print(fruits.count("中華電"))
print(fruits.count("台積電"))
for x in fruits:
  print(x) 
  if x == "中華電": #判斷式是否banana
    print('I hate 中華電.')
  if x == "聯發科": #判斷式是否cherry
    print('I like cherry.')
  if x == "鴻海": #判斷式是否apple
    print('You are my sweet apple.')
#體會到只要我有耐心與興趣,我也可以當一個專業的程式開發人員
#連宸宏w3schools練習
#for x in 'Takming gold': #迴圈逐字字元
#   print("字母: %s" % x)
fruits = ['台積電', '鴻海', '聯發科']
for x in fruits:      # 迴圈印出元素
   print ("當前公司: %s"% x)
print ("append") #append在末尾增加
fruits.append("中華電") #append接元素
print(fruits)
print("移除所有元素clear")
fruits.clear() #Removes all the elements from the list
print(fruits)
fruits = ['台積電', '鴻海', '聯發科',"中華電"]
print(fruits)
pig = fruits.copy()
print("輸出pig用copy方法" + str(pig))
print("使用copy和直接賦予值 pig = fruits有何不同? ")
dog = fruits
print("輸出dog直接=" + str(dog))
print("fruits.extend(pig)")
fruits.extend(pig) #extend接陣列或清單
print(fruits)
fruits.extend(['台塑化','台達電','富邦金'])
print(fruits)
#print("幾個台積電? " + str(fruits.count("台積電)))
print(fruits.index("富邦金"))
fruits.insert(10,"國泰金")
print(fruits)
print(fruits.index("富邦金"))
print("fruits元素個數" + str(len(fruits)))
fruits.pop(4)
print(fruits)
fruits.reverse() #反序排列
print(fruits)
fruits.sort() #排序按照字碼
print(fruits)
#體會到只要我有耐心與興趣,我也可以當一個專業的程式開發人員

W3schools陣列方法Array Methods

 Python有一套內建方法(built-in methods).
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the first item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

2023年3月27日 星期一

連宸宏python:集合set串列或清單list字典dictionary元組tuple

w3schools截圖

w3schools程式碼

#連宸宏 集合{},字典{key:value,},元組(),清單]
s= {"台積電", "鴻海", "聯發科"}
t= ("台積電", "鴻海", "聯發科")
list= ["台積電", "鴻海", "聯發科"]
d= {2330:"台積電",2317:"鴻海",2454:"聯發科"}
print("型態" + str(type(s)) + str(s))
print("型態" + str(type(t)) + str(t))
print("型態" + str(type(d)) + str(d))
print("型態" + str(type(list)) + str(list))
#字串與字串才能+所以要用str轉成字串
i = 0
for a in list: 
  i = i+1
  print("台灣第" + str(i) + "大的公司是")
  print("    " + a)
'''大區域註解用三個引號set集合沒有順序unordered, 練習使用迴圈輸出集合內的內容'''

w3schools集合方法

Python has a set of built-in methods that you can use on sets.
MethodDescription
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update()Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others

w3schools清單或串列方法

Python has a set of built-in methods that you can use on lists.
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list
Python has a set of built-in methods that you can use on lists.
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

w3schools元組方法

Python has two built-in methods that you can use on tuples.
MethodDescription
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

2023年3月20日 星期一

連宸宏Pyton字典

w3schols截圖

w3schols練習程式碼

#字典 keys:values, 連宸宏
#w3schools原來 字串:字串,改成 整數:字串
a =	{   #市場價值最大的五家公司
  2330: "台積電",
  2317: "鴻海",
  2454: "聯發科",
  2412: "中華電",
  6505: "台塑化"
  }
print(a)
print(a[6505])
print(a.get(2330))#功能同 a[2330]
print(a.keys())   #keys()方法列出key搜尋鍵
print(a.values()) #keys()方法列出values值
b = a.copy()
print("列出b " + str(b))
print(b[2317])
print(b.clear())
a.update({2308: "台達電"})
print(a.values())
print("用迴圈列出字典a的所有值")
for t in a:
   print(a[t])
.

W3chools字典用法

a
MethodDescription
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 specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary

2023年2月20日 星期一

連宸宏python,print,input,字串方法[::-1]

w3schools

微軟Visiual Studio Code編寫python程式

微軟Visiual Studio Code編寫python程式碼

#劉任昌的第一個python程式2023/2/20
print("你在跟我哈啦嗎?")  #python輸出比Java簡單太多
a = "你在跟我哈啦,狗吃屎" #python指令結尾不需要;分號
b = '貓抓鼠,師父開撈駛來時' #phthon字串單引雙引號都可
print(a)
print(b)
c = a[1:5:] #取字串a的第1到5(不含),從0開始
print(c)
c = a[::-1]
print(c)
a = input("自己輸入字串:")
print("你輸入:"+a)
print("反過來:"+a[::-1])

2023年1月9日 星期一

D11117234連宸宏JavaScript執行輸入字串再反向輸出JS Code編輯Java

JavaScript輸出結果

連宸宏輸入字串:

JavaScript程式碼

<p>連宸宏輸入字串:<input type="text" len="50" id="in"></p>
<p><input type="button" value="連宸宏執行JavaScript" onclick="f()"></p>
<p id="out"></p>
<script>
function f(){
  var a = document.getElementById("in").value;/*取得id=in的文字*/
  var b = "源自串: "+a+"<br>長度是:"+a.length+"<br>反串是: ";
  var c = "";
  for (var i = 0; i < a.length; i++)
    c = a.slice(i,i+1) + c ;
  b = b +"<font size=7>"+ c+"</font>";
  document.getElementById("out").innerHTML = b;
}
</script>

微軟VS Code編輯Java程式

Java程式碼

import java.util.Scanner; /*開啟套件package util=utility用途,Scanner掃描器*/
/*連宸宏utility industry=公用事業產業,電力,自來水,效用=utility */
class MyClass {
  public static void main(String[] args) {
    String a, b="";                           /*定義字串a,b */
    Scanner myObj = new Scanner(System.in);   /*建構掃描物件*/
    System.out.print("輸入: ");
    a = myObj.nextLine();                     /*輸入文字nextLine到變數a */
    System.out.println("長度: " + a.length());/*輸出字串長度length() */
    for (int i = 0; i < a.length(); i++)      /*迴圈 */
        b = a.charAt(i) + b;                  /*a字元順序放到b前面*/
      System.out.println("輸出: "+ b);
    }
}
Visual Studio Code程式開發環境