陳佑竑python全域變數global和判斷if

 

from tkinter import * #或者import tkinter as tk
import math
tk = Tk()           #建構視窗名為tk
tk.geometry('1200x400')#視窗 寬1200像素
tk.title("陳佑竑python tkinter三角函數")
canvas = Canvas(tk, width=1200, height=400, bg='black')
canvas.grid(row=0,column=0,padx=5,pady=5,columnspan=3)
delay=10 # milliseconds, 1/1000秒
x1,y1,z1=0,200,10#python特徵,多變數=對等值
h=190      #上下範圍,相當於數學1到-1
def LH():
    global x1, y1, z1#global全球,local當地
    x2 = x1 + 1 #換到下個+1 
    y2=200 - h*math.sin(0.02*x2)
    z2=200 - h*math.cos(0.02*x2) 
    L1=canvas.create_line(x1,y1,x2,y2,fill='pink',width=10)
    L2=canvas.create_line(x1,z1,x2,z2,fill='white',width=10)   
    if (x2 < 1200): #沒有超過, 記得將 "小於" 改成運算
        x1,y1,z1=x2,y2,z2#下一個起點是現在終點
        canvas.after(delay,LH)#每隔delay執行
    else:
        canvas.delete("all")
        x1,y1,z1=0,200,10
        canvas.after(delay,LH)
        return    
LH()            #執行LauHou老猴
tk.mainloop()

mainloop的說明

tkinter的說明:tkinter 是 Python 的內建函式庫,只要 import tkinter 就能使用,下方的程式碼執行後,會使用 Tk() 方法建立 tkinter 視窗物件,接著使用 mainloop() 將其放在主迴圈中一直執行,直到使用者關閉該視窗才會停止運作。

https://steam.oxxostudio.tw/category/python/tkinter/start.html

財金程式設計615拷貝mainloop的資訊

留言

  1. 截圖要「原始大小」程式碼要加上pre.https://chenyouhong.blogspot.com/2026/01/python.html
    curly brackets. https://chenyouhong.blogspot.com/2025/12/python_28.html
    https://chenyouhong.blogspot.com/2025/12/pythonglobalif.html
    https://chenyouhong.blogspot.com/2025/12/python.html
    https://chenyouhong.blogspot.com/2025/11/611tryexcept.html
    1116截圖要「原始大小」https://chenyouhong.blogspot.com/2025/11/vscodefont_16.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

陳佑竑Python+csv+pandas

陳佑竑python字串方法count與split

陳佑竑PYTHON產生金融常識測驗