Python学习日志–摆动吧!钟摆!
由于读研跟着导师搞人工智能方向,所以开始学习python,在学习到使用到tkinter进行GUI设计这部分的时候,有一道很有意思的编程题:用tkinter动态演示钟摆的摆动(钟摆摆起来的时候是很有意思啊!),本题要求我们按方向键上(即Up键)加速钟摆摆速,按方向键下(即Down键)减速,按S键(注意是大写S)停止动画,按R键(大写)重新开始摆动,我的代码如下:
from tkinter import *
import math
class Pendulum:
def __init__(self):
window = Tk() #创建窗口
window.title("Pendulum")
self.canvas = Canvas(window , width = 700 , height = 500 , bg = "white") #创建画布
self.canvas.pack()
self.canvas.create_oval(317,47,323,53,fill = "black" , tags = "center") #画出钟摆中心点
self.x1 = 320 #x1 , y1为钟摆不动端端点
self.y1 = 50
self.radius = 350 #钟摆长度
self.degree =